Search code examples
c#listboxopenfiledialog

How to get a Listbox to read to a Listbox


I am trying to open any selected textfile and have the text input be sent to a listbox... Originally I wrote this code for a textbox which worked great now that I am converting it to a listbox it doesnt seem to work so much. I left the default item names in order for better understanding of what is going on.

private void button1_Click(object sender, EventArgs e)
{
   if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
   {
      label1.Text = openFileDialog1.FileName;
      listBox1.Items.Add = File.ReadAllText(label1.Text);
   }
}

Solution

  • listBox1.Items.AddRange(File.ReadAllLines(label1.Text));