Answer the question
In order to leave comments, you need to log in
How to correctly pass data between forms?
How to correctly pass data between forms.
There is form 1 - it has a table, when you click on the "add" button, a second form appears in which the necessary information is entered, processed, etc. After pressing the button "ok" in the 2nd form - the line is added to the table.
1. Is it permissible to do it through a parent?
In the first form:
addForm addFormWindow = new addForm(){ Owner = this };
addFormWindow.ShowDialog();
public void addFormBtn_Click(object sender, EventArgs e)
if (this.Owner != null)
{
main f = (main)this.Owner;
f.dataView.Rows.Add(codeUser.Text, nameUser.Text, surnameUser.Text, fatherUser.Text);
}
Answer the question
In order to leave comments, you need to log in
1. Will work
but there may be problems with the secondary use of the add form, for example, if you want form 3 and there is already dataView2 on it, then it will not work
I advise you to read in the direction of Events , in a nutshell, an event is created on form 2 with parameters that need to be passed to the first form, the first form subscribes to it and does whatever it likes with these parameters, the second form calls event when necessary and passes the parameters. Such an implementation may make sense if you need to select several entries or options in a dialog, for example.
Create parameters on form 2 (google for "C# get; set;"), respectively, they are filled in on the second form
. ShowDialog is called on the first form and, depending on the result, these parameters are simply read from the form.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question