important question concerning listboxes

Keo

New member
Joined
Nov 26, 2006
Messages
4
Programming Experience
Beginner
hello everyone, hopefully i can explain this in a clear to understand way

what im trying to do is....

there are 2 comboboxes and a richtextbox.. on the first combobox you select #1 or #2.. if you select #1... combobox 2 now becomes populated with a list of files in a directory i specified (they are text files).. now.. the part i have spent hours trying to get to work is, im trying to make it where when you select one of those "items" (one of the file names that combobox 2 is populated with) it should read the contents of that selected text file and display it in the richeditbox ..

hopefully all that made sense ;) thanks for the help / suggestions! :)
 
you'll need to be able to know which file is associated with each item in combobox 2, you can do this with an arraylist or using the combobox's value property then once you know what file to open, simply pass that to the streamreader and set the richtextbox's text property using the reader's ReadToEnd() method
 
When you have finished filling the combobox with filenames you can set the 'initalizing' variable to False. The code in SelectedValueChanged event handler is only executed when the 'initalizing' variable is False. This because if you set datasource the event triggers each filled item.
VB.NET:
Private initalizing As Boolean = True
 
Private Sub ComboBox1_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles ComboBox1.SelectedValueChanged
    If Not initalizing Then
        RichTextBox1.Text = My.Computer.FileSystem.ReadAllText(ComboBox1.SelectedValue) 'or .SelectedItem depending on fill
    End If
End Sub
You can also use this event and code:
VB.NET:
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles ComboBox1.SelectedIndexChanged
    RichTextBox1.Text = My.Computer.FileSystem.ReadAllText(ComboBox1.Items(ComboBox1.SelectedIndex))
End Sub
 
earlier in my post i made mistake stating "combobox" i meant "listbox" :eek::D

i still though tried your second code sample, (replacing combobox with listbox) and it gives me this error

"Conversion from type 'FileInfo' to type 'String' is not valid."

thanks! :)
 
Listboxes and comboboxes behave the same.
Use the FullName property of FileInfo class instance..
VB.NET:
Dim fullnamestring As String = DirectCast(your_fileinfo_object, FileInfo).FullName
 
thanks john! i got it to work ;)

VB.NET:
 Dim fullnamestring As String = DirectCast(ListBox3.SelectedItem, IO.FileInfo).FullName

        RichTextBox1.Text = My.Computer.FileSystem.ReadAllText(fullnamestring)
 
Back
Top