add textbox to listbox c# code example

Example 1: add items to listbox from text file c#

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: add text file to listbox c#

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);

            }
        }
    }