Ben-There

Hello,

My text file has two fields F1 and F2. I am trying to use the F1 as the Value member and F2 as Display Member. I am able to display both fields as a string. I am new to VB and I have tried many unsuccessful versions.

Any help would be great, thanks

Text File

F1,F2

1,Name1

2,Name2

Private Sub Main_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim rdr As IO.StreamReader = IO.File.OpenText("C:\text\N.txt")
While Not rdr.EndOfStream
Dim line As String = rdr.ReadLine()

' Combobox Name

Me.N.Items.Add(line)
End While


rdr.Close()


End Sub



Re: Visual Basic Express Edition Help: Using text file to populate Combo box

SickDotNet

Do you mean splitting the line, like this example:

Code Block

Dim line As String = "1,Name1"

'Split the line by a comma:

Dim arraySplit = line.Split(",")

'Get the right values:

Dim returnId As Integer = arraySplit(0)

Dim returnText As String = arraySplit(1)



... or do you mean how to add both the Text and Value to a combobox If it is the latter, perhaps this post will help you further.





Re: Visual Basic Express Edition Help: Using text file to populate Combo box

Derek Smyth

Hi,

Here is some example code that demonstrates what you need to do....

First create a class that represents a single line in your file....

Code Block

Public Class Lookup
Private m_F1 As String
Private m_F2 As String

Public Sub New(ByVal F1 As String, ByVal F2 As String)
Me.m_F1 = F1
Me.m_F2 = F2
End Sub

Public ReadOnly Property F1() As String
Get
Return m_F1
End Get
End Property

Public ReadOnly Property F2() As String
Get
Return m_F2
End Get
End Property

End Class

Then you parse you file and create create an ArrayList containing a collection of the above class.

Code Block

Dim lookups As New ArrayList

lookups.Add(New Lookup("1", "Hello World"))

lookups.Add(New Lookup("2", "Goodbye World"))

Like that but you'll need to get the values from the file. For each like create a new Lookup class and add it to the ArrayList. Then finally you can bind the combo box to this array list with the following.

Code Block

Me.ComboBox1.DataSource = lookups

Me.ComboBox1.ValueMember = "F1"

Me.ComboBox1.DisplayMember = "F2"

The ValueMember and DisplayMember properties must be set to the property names in the Lookup class. F1 and F2 here correspond the the Lookup.F1 and Lookup.F2 properties of each Lookup object in the ArrayList..

Here's the full code listing....


Code Block

Public Class Form1

Public Sub New()

' This call is required by the Windows Form Designer.
InitializeComponent()

' Add any initialization after the InitializeComponent() call.
Dim lookups As New ArrayList
lookups.Add(New Lookup("1", "Hello World"))
lookups.Add(New Lookup("2", "Goodbye World"))

Me.ComboBox1.DataSource = lookups
Me.ComboBox1.ValueMember = "F1"
Me.ComboBox1.DisplayMember = "F2"

End Sub

Public Class Lookup
Private m_F1 As String
Private m_F2 As String

Public Sub New(ByVal F1 As String, ByVal F2 As String)
Me.m_F1 = F1
Me.m_F2 = F2
End Sub

Public ReadOnly Property F1() As String
Get
Return m_F1
End Get
End Property

Public ReadOnly Property F2() As String
Get
Return m_F2
End Get
End Property

End Class

End Class






Re: Visual Basic Express Edition Help: Using text file to populate Combo box

Ben-a-While

Thank you for the quick response.

The code is working for me, but how would I be able to list all the lines in my text file I am only able to list a line from my file. I have too many lines to type each of them in to the New ArrayList (2000 Lines).

Dim lookups As New ArrayList
lookups.Add(New Lookup("1", "Hello World"))
lookups.Add(New Lookup("2", "Goodbye World"))

He is the code that I have so far:

Thanks for your help.

Ben

Public Class Main

Public Sub New()

' This call is required by the Windows Form Designer.
InitializeComponent()

Dim rdr As IO.StreamReader = IO.File.OpenText("C:\text\N.txt")
While Not rdr.EndOfStream
Dim line As String = rdr.ReadLine()

'Split the line by a comma:
Dim arrayList = line.Split(",")

'Get the right values:
Dim returnId As Integer = arrayList(0)
Dim returnText As String = arrayList(1)

' Add any initialization after the InitializeComponent() call.
Dim lookups As New ArrayList
lookups.Add(New Lookup(arrayList(0), arrayList(1)))

Me.N.DataSource = lookups
Me.N.ValueMember = "F1"
Me.N.DisplayMember = "F2"

End While

rdr.Close()
End Sub

Public Class Lookup
Private m_F1 As String
Private m_F2 As String

Public Sub New(ByVal F1 As String, ByVal F2 As String)
Me.m_F1 = F1
Me.m_F2 = F2
End Sub

Public ReadOnly Property F1() As String
Get
Return m_F1
End Get
End Property

Public ReadOnly Property F2() As String
Get
Return m_F2
End Get
End Property

End Class

End Class





Re: Visual Basic Express Edition Help: Using text file to populate Combo box

Derek Smyth

Hi,

2000 items in a combo list. You sure a combo box is the right approach here, perhaps the list box would be more appropriate or even some sort of filter that loads only a subsection of records, anyway I'll leave that for you to decided.

You just placed the ArrayLisy at the wrong scope, should have class level, in your loop you kept creating a new array.

Public Class Main

Dim lookups As New ArrayList

Public Sub New()

' This call is required by the Windows Form Designer.
InitializeComponent()

Dim rdr As IO.StreamReader = IO.File.OpenText("C:\text\N.txt")
While Not rdr.EndOfStream
Dim line As String = rdr.ReadLine()

'Split the line by a comma:
Dim arrayList = line.Split(",")

'Get the right values:
Dim returnId As Integer = arrayList(0)
Dim returnText As String = arrayList(1)


lookups.Add(New Lookup(arrayList(0), arrayList(1)))

End While

Me.N.DataSource = lookups
Me.N.ValueMember = "F1"
Me.N.DisplayMember = "F2"

rdr.Close()
End Sub






Re: Visual Basic Express Edition Help: Using text file to populate Combo box

Ben-There

Thanks again.

It is working now, the combobox is functional since they are known values and I have the type ahead function working.

I would like to place a filter on my second combo box to limit its values based on the first combobox, how can I accomplish this. Do I just add a field to my P1,P2 etc (F1,P1,P2) or do I make a key file (F1,P1) and then how do I apply the filter I only have done filters on Dataview's therefore I am not sure how apply them to the array.

Thanks,

Ben

F1,F2

1, Name1

2,Name2

P1,P1

1,Product1

2,Product2

Key1,Key2

1,1

1,2

2,1

2,2

Public Class Main

Public Sub New()

' This call is required by the Windows Form Designer.
InitializeComponent()

Dim rdr As IO.StreamReader = IO.File.OpenText("C:\text\N.txt")
While Not rdr.EndOfStream
Dim line As String = rdr.ReadLine()

Public Class Main
Dim Names As New ArrayList
Dim Products As New ArrayList

Public Sub New()

' This call is required by the Windows Form Designer.
InitializeComponent()

Dim rdr As IO.StreamReader = IO.File.OpenText("C:\pocket\text\N.txt")
While Not rdr.EndOfStream
Dim Nline As String = rdr.ReadLine()

'Split the line by a comma:
Dim arrayList = Nline.Split(",")

'Get the right values:
Dim returnId As Integer = arrayList(0)
Dim returnText As String = arrayList(1)

' Add any initialization after the InitializeComponent() call.
Names.Add(New ListNames(returnId, returnText))

End While

Me.N.DataSource = Names
Me.N.ValueMember = "IDN"
Me.N.DisplayMember = "N"

rdr.Close()

Dim prdr As IO.StreamReader = IO.File.OpenText("C:\text\P.txt")
While Not prdr.EndOfStream
Dim Pline As String = prdr.ReadLine()

'Split the line by a comma:
Dim arrayList = Pline.Split(",")

'Get the right values:
Dim ProductId As Integer = arrayList(0)
Dim ProductText As String = arrayList(1)

' Add any initialization after the InitializeComponent() call.
Products.Add(New ListProduts(ProductId, ProductText))


End While

Me.P.DataSource = Products
Me.P.ValueMember = "IDP"
Me.P.DisplayMember = "P"

prdr.Close()
End Sub





Re: Visual Basic Express Edition Help: Using text file to populate Combo box

Derek Smyth

Hello again,

Ok if your needing a filter then perhaps an array is not your best choice. You could certainly get it working but a more elegant approach would be to use a DataTable. From this DataTable you can create a DataView that you can filter on, the good thing about this is you can still use DataBinding with the combo box. Set DataSource to the Table and the Value/Display members to the table column names.

So really what I think you want to do is read your files into a DataSet, actually looking at the data it looks like it is a Database structure, F1 and P1 looked to be related through Key1 and Key2. If this is correct then I would give serious thought to replacing these text files with a database.

If you have experience of DataViews then you should be familar with DataTables and DataSets so really I think forget the ArrayList and my last suggestion (didn't know you had relational data) and treat the data as it's structured.