0

I want to add items to a "listbox" from a text file by browsing the file that contains text like this:

"one"

"two"

"three"

"many more"

Please tell me how to browse a text file & add items to a listbox.

xelco52
  • 5,257
  • 4
  • 40
  • 56
user1563019
  • 69
  • 1
  • 2
  • 8

2 Answers2

0
foreach (var line in System.IO.File.ReadAllLines("file.txt"))
{
   listbox.Items.Add(line);
}
Michal Klouda
  • 14,263
  • 7
  • 53
  • 77
  • yes i want to select file by user like we save or open file from any location. i want to browse file using textbox that contain file path or a button that select a file. – user1563019 Aug 17 '12 at 21:06
  • Then check the [OpenFileDialog](http://msdn.microsoft.com/en-us/library/aa984392(v=vs.71).aspx). – Michal Klouda Aug 17 '12 at 21:21
0

The code below reads a file and places the text line by line into the Listbox

   ListBox lb = new ListBox();
    System.IO.StreamReader sr = new System.IO.StreamReader("Path to File");

    while (!sr.EndOfStream)
    {
        lb.Items.Add(sr.ReadLine());
    }

    sr.Close();
Sorceri
  • 7,870
  • 1
  • 29
  • 38