views:

48

answers:

3

Hey guys, I am having trouble effectively transferring data between forms.

So I have my entry form. It has one button:

    private void addBtn_Click(object sender, EventArgs e)
    {
        string data = string.Format("{0} \t {1} \t {2} \t {3} \t {4} \t {5}",
            fnameTxtBox.Text, 
            lnameTxtBox.Text, 
            cellNumTxtBox.Text, 
            landLineTxtBox.Text, 
            dobPicker.Text, 
            commentsTxtBox.Text);

        Black_Book_2010 myBlack_Book_2010 = new Black_Book_2010();
        myBlack_Book_2010.info = data;
        myBlack_Book_2010.ShowDialog();
    }

So from the top, what this does is gets the data from every txtbox on the form, puts it all into one string, then stores all that information into the "data" variable.

Then I click the button which takes me to my second form. I say, save the "data" variable into the "info" variable on the Black_Book_2010 form.

Heres the Black_Book_2010 form:

At the top i declear a variable I wont to ultimately store the data into

string moreData = "";

Then here is the "info" variable that now has the data:

    public string info
    {
        set
        {
            moreData = value;
        }
    }

When the form loads, I wont it to get the "moreData" variable and add it to the listbox:

    private void Black_Book_2010_Load(object sender, EventArgs e)
    {
        data.Items.Add(moreData.ToString());
    }

I also have a button on the Black_Book_2010 form, called Add, which would take me back to my data entry form if I wanted to add more data.

But when I fill in the form and click add, my summary form starts again, it doesnt load the previous data, it just opens a new summary form.

What I need is when every I hit add on my data entry form, it adds that data to the existing summary form and not create a whole new one.

+1  A: 

You are creating a new instance of Black_Book form every time the add button clicked.
So the old data is gone.
Try to use the same instance of the form.

Another solution will be to maintain a list of items outside the black book form and have the form use this list.

Itay
How do I use the same instance of the summary form?
Codie Vincent
A: 

it is because in the add button every time you are creating a new instance of Black_Book_2010.

you can create private property in the main form and initlize this property with a instance of the Black_Book_2010.

and in the add button you can access this property and access info property to assign the data and then use ShowDialog().

Create a private property in mainform

private Black_Book_2010 myBlack_Book_2010 {get;set;} 

i am assuming that add button is in the form called mainform so in the constructor of the mainform do following .

public mainform()
{
      myBlack_Book_2010 = new Black_Book_2010();
}

now go to the add button's code remove line myBlack_Book_2010 = new Black_Book_2010();

assign the info property with the formatted string.

 private void addBtn_Click(object sender, EventArgs e) 
{ 
    string data = string.Format("{0} \t {1} \t {2} \t {3} \t {4} \t {5}", 
        fnameTxtBox.Text,  
        lnameTxtBox.Text,  
        cellNumTxtBox.Text,  
        landLineTxtBox.Text,  
        dobPicker.Text,  
        commentsTxtBox.Text); 

    myBlack_Book_2010.info = data; 
    myBlack_Book_2010.ShowDialog(); 
} 
saurabh
Thanks for your input, but re-read your answer a dozen times, and it doesnt make sense to me..
Codie Vincent
I did exactly what you said to do, but i still have the same problems. It opens a new form, and doesnt add data to the existing summary form
Codie Vincent
+1  A: 

Make first form totally in control of the second form.

Make Add button on second form public, and attach click event handler to it and let it be handled in the first form.

In that event handler, Hide() second dialog, collect the data and show it again when ready.

Make sure that you create Black_box only once, and show/hide it as appropriate.

Daniel Mošmondor