How do I populate a ComboBox in vb.net (visual studio 2008) with .txt file?

Question by WisoBe: How do I populate a ComboBox in vb.net (visual studio 2008) with .txt file?
Hello,

I would like to populate a combobox in vb.net with a .txt file. Unfortunately I don’t know how to do this. The main goal afterwards is to populate a second and a third combobox based on the previous one. I didn’t find any solution on Internet. :( Could somebody please help me with this issue ? Thank you.

Best answer:

Answer by ross613
Interesting problem. But not a hard one to solve – if we apply the core approach used to solve any problem with software: divide and conquer. The problem can be divided as follows:

- standardize your data (the .txt file(s)) by either determining how records are delimited; by a character like a semicolon or comma (comma-delimited data is used by MS Excel and is typically a text file ending with .csv) or maybe it’s data delimited by a carriage return – that is one record per line
- determine how the data will be organized in memory; will it be elements of an array or, more likely, some kind of objects that can easily be consumed by the control you’re using (in this instance, the ComboBox)
- format the presentation of the data; is there anything we need to do to the data when its displayed in the combobox or can we display it as-is?

So the first step is reading the data and you’ll want to consider using the File object in the System.IO namespace and doing something like a File.Open() to read your text file into a string object.

Next, you’ll want to break up the string into the individual data records we’ll use as items to be displayed in the ComboBox: String.Split() is one way of doing this. However, you may recall that the ComboBox is a special kind of control called a “data bound control”; which means it offers properties and methods that specifically facilitate working with data. In particular, the ComboBox.DataSource property is compatible with objects that expose the IEnumberable interface – and so it might be worthwhile converting that string array (represented as string()) to a generic List (or List). Once you have your data in a List, you can simply assign that member to the DataSource property of your ComboBox control and you’ll see your data displayed (in a raw, untreated form) therein:

Dim rawTextData As string;
… ‘ insert logic to read text file into rawTextData here…

Dim myTextData As string()
myTextData = rawTextData.Split(Chr(10))
‘ at this point your text file is records in the string array myTextData
Dim bindableTextData As New List(Of String)

For Each s As string In myTextData
·····bindableTextData.Add(s)
Next

ComboBox.DataSource = bindableTextData

NOTE: The above syntax may not all be entirely correct – I work mostly in C#, but the ideas are basically the same since it’s all .NET. ;)

Good luck!

What do you think? Answer below!

This entry was posted in Vb.net File and tagged , , , , , , , , , , , , , , , , , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>