In this tutorial, we’ll learn How to add items to listbox from text file in c#.
Example 1:
1 2 3 4 5 6 7 8 9 10 | ListBox lb = new ListBox(); System.IO.StreamReader sr = new System.IO.StreamReader("userTypes.txt"); while (!sr.EndOfStream) { lb.Items.Add(sr.ReadLine()); } sr.Close(); |
Example 2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | OpenFileDialog f = new OpenFileDialog(); if (f.ShowDialog() ==DialogResult.OK) { listBox1.Items.Clear(); List<string> lines = new List<string>(); using (StreamReader r = new StreamReader(f.OpenFile())) { string line; while ((line = r.ReadLine()) != null) { listBox1.Items.Add(line); } } } |