How to load a text file to ComboBox

laroberts

Freshman
Joined
Jun 29, 2005
Messages
34
I have a form in VB.NET in which users will need to make new jobs and so on which means the drop down list will need to change all the time. The information is stored in either RTF files or text files. I need to know how to do this... when the form opens, I need that combobox to load its selections from a text file or RTF file. Can somebody show me a way of doing this?

Thank you
 
Use a System.IO.StreamReader to read the file into an ArrayList. Then set that ArrayList as the DataSource for the ComboBox.
Visual Basic:
        Dim myStream As New IO.StreamReader("file path")
        Dim fileLines As New ArrayList

        While myStream.Peek <> -1
            fileLines.Add(myStream.ReadLine())
        End While

        myStream.Close()
        Me.ComboBox1.DataSource = fileLines
 
One more thing....

I have a button on that form that says find job. When the user presses this button it needs to load a text file depending on what the option was they choose from the drop down list. Now I know how to do this if the list is hard coded. You just do if and elseif commands... but I do not know how to code it for something like this where the drop down list can change all the time. Any code examples?

Thanks
 
You must know something about the value they are selecting. What does it represent? Can it be any value at all or are there only certain legal values? You may well be able to use a Select statement if you know what the values can be. If it is the name of the file, then you simply pass that name to the method that opens the file.
 
more...

Well for example all of the files in the directory are in this format: Job #xxxx
The xxxx can be any set of numbers, like Job #3454

The drop down list pulls in a textfile that contains the Job numbers. For example the test file it reads looks like this:

Job #3525
Job #325432
Job #2334

And so on.

So lets say the user selects Job #2334 from the drop down list, how do I tell VB.NET to go out and load the RTF file Job#2334?

Thanks!
 
I'll assume that the job file name is in the form "JobXXXX.rtf" for this example. If its not then you can adapt it accordingly:
Visual Basic:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim selectedItem As String = CStr(Me.ComboBox1.SelectedItem)
        Dim selectedJobNum As Integer = selectedItem.Substring(selectedItem.IndexOf("#"c) + 1)

        Process.Start(String.Format("C:\\MyJobFolder\\Job{0}.rtf", selectedJobNum))
    End Sub
This will open the file in the default program for RTF file type, which would probably be Word or Wordpad. If you want to use the file in some other way then do so, but the call to String.Format would be the same to create the file path.
 
Thank you

Thank you very much. I will give it a try. Yes the file is called into a Rich Text Box on the form.
 
Not working....

For some reason this is not working. I keep getting this error.....

The system cannot find the file specified.

It never changes the 0 to the job number that is selected from the drop down list...
 
Last edited:
The numbered place-holders in a format string have braces (curly brackets) around them, not parentheses (round brackets).
 
Nope still not working even after I change that. I do not think its the code thats the problem.... How should I format the name of the files for it to load? Another words should they be called "Job#xxxx" or "#0000" or just"xxxx"

I think thats the problem and I am not sure exactly how this code is formating it.

Thank you.
 
Back
Top