Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <ol> <li><p>Change the access modifier In Form2 Set the access modifier for the control / field public Anywhere Form1.</p> <pre><code>Form2 f = new Form2(); f.ShowDialog(); this.textBox1.Text = f.textBox1.Text; </code></pre> <ul> <li>The fastest to implement and convenient way</li> <li>Contrary to all the basics of OOP</li> <li>The transmission of only a later form of an earlier</li> <li>Form f only shown using ShowDialog (), ie in the first form control returns only the second closing. Avoid this by maintaining a link to the second form in the first form</li> </ul></li> <li><p>Use of public property / method. The method is very similar to the first In Form2 class defines a property (or method).</p> <p>In Form2 class defines a property (or method)</p> <pre><code>public string Data { get { return textBox1.Text; } } </code></pre> <p>Anywhere in Form1</p> <pre><code>Form2 f = new Form2(); f.ShowDialog(); this.textBox1.Text = f.Data; </code></pre> <ul> <li>Does not contradict all the basics of OOP</li> <li>Cons same</li> </ul></li> <li><p>Data transfer to the constructor Form2. Change the constructor Form2</p> <pre><code>public Form2(string data) { InitializeComponent(); this.data = data; } string data; </code></pre> <p>And create a shape anywhere Form1 as follows:</p> <pre><code>Form2 f = new Form2(this.textBox1.Text); f.ShowDialog(); </code></pre></li> <li><p>To send a link to constructor. Change the constructor Form2.</p> <pre><code>public Form2(Form1 f1) { InitializeComponent(); string s = f1.textBox1.Text; } </code></pre> <p>And create a shape anywhere Form1 so that pass it a reference to the first form</p> <pre><code>Form2 f = new Form2(this); f.ShowDialog(); </code></pre> <ul> <li>Access to all open fields / functions of the first form</li> <li>Data transfer is possible in both directions</li> <li>Violates the PLO</li> </ul></li> <li><p>Use the property 'parent'. When you create a second form sets the owner</p> <pre><code>Form2 f = new Form2(); f.Owner = this; f.ShowDialog(); </code></pre> <p>In the second, we determine the owner</p> <pre><code>Form1 main = this.Owner as Form1; if(main != null) { string s = main.textBox1.Text; main.textBox1.Text = "OK"; } </code></pre> <ul> <li>Access to all open fields / functions of the first form</li> <li>Data transfer is possible in both directions</li> <li>Do not violate the PLO</li> </ul></li> <li><p>Use a separate class. Create a separate class, better static, mainly namespace, ie for example in the Program.cs file</p> <pre><code>static class Data { public static string Value { get; set; } } </code></pre> <p>His public properties / methods are available from any form.</p> <pre><code>Data.Value = "111"; </code></pre> <ul> <li>The most convenient way when data is actively used in several forms.</li> </ul></li> <li><p>Transfer method in the constructor. Create delegate</p> <pre><code>public delegate void MyDelegate(string data); </code></pre> <p>In Form1, create a method that will handle the received data</p> <pre><code>void func(string param) { //Process } </code></pre> <p>Create a second form as follows:</p> <pre><code>Form2 f = new Form2(new MyDelegate(GetData)); f.ShowDialog(); </code></pre> <p>Change constructor of second form to accept a delegate</p> <pre><code>MyDelegate d; public Form2(MyDelegate sender) { InitializeComponent(); d= sender; } </code></pre> <p>And at any moment send data</p> <pre><code>d(textBox1.Text); </code></pre></li> <li><p>Creating a separate class with a delegate. In main namespace create separate class</p> <pre><code>public static class Data { public delegate void MyEvent(string data); public static MyEvent EventHandler; } </code></pre> <p>In the first form, add a handler</p> <pre><code>void func(string param) { MessageBox.Show(param); } </code></pre> <p>and initialize the EventHandler</p> <pre><code>Data.EventHandler = new Data.MyEvent(func); </code></pre> <p>Create a second form in the usual way and then call it from</p> <pre><code>Data.EventHandler(textBox1.Text); </code></pre> <ul> <li>The most flexible way to transfer data</li> <li>Difficult to implement and understand</li> </ul></li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload