1
Reply

Please help with an array.

Alina Zamorskaya

Alina Zamorskaya

Oct 22 2013 9:28 PM
970
Hello everyone,

I need some help. I am working in a Windows Form application in C# in Microsoft Visual Studio.

I have created 3 labels.
I have created 2 buttons.
        The first button called button1 is the button to add a contact.
        The second button called button2 is the button that refreshes the list box.
I have created 1 list box called listBox1
I have created 2 textboxes
        The first is called nameTextBox
        The second is called emailTextBox
All of these are located on a form called form1

My application is supposed to get input from the user (name and email). The user click Add Contact button and the information saves to a .txt file. My code does this fine so far. Then, the user clicks the Refresh button and the list box is supposed to show all the information in the .txt file. This is where I run into a problem. I believe the problem is in my array. If someone could help that would be great!

Here is my code:

using



System;


using



System.Collections.Generic;


using



System.ComponentModel;


using



System.Data;


using



System.Drawing;


using



System.Text;


using



System.Windows.Forms;


using



System.IO;



namespace



Lab7A


{


   


public partial class Form1 : Form


    {



       


public Form1()


        {


            InitializeComponent();


        }



       


private void Form1_Load(object sender, EventArgs e)


        {


         


        }



       


private void nameLabel_Click(object sender, EventArgs e)


        {



        }



       


private void nameTextBox_TextChanged(object sender, EventArgs e)


        {



        }



       


private void emailLabel_Click(object sender, EventArgs e)


        {



        }



       


private void emailTextBox_TextChanged(object sender, EventArgs e)


        {



        }



       


private void button1_Click(object sender, EventArgs e)


        {


           


string name, email;



           


try


            {


               


using (StreamWriter storeInfo = new StreamWriter("info.txt", true))


                {


                    name = nameTextBox.Text;


                    email = emailTextBox.Text;



storeInfo.WriteLine(name +


"|" + email);


                   


this.button2.Visible = true;


                    nameTextBox.Text =


string.Empty;


                    emailTextBox.Text =


string.Empty;


                }


            }


           


catch (Exception exc)


            {


               


MessageBox.Show("Error!\n" + exc.Message);


            }


        }



       


private void contactsLabel_Click(object sender, EventArgs e)


        {



        }



       


private void listBox1_SelectedIndexChanged(object sender, EventArgs e)


        {



        }



       


private void button2_Click(object sender, EventArgs e)


        {


           


string info;


           


string [ ] recordData = new string [2];


           


string cname = string.Empty;


           


string cemail = string.Empty;


           


string outInfo = string.Empty;;



           


try


            {


               


using (StreamReader getInfo = new StreamReader("info.txt"))


                {


                    listBox1.Items.Clear();


                   


while (!getInfo.EndOfStream)


                    {


                        info = getInfo.ReadLine();


                        recordData = info.Split (


'|');


                        outInfo = recordData[0] +


"     " +


                           


String.Format("{0:c}", double.Parse(recordData[0]));


                        listBox1.Items.Add(outInfo);


                    }


                   


                    listBox1.Visible =


true;



                }


            }


           


catch (Exception exc)


            {


                listBox1.Text = exc.Message;


            }


        }


    }


}




Answers (1)