Reputation: 187
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.
Upvotes: 0
Views: 825
Reputation: 19956
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.
Upvotes: 1
Reputation: 18286
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.
Upvotes: 1