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?

Answers


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.


Need Your Help

Apache module in C: how to write data to client socket?

c apache sockets mod-perl apache2-module

I have created a simple mod_perl module, which writes a 0-terminated string to the Flash-clients connecting to the port 843. It works ok, but uses 20m per httpd-child at my CentOS 5 Linux machine.

Generating HTML dynamically in modern Perl web frameworks

html perl cgi template-toolkit

What is the "modern Perl" recommended way to generate HTML dynamically?