open a file whose name and directory are determined by the content selected in combobox vb.net
I have a combobox (combobox1) with a list of names for example: CHARLY SUSAN BOB.
What I'm trying to achieve is to read the contents of a corresponding file in another combobox (combobox2) so
- CHARLY would open CHARLY.txt and populate combobox2 with that files contents
- SUSAN would open SUSAN.txt and populate combobox2 with that files contents
- BOB would open BOB.txt and populate combobox2 with that files contents
The list of names its self is read from a text file this is because I would in future like to add more names.
These are read into (Combobox1) by the following code:
'Compiles list for plotter/ printer items... ComboBox1.Items.AddRange(System.IO.File.ReadAllLines("C:\PEOPLE\NAMES.txt"))
I’m thinking to keep the concept open to an ever expanding list to set a variable/ string to the target name then add a prefix to it example being: BOBfile.txt with a suffix also added which would point to the target files directory thus then being C:\PEOPLE\BOBSfile.txt
This set in a variable would then open the concerning folder as follows:
ComboBox2.Items.AddRange(System.IO.File.ReadAllLines( BOBSVARIABLE ))
Dose anybody know how to achieve this?
Here is a simple example:
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) _ Handles ComboBox1.SelectedIndexChanged If ComboBox1.SelectedIndex = -1 Then ComboBox2.Items.Clear() Else Dim peoplePath As String = "C:\PEOPLE" Dim personFile As String = Path.Combine(peoplePath, _ ComboBox1.SelectedItem & "file.txt") If File.Exists(personFile) Then ComboBox2.Items.AddRange(File.ReadAllLines(personFile)) Else ComboBox2.Items.Clear() End If End If End Sub
Do consider a database instead. Also, your current system wouldn't handle duplicate names very well.