Sir/Ma'am
I have a .txt file int the following format:
[test1]
blue
red
green
[test2]
one
two
three
[test3]
white
black
grey
I'm trying to read the everything below [test2] and above [test3] into a combobox. So, I'm trying to populate comboBox1 with one, two, and three. I thought this might work, but I was wrong:
{
comboBox1.Items.Clear();
using (StreamReader sr = new StreamReader(@"c:\test.cfg"))
{
String line1;
do
{
line1 = sr.ReadLine();
if (line1.StartsWith("[test2"))
{
line1 = line1.Split(']')[1].Trim();
String[] strgArray;
strgArray = line1.Split('\n');
foreach (String strg in strgArray)
{
comboBox1.Items.Add(strg.Trim());
}
break;
}
}
while ((line1 != null));
Console.Read();
}
}
}
THANK YOU!!!
Loading

Amit ChoudharyRank #145 · 15.8K reputationPosted Mar 26, 2010, 10:38 PM EDT
i have worked on your code.. it is reading form the file and binding to combobox is working well:
But there is a problem i found that your string "[test1]" is not readable by StreamReader. it just getting skipped while reading from file and so
the condition
Hope it helps. Please mark as answer if you get some help.
Jaish MathewsRank #225 · 11K reputationPosted Mar 26, 2010, 10:47 PM EDT
using (StreamReader str = new StreamReader(@"c:\test.cfg"))
{
String line1;
while (!str.EndOfStream)
{
line1 = str.ReadLine();
if (line1.StartsWith("[test2"))
{
line1 = str.ReadLine();
while (!line1.StartsWith("[test3"))
{
//ADD ITEMS HERE//////////////
line1 = str.ReadLine();
}
}
}
}
matthew king51 reputationPosted Mar 27, 2010, 7:36 AM EDT
You guys nailed it! You came up with exactly what I asked for. However, I did a poor job of trying to articulate what I was try to accomplish. I'm trying to make the .cfg file dynamic, so the the order in which the [test1], [test2], and [test3] won't matter. From what I asked, test2 would have to be between [test1] and [test3] for combox2 to be populated with the data. However, what happens if the user moves [test2] up one position. Thus, [test1] would be populated in the combobox. Again, I appreciate the help!
Jaish MathewsRank #225 · 11K reputationPosted Mar 28, 2010, 9:47 AM EDT
Which set needs to populate with combo?