Populate a dropdown menu from a text file

gkiril

Newcomer
Joined
Feb 15, 2004
Messages
21
Hello,

I've been searching high and low for this answer, but not having any luck. Can someone provide a simple example (VB .net) of saving a value to a text file, populating the dropdown menu with that value, and then reloading that value from the text file when the application is closed and restarted? This code can be executed by clicking a button on a blank form with a splitbutton. Separately, I know how to add items to the splitbutton control, and how to write text to a text file. However, I do not know how to write data in the necessary format to a text file for a dropdown control or how to call/retrieve that information in the form load event. Please help. A simple example will work. As always, thanks in advance.

-gk
 
I won't insult you by adding code to write to a textfile, as you've indicated that you're okay in that area. Try adding this bit of code to your form's load event to read the values and load them into a ComboBox.

Code:
Dim InputFile As String = "SomeFile.txt"
Dim CurrentLine As String()
Dim CurrentField As String

'instantiate a TextFieldParser object 
Dim FileReader As TextFieldParser = My.Computer.FileSystem.OpenTextFieldParser(InputFile)
FileReader.TextFieldType = FieldType.Delimited
FileReader.Delimiters = New String() {","}

'ensure that the file exists before processing
If My.Computer.FileSystem.FileExists("StudentFiles.txt") Then
     'ensure that the file contains data
     While Not FileReader.EndOfData
          Try
               CurrentLine = FileReader.ReadFields
               For Each CurrentField In CurrentLine
                    Me.ComboBox.Items.Add(CurrentField)
               Next
          Catch ex As Exception
               MessageBox.Show("An error occurred while processing.")
          End Try
     End While
End If

As for the format, you need only be concerned with you delimiter. Ensure that, if you have multiple fields per line, you change this line to match your actual delimiter. In this example, I'm indicating that the delimiter is a comma.

Code:
FileReader.Delimiters = New String() {","}

Just because it's piqued my curiosity, why would you be re-writing the same values back to the textfile? Anyway, good luck and I hope this helps.
 
Hello med,

Thank you for the response and the code. It took me a second to get that I had to add an import statement to the MS VB fileIO namespace. Kind of silly. The code worked great!!!

I guess I should clarify, I know how to write text to a file in its simplest form. I thought that might be enough, but then I realized that I am going to need to separate my data, probably under headings of some sort. I suppose I'm looking at having a kind of settings/configuration file in order to populate multiple fields. Any thoughts on that are welcome.

I do have another question, however. I have observed that a combobox (maybe... one of the dropdown boxes anyway) can have a property of .selectedvalue. It seems that the toolstrip splitbutton does not. I can access the text of a specific menu item by using toolstripbutton1.dropdownitems(0).text, but I need to get the text of the menu item that is pressed by the user. If you can help with that, I would also be grateful.

To answer your question, the reason that I need to write dropdown values and then load them is because they are not hard coded. Obviously, when the application closes any values that were added disappear. In this app, the user will be given the ability to access certain functionality/objects and to have those items placed in a shortcuts list (so to speak). It could be a dropdown, a list button, whatever. Right now, I'm just using a splitbutton to get it all together. An example would be changing the color of the form background and saving it (I would rather not use the registry). That example is simplistic, but you get the idea. The user will also have the ability to delete these values. I expect I should be able to use the replace function to edit the text file, or rather, I hope. We'll see when I get there. That's pretty much it. It's just a small piece of the puzzle, but to this point has been the most complex for me.

Anyway, thanks for the help and I hope you can help with the remaining questions.

-gk
 
I have read that it is not recommended to edit the config file. I figured I would use an .ini file type format, but I still need to know how to read the delimited text, under multiple headings. Would editing the config file not follow the same principles?

It turns out though, that my apprehension at using the registry (Microsoft's preferred method) may not be well founded and I may go that route. Regardless, if someone can help with reading data from a text file under multiple headings, I would be grateful.

That said, I am more interested in how to obtain the item value of a splitbutton dropdown menu. Because that does not apply to this thread, I will start another. I will still check here for any posted solutions on how to read the data from a text file.

Thanks to everyone who has responded.

-gk
 
I would tend to avoid the registry as it is often more trouble than it is worth. If you are planning on ini files then http://www.mentalis.org/soft/class.qpx?id=6 is worth a look.

If this information is to be shared between multiple users and is of a read only fashion then the app.config is a perfect place to store this kind of thing.

However given that you seem to be using .Net / vs 2005 the easiest method is probably the built in setting support. If you bring up the project properties and look down the list of tabs on the left of the property page there should be one called 'Settings' - go there and it is pretty self explanatory.
 
Okay, but that begs the question. Do you save information and read information to the config file the same as you would a text file? By the way, I am using VB .net in VS 2005. I do not distinguish between the two. If asked, however, I would not say that I am specifically using the .net architecture.

-gk-
 
Back
Top