Good morning c# experts,
I have another question for you: Is it possible to populate a combobox with data in a txt file?
For example: I have a textbox (textbox1) where the user enters a building number and based on that input, combobox1 would populate with all the room numbers associated with that building. I already have a txt file with all the corresponding information:
439: 123, 124, 424, 136
321: 134, 843, 339, 399]
So, if the user enters 439 into textbox1, combobox1 would populate with 123, 124, 424, 136. IS that possible?
Loading
Kirtan PatelPosted Sep 3, 2009, 11:51 AM
Here i coded it for you :)
I have also Provided Project Files so you can take a Look if need reference Download Link is at bottom of the post :)
dont forget to mark "Do you like answer" please it will give me some credits :)
private void btnPopulate_Click(object sender, EventArgs e)
{
if (txtRoomNo.Text != "")
{
string RoomNumberToPop = txtRoomNo.Text.Trim();
string filePath = Environment.CurrentDirectory + @"\test.txt";
string AllData = File.ReadAllText(filePath);
string[] lines = AllData.Split('\n');
comboBox1.Items.Clear();
foreach (string line in lines)
{
if (line.Contains(RoomNumberToPop))
{
string[] subdata = line.Split(':');
string[] subsubdata = subdata[1].Split(',');
foreach (string str in subsubdata)
{
comboBox1.Items.Add(str);
}
}
}
}
}
Download Project Files
BenPosted Sep 3, 2009, 11:49 AM
if you don't know if there's gonna be a ] at the end of the lines you can do a split(']')[0] on each strg before adding them to the combobox.
if the ] is only on the last line edit your file to put it on a new line.
BenPosted Sep 3, 2009, 11:42 AM
the first split gets rid of the "id number"
then you create an array of strings, cutting the line on each ','
for each string in the array you add an item to your combobox.
You'll need to empty the comboBox before executing this code with combobox.item.clear();
Hope it helps.