2

I am can't figure out how to put the index [i] in the context when I try to write in to the listbox1. The code I got so far is:

    private void button1_Click(object sender, EventArgs e)
    {            
        StreamWriter Info = File.AppendText("Contacts.txt");

        for (int i = 0; i < listBox1.Items.Count; i++);
            Info.WriteLine(listBox1.Items[i]);
        Info.Close();
    }

I am trying to make a Windows Form application that accepts names and email addresses and places them in a list box. Can anyone help me out on what I am missing here?

5
  • What are you trying to do? Jun 23, 2013 at 20:47
  • I am trying to make a list of email contacts Jun 23, 2013 at 20:48
  • 3
    You said you are adding items in listbox, but in code you are doing opposite i.e. writing listbox items to a file ?
    – Code0987
    Jun 23, 2013 at 20:49
  • If you show you sample output, that will probably help us to get what you want to do
    – Shaharyar
    Jun 23, 2013 at 20:50
  • the output in the list box would look something like this: Tom Spickler TSpic@gmail.com Jessica Murray Jmurray@yahoo.com Jun 23, 2013 at 21:00

2 Answers 2

10

The semicolon in this line for (int i = 0; i < listBox1.Items.Count; i++); may cause an error. Delete it and then try again.

6
  • oh yeah the one at the end, well spotted Jun 23, 2013 at 20:52
  • because of this, that would not be going into the loop
    – Shaharyar
    Jun 23, 2013 at 20:52
  • That worked removing that semicolon now once the button is pressed it does not display in the listbox, how come? Jun 23, 2013 at 21:06
  • @user2514327 what doesn't display in the listbox? while asking something, ask in detail. Don't be precise
    – Shaharyar
    Jun 23, 2013 at 21:10
  • 1
    Can I also suggest following the StyleCop suggestion of using {} for all loops including one-liners; Microsoft added this as an internal review found it to be a common source of bugs similar to yours Jun 23, 2013 at 22:12
1

I don't know what you are trying to do. I am providing you the possible solutions here as i understood your problem.

  1. If you need to write a file with listbox1 contents, same code with correction by Misa Lazovic:

    private void button1_Click(object sender, EventArgs e)
    {            
        StreamWriter Info = File.AppendText("Contacts.txt");
    
        for (int i = 0; i < listBox1.Items.Count; i++)
            Info.WriteLine(listBox1.Items[i]);
        Info.Close();
    }
    
  2. If you need to read the contents from file and place them to your listbox1:

    private void button1_Click(object sender, EventArgs e)
    {            
        foreach (string s in System.IO.File.ReadAllLines("Contacts.txt"))
                listBox1.Items.Add(s);
    }
    
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.