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 want 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 following and a third combobox based on the previous one. I didn’t find any solution on Internet. :( May possibly somebody please help me with this issue ? Be grateful you.

Best answer:

Answer by ross613
Fascinating 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 on terrible terms 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 end 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 here anything we need to do to the data when its showed in the combobox or can we show 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 showed in the ComboBox: String.Split() is one way of doing this. But, you may recall that the ComboBox is a special kind of control called a “data leap control”; which means it offers properties and methods that specifically facilitate working with data. In fastidious, the ComboBox.DataSource material goods is compatible with objects that expose the IEnumberable boundary – and so it force 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 material goods of your ComboBox control and you’ll see your data showed (in a raw, untreated form) therein:

Dim rawTextData As string;
… ‘ insert judgment 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 assess – I work mostly in C#, but the thoughts are basically the same since it’s all .NET. ;)

Excellent luck!

What do you reckon? Answer below!

Post a comment below...
Related Products:

  • Share/Bookmark

Related posts:

  1. How do I take a .vb file and compile it in visual studio 8?
  2. A Guided Tour of the Visual Dataflex Rad Database Development Studio
  3. Visual Basic 2008 Tutorial – Copy a File
  4. Finishing touches on little brother c++ program to read text file.?
  5. Find a certain string within a text file in VB .NET?
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>