1

Topic: c# transmission of events and the data between forms

I how many study , but anywhere specific useful lessons did not see, that showed  less difficult application and useful
Decided to study on slices and here it was thought, and as other forms become, for example at pushing the button of adjustments
Found the answer, made, works
There was a question: how there given to transfer? And then - how to transfer not only the data from the first in the second but also that the second form knew that the data exchanged
For this purpose I made on the first form  and a label with digits
And in the second form made
Looked how to transfer the data from the first form and like as though it and it is transferred or the code without errors is more exact, but the data is not transferred
And the more so event (I am more exact than it yet ) is not transferred
Looked, found, but there understood nothing with delegates, there is no anywhere a picture that it has been signed, in what place what words for what
Question the first
Why on the second form  does not show 2 divisions?
Question of the second
How to write the delegate or that there it is necessary, that at driving  on the first form changed  on the second
Question the third
How to make too most that was  on the second form, but me the indicator on the first?
Question the fourth
How it to make most correctly to untie as though forms from each other (it complicates the code?)
The code of principal form MainForm.cs

 namespace form2form
{
public partial class MainForm: Form
{
public SecondForm seconform;
public int trackbar;
public MainForm ()
{
InitializeComponent ();
}
public void button1_Click (object sender, EventArgs e)
{
seconform = new SecondForm (this);
seconform. Show ();
trackbar = 2;
}
public void trackBar1_Scroll (object sender, EventArgs e)
{
trackbar = trackBar1.Value;
label1.Text = trackbar. ToString ();
}
}
}

The code of second form SecondForm.cs

namespace form2form
{
public partial class SecondForm: Form
{
public SecondForm (MainForm frm)
{
InitializeComponent ();
progressBar1.Value = frm.trackbar;
}
}
}

Actually
seconform. Show ();
trackbar = 2;
Here for debugging I appropriated variable value, but in the second form it is not displayed:lamer::mad:
Adding from 9/12/2017 05:25:
for others (type a course of my thoughts)
Now once again  with
seconform. Show ();
trackbar = 2;
And here me reached that at first we creates object
seconform = new SecondForm (this);
Also we transfer in the designer the main form on this
And only then we change variable value trackbar in the first form, and the second knows nothing about changes in the first
It was necessary to change places of a line and the indicator became :super::cool:
Now it is necessary to make somehow so that the second form knew about  a variable trackbar:rolleyes:

2

Re: c# transmission of events and the data between forms

kalobyte
Now it is necessary to make somehow so that the second form knew about  a variable trackbar
Such notifications are implemented by events, and the type int does not implement any events. In this case most easier to manage INPC:
[code=csharp] public class IntChangesWrapper: INotifyPropertyChanged
{
int _value;
public int Value
{
get => _value;
set
{
if (_value == value) return;
_value = value;
OnPropertyChanged (nameof (Value));
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged (string propertyName)
{
PropertyChanged?.Invoke (this, new PropertyChangedEventArgs (propertyName));
}
}
public partial class Form1: Form
{
readonly IntChangesWrapper _intChangesWrapper = new IntChangesWrapper {Value = 2};
public Form1 ()
{
InitializeComponent ();
_intChangesWrapper. PropertyChanged + = (s, e) =>
{
if (string. IsNullOrEmpty (e. PropertyName) || "Value".Equals (e. PropertyName))
label1.Text = _intChangesWrapper. Value. ToString (CultureInfo. InvariantCulture);
};
label2.DataBindings. Add ("Text", _intChangesWrapper, "Value");
}
void Button1Click (object sender, EventArgs ea)
{
new Form2 (_intChangesWrapper).ShowDialog (this);
}
}
public partial class Form2: Form
{
readonly IntChangesWrapper _intChangesWrapper;
public Form2 (IntChangesWrapper intChangesWrapper)
{
InitializeComponent ();
_intChangesWrapper = intChangesWrapper;
}
private void Button1Click (object sender, EventArgs e)
{
_intChangesWrapper. Value ++;
}
}
[/code] - the class-vrapper at property change initiates event PropertyChanged, subscribers it is event process explicitly - as in a case with label1, or is implicit - as in a case label2 where on PropertyChanged the built in mechanism data bindings implicitly subscribes.
P.S. And for date transmission through pablik-fields to you can beat off hands.

3

Re: c# transmission of events and the data between forms

Well I it too saw and thought that in properties  it is possible to specify any  the data in the form of a variable and it there somehow itself will work
Only I from this heap of words understood nothing:lamer:
For my example as this code will look?

4

Re: c# transmission of events and the data between forms

kalobyte
For my example as this code will look?
The code for this example is resulted.

5

Re: c# transmission of events and the data between forms

All the same I understand nothing
That each line and all these words of type does
_intChangesWrapper. PropertyChanged + = (s, e) =>
new Form2 (_intChangesWrapper).ShowDialog (this); what it for record such? It confuses me
label2.DataBindings. Add ("Text", _intChangesWrapper, "Value");
Here only one label is anchored
And if I want to anchor still, I should add in the second line for other component or once again completely to copy
_intChangesWrapper. PropertyChanged + = (s, e) =>
30
{
31
if (string. IsNullOrEmpty (e. PropertyName) || "Value".Equals (e. PropertyName))
32
label1.Text = _intChangesWrapper. Value. ToString (CultureInfo. InvariantCulture);
33
};
34
label2.DataBindings. Add ("Text", _intChangesWrapper, "Value");
35
}
?
IntChangesWrapper _intChangesWrapper
2 identical words again confuse all
I noted, what very few people can inform a material, not clearly only who it informed you?
Your code looks as the circuit of the unit from a TV set
http://radiowiki.ru/images/7/7b/0_ee289 … e_orig.png

6

Re: c# transmission of events and the data between forms

kalobyte
That does the code ash of mind ...

 class IntChangesWrapper 

it is object which is able to notify on change of the state (in this case variable values Value) all interested. The notification message happens through event under title PropertyChanged.
new Form2 (_intChangesWrapper).ShowDialog (this);
The first form (Form1) in the code is principal and creates (and shows) the form 2 (Form 2) each time at button click Button1.
That is at each pushing the new form (new) will be created and at once is shown (ShowDialog).

 _intChangesWrapper. PropertyChanged + = (s, e) => 

it is a subscription to event PropertyChanged. Normally after a sign + = there should be a function name which will react to the given event. But in this case ash of mind applied an anonymous function which code follows a sign =>

 {
if (string. IsNullOrEmpty (e. PropertyName) || "Value".Equals (e. PropertyName))
label1.Text = _intChangesWrapper. Value. ToString (CultureInfo. InvariantCulture)
};

the Compiler at program creation this code  in ordinary function with the auto-generated name of type "Function 1", and after a sign + = delivers this generated name.
It on the code. Now an example I will write.

7

Re: c# transmission of events and the data between forms

kalobyte
  I noted, what very few people can inform a material, not clearly only who it informed you?
MSDN

8

Re: c# transmission of events and the data between forms

kalobyte
Most simple (but not the most elegant) the decision of your problem with minimum change of the code:
1. To change access modifier for progressBar1 on SecondForm on public.
2. In the first form (MainForm) to change the code trackBar1_Scroll for direct change of value progressBar1 from SecondForm.

 public void trackBar1_Scroll (object sender, EventArgs e)
{
seconform.progressBar1.Value = trackBar1.Value;
}

it is clumsy, of course, but the purpose is reached.

9

Re: c# transmission of events and the data between forms

Arahorn
That is at each pushing the new form (new) will be created and at once is shown (ShowDialog).
But after all in my example this code looks so
seconform = new SecondForm (this);
seconform. Show ();
I here __ as as happens since it is normal and it is necessary to write and to all  so write
In circuits certain rules of drawing too are accepted and some too draw as want that at first sight not  that there is drawn and where is connected
Applied an anonymous function which code follows a sign =>
I hate them:mad:
It is possible without them?
ash of mind
MSDN
It  80 level, there is written nothing, how as why works, it is not intended for training, only the reference manual
1. To change access modifier for progressBar1 on SecondForm on public.
And how it to change? Only in a file of the designer
private void InitializeComponent ()
{
this.progressBar1 = new System. Windows. Forms. ProgressBar ();
this. SuspendLayout ();
It would not be desirable to change for all function, especially the code is generated by studio and I do not want there
seconform.progressBar1.Value = trackBar1.Value;
So I tried to do, but after secondform I do not see in the help
Why all so is difficult?:mad:

10

Re: c# transmission of events and the data between forms

kalobyte :
It would not be desirable to change for all function, especially the code is generated by studio and I do not want there

to Add public accessor for progressBar1 in not generated piece of class SecondForm.  property,  an ordinary function.

seconform.progressBar1.Value = trackBar1.Value;
So I tried to do, but after secondform I do not see in the help

,  does  forms private, for encapsulation.

Why all so is difficult?:mad:

because it is difficult to write, but simply to read (easier). In a case "it becomes simple to write" in a week the code difficult to read, and in a month absolutely not clearly that is written.

11

Re: c# transmission of events and the data between forms

kalobyte
I here __ as as happens
Everyone sees moderately the qualification. I here perfectly see that happens in the call of the form resulted by me, and in your call. In my case at level IL method ShowDialog will be caused through call, and in yours - through callvirt that , plus layout on a stack to nobody of the necessary reference. In this case it is any role does not play, but in any other case can and play.
It  80 level, there is written nothing, how as why works, it is not intended for training, only the reference manual
All there is written, it is necessary not to be lazy only to search.
Example  on a subject:
1. https://www.google.ru/search?q=C%23+implemeting+events
2. The first link in output: https://msdn.microsoft.com/en-us/library/5z57dxz2 (v=vs.90).aspx

12

Re: c# transmission of events and the data between forms

ash of mind
In my case at level IL method ShowDialog will be caused through call, and in yours - through callvirt
To me it generally about what does not speak
All there is written, it is necessary not to be lazy only to search.
At me 5 tabs to one browser openly only on date transmission between forms, in other browser 2 videos are opened on the same subject
I looked , there at the sad code, I do not see dynamics of development of the code and that turns out visually
All the same that to me show a double-Dutch
Therefore I watch commercials only training where write a piece, launch also I see that was specific made this piece in comparison with a state without this piece
For this reason I in the question stuck a screenshot
deadok
To Add public accessor for progressBar1 in not generated piece of class SecondForm.
I added also it earned

public partial class MainForm: Form
{
public SecondForm seconform;
public int trackbar;
public MainForm ()
{
InitializeComponent ();
}
public void button1_Click (object sender, EventArgs e)
{
seconform = new SecondForm (this);
seconform. Show ();
}
public void trackBar1_Scroll (object sender, EventArgs e)
{
trackbar = trackBar1.Value;
label1.Text = trackbar. ToString ();
seconform. ProgressBar = trackbar;
}
}

And the second form

public partial class SecondForm: Form
{
public int ProgressBar
{
set {progressBar1.Value = value;}
}
public SecondForm (MainForm frm)
{
InitializeComponent ();
ProgressBar = frm.trackbar;
}
}

But so it is wrong from the point of view of correctness of programming?
To me it is now clear

13

Re: c# transmission of events and the data between forms

kalobyte :
But so it is wrong from the point of view of correctness of programming?

At  2 variants to generate the code for :
-To do its private and then you can add public
-To do its public and then you cannot hide it if you do not need to show outside giblets
-To ask at creation what it to do
The first variant - the most convenient (in the third variant there is a question - how to edit)

14

Re: c# transmission of events and the data between forms

kalobyte
It  80 level
It explains why at you it turns out nothing