Patrick,
It ain't that straightforward. Add the following code to a form with a
combobox and a textbox. Note a couple of things. I have created a
structure, "FileItem" and also you will want to change the textfile path.
The SelectedIndexChanged event procedure signature has also been modified.
We DON'T want it firing during the loading of the combobox. The AddHandler
at the end of the form_open procedure sets up the delegate...after loading
the combobox.
This example will display the name in the combobox, and the rest of the
string in the textbox, when a name is selected.
HTH,
Larry Woods
(Sorry for everything being left-justified. When you add it to your form it
will reformat.)
CODE:
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs)
TextBox1.Text = CType(ComboBox1.SelectedValue, String)
End Sub
Private Sub frmFillComboFromSeq_Load(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles MyBase.Load
Dim intCount As Integer
Dim strPeople As String
Dim strLine As String
Dim a As New ArrayList
If System.IO.File.Exists("test.txt") Then
Dim objstreamreader As StreamReader = System.IO.File.OpenText("test.txt")
strLine = objstreamreader.ReadLine
Do Until strLine Is Nothing
intCount = intCount + 1
a.Add(New FileItem(strLine))
Me.ComboBox1.Items.Add(a)
strLine = objstreamreader.ReadLine()
Loop
objstreamreader.Close()
With Me.ComboBox1
..DataSource = a
..DisplayMember = "Name"
..ValueMember = "Detail"
AddHandler .SelectedIndexChanged, AddressOf ComboBox1_SelectedIndexChanged
End With
End If
End Sub
End Class
Public Class FileItem
Private targetName As String
Private targetDetail As String
Public Sub New(ByVal fileRecord As String)
MyBase.New()
Dim strTemp As String = Trim(fileRecord)
Dim firstIndex As Integer = strTemp.IndexOf(","c)
targetName = strTemp.Substring(0, firstIndex)
targetDetail = strTemp.Substring(firstIndex + 1)
End Sub
Public ReadOnly Property Detail() As String
Get
Return targetDetail
End Get
End Property
Public ReadOnly Property Name() As String
Get
Return targetName
End Get
End Property
Public Overrides Function ToString() As String
Return targetName & targetDetail
End Function
End Class
END OF CODE
"Patrick" <pa*****@hello.com> wrote in message
news:eg**************@TK2MSFTNGP15.phx.gbl...
Hello - I can read the csv file line by line into a one dimensional
array - no problem. But when the data has 7 columns - and you'd like the Names to
appear in a combo box, it can get tricky.
Fred,0,0,0,0,0,0
Wilma,-1,1,0,0,0,0
Betty,1,0,0,0,-1,0
etc
I found examples elsewhere of using every language BUT VB.NET - so if
somebody wouldn't mind, please? Here is my current status, and even though
I'm only placing the name into a ComboBox now, I plan on making hay with
the rest of the data elsewhere.
Dim intCount as integer
Dim strPeople as string
If System.IO.File.Exists("c:\temp\test.csv") Then
objstreamreader = System.IO.File.OpenText("c:\temp\test.csv")
Do Until objstreamreader.peek = -1
strLine = objstreamreader.readline()
intCount = intCount + 1
Dim strPeople(,) As String = {{strLine}}
Me.RaceComboBox.Items.Add(strPeople(intCount, 1))
Loop
objstreamreader.close()