Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>Solution 1:</strong> you can pass your <code>int</code> variable to New Form <code>Constructor</code> and assign that value in the <code>NewForm</code> constructor.</p> <p>Try This: </p> <p><strong>Program.cs</strong> </p> <pre><code> static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1(0)); } </code></pre> <p><strong>Form1 Code:</strong> </p> <pre><code> int no = 10; public Form1(int no) { this.no=no; InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Form2 form2 = new Form2(no); this.Hide(); form2.ShowDialog(); } </code></pre> <p><strong>Form 2: Code</strong> </p> <pre><code> int no; public Form2(int no) { this.no = no; InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { no = 20; Form1 form1 = new Form1(no); this.Hide(); form1.Show(); } </code></pre> <p><strong>Solution 2:</strong> you can take <code>public</code> <code>static</code> varible in <code>Form1</code> and access that variable in chid forms using their class names.</p> <p>Try This:</p> <p><strong>Form1 Code:</strong> </p> <pre><code> public static int no = 10; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Form2 form2 = new Form2(); this.Hide(); form2.ShowDialog(); } </code></pre> <p><strong>Form 2: Code</strong> </p> <pre><code> public Form2() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Form1.no = 20; //access Form1 int variable Form1 form1 = new Form1(no); this.Hide(); form1.Show(); } </code></pre>
 

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