L
L
littleguga2015-05-14 04:10:25
C++ / C#
littleguga, 2015-05-14 04:10:25

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();

In the second:
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);
  }

2. If you use delegates:
2.1 A delegate is a reference to a method that allows you to call this method by referring to this reference, right?
2.2 Can you please advise some lesson, because, unfortunately, I did not understand anything from professorweb.ru and the msdn documentation.
Thanks in advance for your reply!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaly Pukhov, 2015-05-14
@littleguga

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

2. It is possible, but I do not think that it is necessary,
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.

Option 3 (easier)
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 question

Ask a Question

731 491 924 answers to any question