Populate combo box from txt file
Populate combo box from txt file
Populate combo box from txt file
Populate combo box from txt file
Populate combo box from txt file
Populate combo box from txt file Populate combo box from txt file Populate combo box from txt file Populate combo box from txt file Populate combo box from txt file Populate combo box from txt file Populate combo box from txt file Populate combo box from txt file
Populate combo box from txt file Populate combo box from txt file
Populate combo box from txt file
Go Back  Xtreme Visual Basic Talk > > > Populate combo box from txt file


Reply
 
Thread Tools Display Modes
  #1  
Old 06-20-2006, 10:01 AM
flipside82 flipside82 is offline
Regular
 
Join Date: Nov 2003
Posts: 73
Default Populate combo box from txt file


I am trying to populate a combo box from a text file i have stored on my local hard drive. I am trying to transition from vb6 to vb2005 and things are very new to me. I can get the code to read my text file, but I can't get the data to go into the combo box one at a time. When I do, they all show up in one line in the combo box.

Dim fileReader As String
fileReader = My.Computer.FileSystem.ReadAllText("C:\CITAlert\users.txt")
cmbUsers.Items.Add(fileReader)

Any help would be great. Thanks.
Reply With Quote
  #2  
Old 06-20-2006, 10:06 AM
reboot's Avatar
rebootPopulate combo box from txt file reboot is offline
Keeper of foo

Retired Moderator
* Guru *
 
Join Date: Nov 2001
Location: Graceland
Posts: 15,614
Default

That's because you're reading it all at one time. fileReader contains the entire file.
__________________
~ Quod non mortiferum, fortiorem me facit ~

Avatar by lebb
Reply With Quote
  #3  
Old 06-20-2006, 10:07 AM
flipside82 flipside82 is offline
Regular
 
Join Date: Nov 2003
Posts: 73
Default

So, what would read the file, one line at a time?
Reply With Quote
  #4  
Old 06-20-2006, 10:13 AM
reboot's Avatar
rebootPopulate combo box from txt file reboot is offline
Keeper of foo

Retired Moderator
* Guru *
 
Join Date: Nov 2001
Location: Graceland
Posts: 15,614
Default

Code:
Dim fileReader As System.IO.StreamReader fileReader = My.Computer.FileSystem.OpenTextFileReader("C:\CITAlert\users.txt") Dim stringRead As String stringRead = fileReader.ReadLine() cmbUsers.Items.Add(stringRead)
__________________
~ Quod non mortiferum, fortiorem me facit ~

Avatar by lebb
Reply With Quote
  #5  
Old 06-20-2006, 10:33 AM
flipside82 flipside82 is offline
Regular
 
Join Date: Nov 2003
Posts: 73
Default

That adds the first item from my txt file, but doesn't read the rest of them. How do I get it to loop through the entire txt file?
Reply With Quote
  #6  
Old 06-20-2006, 10:34 AM
wayneph's Avatar
waynephPopulate combo box from txt file wayneph is offline
Web Junkie

Retired Moderator
* Expert *
 
Join Date: Apr 2004
Location: D/FW, Texas, USA
Posts: 8,393
Default

put the ReadLine and Add in a loop?
__________________
-- wayne, MSSM Retired
> SELECT * FROM users WHERE clue > 0
0 rows returned
Reply With Quote
  #7  
Old 06-20-2006, 10:47 AM
reboot's Avatar
rebootPopulate combo box from txt file reboot is offline
Keeper of foo

Retired Moderator
* Guru *
 
Join Date: Nov 2001
Location: Graceland
Posts: 15,614
Default

You can lead a horse to water, but you can't always make him drink.
__________________
~ Quod non mortiferum, fortiorem me facit ~

Avatar by lebb
Reply With Quote
  #8  
Old 06-20-2006, 10:50 AM
flipside82 flipside82 is offline
Regular
 
Join Date: Nov 2003
Posts: 73
Default

I am unable to get the loop to work. Any suggestions?

Do
stringRead = fileReader.ReadLine()
cmbUsers.Items.Add(stringRead)
Loop
Reply With Quote
  #9  
Old 06-20-2006, 10:51 AM
wayneph's Avatar
waynephPopulate combo box from txt file wayneph is offline
Web Junkie

Retired Moderator
* Expert *
 
Join Date: Apr 2004
Location: D/FW, Texas, USA
Posts: 8,393
Default

add a condition to your loop. Right now you don't have anything on there to tell the loop to ever stop.
__________________
-- wayne, MSSM Retired
> SELECT * FROM users WHERE clue > 0
0 rows returned
Reply With Quote
  #10  
Old 06-20-2006, 12:13 PM
flipside82 flipside82 is offline
Regular
 
Join Date: Nov 2003
Posts: 73
Default

I've got the combo boxes to be populated using a database and that is working okay now. But, how do I get the entry in the combo box to go into a label when a button is clicked. For example... my form loads and populates the combo box. I select an entry from the drop down and then click a button to go to another form and I want the entry i selected from the drop down box, to appear on the next form in a label. Is this possible? I keep getting the error Error: Value of type 'System.Windows.Forms.ComboBox' cannot be converted to 'String'
Reply With Quote
  #11  
Old 06-20-2006, 01:12 PM
wayneph's Avatar
waynephPopulate combo box from txt file wayneph is offline
Web Junkie

Retired Moderator
* Expert *
 
Join Date: Apr 2004
Location: D/FW, Texas, USA
Posts: 8,393
Default

Highlight your combobox, Press F1 and look at the ComboBox properties in the help window. You should see one that looks like it would give you the selected value.
__________________
-- wayne, MSSM Retired
> SELECT * FROM users WHERE clue > 0
0 rows returned
Reply With Quote
  #12  
Old 06-20-2006, 01:58 PM
flipside82 flipside82 is offline
Regular
 
Join Date: Nov 2003
Posts: 73
Default

Thanks for the reply, but it doesn't work. I've tried all the 'Selected' things and none of them work. Any other ideas?
Reply With Quote
  #13  
Old 06-20-2006, 02:23 PM
Baaklar Baaklar is offline
Centurion
 
Join Date: Mar 2003
Location: Omaha, Nebraska
Posts: 197
Default

They work. Try posting the code where you've attempted wayneph's suggestion, and we can show you where it went wrong.
__________________
The first one in, the last one out, and if the gods permit, the last one standing.
Reply With Quote
  #14  
Old 06-20-2006, 02:28 PM
reboot's Avatar
rebootPopulate combo box from txt file reboot is offline
Keeper of foo

Retired Moderator
* Guru *
 
Join Date: Nov 2001
Location: Graceland
Posts: 15,614
Default

You saying "nothing works" doesn't help us help you. Be specific, post code. We aren't psychic.
__________________
~ Quod non mortiferum, fortiorem me facit ~

Avatar by lebb
Reply With Quote
  #15  
Old 06-21-2006, 08:29 AM
flipside82 flipside82 is offline
Regular
 
Join Date: Nov 2003
Posts: 73
Default

Here is the code I have so far. I get error messages that the combo box entries cannot be converted to string.

Imports System
Imports System.Data
Imports System.Data.Odbc

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'create connection and command string
Dim conStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\CIT Alert\alert.mdb"
Dim sqlStr As String = "SELECT * FROM Alert"
'create connection object
Dim conn As OleDb.OleDbConnection = New OleDb.OleDbConnection(conStr)
'create data adapter object
Dim da As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter(sqlStr, conn)
'create dataset object and fill with data using data adapter's fill method
Dim ds As DataSet = New DataSet()
da.Fill(ds, "Alert")
'attach datasets DefaultView to the datagrid control
Dim dv As DataView = ds.Tables("Alert").DefaultView
cmbUsers.DataSource = dv
cmbUsers.DisplayMember = "users"



End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Label1.Text = cmbUsers.SelectedValue


End Sub
End Class
Reply With Quote
  #16  
Old 06-21-2006, 10:36 AM
Baaklar Baaklar is offline
Centurion
 
Join Date: Mar 2003
Location: Omaha, Nebraska
Posts: 197
Default

Use vb tags for code - makes it easier to read.

This assumes a form with a button, combobox and a label.
Code:
Public Class Form1 Inherits System.Windows.Forms.Form Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Me.Label1.Text = Me.ComboBox1.SelectedItem End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load With Me.ComboBox1 .Items.Add("a") .Items.Add("b") .Items.Add("c") End With End Sub End Class

Worked just fine for me.

You say yours isn't working - what is it doing then?
__________________
The first one in, the last one out, and if the gods permit, the last one standing.
Reply With Quote
  #17  
Old 06-21-2006, 10:45 AM
flipside82 flipside82 is offline
Regular
 
Join Date: Nov 2003
Posts: 73
Default

I am loading the combo box from a database table called table. That all is working just fine. The combo box is showing all of the records that exist but I cannot get the item that I select from the combo box to appear in a separate label field on the form when I click a button. I get errors saying that "Conversion from type 'DataRowView' to type 'String' is not valid." So, I don't know where to go from here. Thanks for the reply though.
Reply With Quote
  #18  
Old 06-21-2006, 10:48 AM
MikeJ's Avatar
MikeJPopulate combo box from txt file MikeJ is offline
Retread

Retired Moderator
* Expert *
 
Join Date: Sep 2002
Location: Austin, Texas
Posts: 6,747
Default

Since DataRowView inherits from Object, see if DataRowView.ToString() returns what you want.
__________________
{ Lex Fori } { Locus Classicus } { Rutilus Scrinium }
Osculare pultem meam!
Reply With Quote
  #19  
Old 06-21-2006, 10:49 AM
Baaklar Baaklar is offline
Centurion
 
Join Date: Mar 2003
Location: Omaha, Nebraska
Posts: 197
Default

Try

Code:
Label1.Text = CStr(cmbUsers.SelectedValue)

And see if that fixes the issue.
__________________
The first one in, the last one out, and if the gods permit, the last one standing.
Reply With Quote
  #20  
Old 06-21-2006, 10:53 AM
flipside82 flipside82 is offline
Regular
 
Join Date: Nov 2003
Posts: 73
Default

where would I use the datarowview.tostring() in my code?
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Populate combo box from txt file
Populate combo box from txt file
Populate combo box from txt file Populate combo box from txt file
Populate combo box from txt file
Populate combo box from txt file
Populate combo box from txt file Populate combo box from txt file Populate combo box from txt file Populate combo box from txt file Populate combo box from txt file Populate combo box from txt file Populate combo box from txt file
Populate combo box from txt file
Populate combo box from txt file
 
Populate combo box from txt file
Populate combo box from txt file
 
-->