I’m loading values from file via File.ReadAllText, straight into ListBox1
the file is .txt – Comma Separated.
HealthyFood.Txt
content:
Dairy,Fruits,Vegetables,Whole Grains
to populate Listbox1 (LstBox_HealthyCat) in its simple way (no Condition applied ) With little Help of my Helper-Method actually, so I will not have repeat my code for the other files(:
public string GetFileContent(string FileName)
{
string Filecontent = "";
string ExtTXT = ".txt";
string initialDir = @"G:\RobDevI5-3xRaid-0\Projects\WindowsFormsApplication1\bin\x64\Debug\HealthFood\";
Filecontent = File.ReadAllText(intialDir + FileName + ExtTXT);
return Filecontent;
}
string[] HealthyFood = GetFileContent(HealthyFood).Split(',');
LstBox_HealthyCat.Items.AddRange(HealthyFood);
So now the result is ListBoxMainCategory will be
Dairy
Fruits
Vegetables
Whole Grains
So Far it’s good example for new developers
My Question is, next step I would like to populate
The Sub-Category oF HealthyFood e.g Fruits
fruits.txt
content:
Apple,Banana,Cherry,Dates
So when I will click on Main Category: Fruits
it will populate ListBox2 with selection of ListBox1
and rest of items availble in listBox1 as well(when clicked).
alternative to my
Filecontent = File.ReadAllText(intialDir + FileName + ExtTXT);
Tim Schmelter’s code:
System.IO.Path.Combine(initialDir, item + ExtTXT);
path combine … nice one !
So if i understood your requirement correctly, you want to fill the second ListBox according to the SelectedItem(s) of ListBox1. For instance, if
fruitwas selected thenfruits.txtshould be used and so on. Then this might help you: