Saska
Saska

Reputation: 1021

Transferring variables between forms in C#

I have 2 forms - Form1 and Form2.

Form1:

    public partial class Form1 : Form
    {
        public void Drawnewmap(bool suzey) {
            bool dsuzey=suzey;
            if (dsuzey==true) textBox1.Text = "1" ;

        }

        public Form1()
        {
            InitializeComponent();
        }
     }

Form2:

private void button1_Click(object sender, EventArgs e)
{
    Form1 f1 = new Form1();
    bool m = false;
    if (checkBox1.Checked == true) m = true;
    f1.Drawnewmap(m);
    this.Close();
}

but when I click, nothing happens. TextBox is empty. Why?

Upvotes: 0

Views: 476

Answers (5)

melih zenciroglu
melih zenciroglu

Reputation: 43

If you want to reach your first active form via your second form, you should define the first form in your second form like:

Form1 frm = (Form1)Application.OpenForms["Form1"];

instead of

Form1 f1 = new Form1();

Since you will create a new form if you use "Form1 f1 = new Form1();", you will not reach the active form.

Upvotes: 2

John Alexiou
John Alexiou

Reputation: 29244

I prefer to solve this problem using properties in the forms that hold a state

== Form 1 ==

public partial class Form1 : Form
{
    bool is_dsuzey;

    public Form1()
    {
        InitializeComponent();
    }

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);

        if (is_dsuzey) textBox1.Text = "1";
    }
    public bool IsDsuzey { get { return is_dsuzey; } set { is_dsuzey = value; } }

}

== Form 2 ==

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Form1 f1 = new Form1();
        f1.IsDsuzey = checkBox1.Checked;
        f1.Show();
    }
}

I would caution to put a this.Close(); statement in Form2 if it is the main form of the application because it will close out the entire application. If Application.Run() calls some other form, then there is no problem.

Upvotes: 1

Piotr Styczyński
Piotr Styczyński

Reputation: 492

Because You create new Form1 instance. And I think that is bad idea to call something from parent window in child Window. But you can do smoething like that: When you create Form2, pass reference to current Form1 object

Form2 form = new Form2(this);

In Form2 constructor save Form1 reference in private variable.

private Form1 myform1;

Then your code need to look like this:

private void button1_Click(object sender, EventArgs e)
{
    bool m = false;
    if (checkBox1.Checked == true) m = true;
    myform1.Drawnewmap(m);
    this.Close();
}

Upvotes: 1

Rowland Shaw
Rowland Shaw

Reputation: 38130

You never actually show the form, perhaps you should have something more like:

I have 2 forms - Form1 and Form2.

private void button1_Click(object sender, EventArgs e)
{
    Form1 f1 = new Form1();
    bool m = false;
    if (checkBox1.Checked == true) m = true;
    f1.Drawnewmap(m);
    f1.Show(); // You're missing this call
    this.Close();
}

Upvotes: 2

Jalal Said
Jalal Said

Reputation: 16162

You didn't show the form1 you created.

private void button1_Click(object sender, EventArgs e)
{
    Form1 f1 = new Form1();
    bool m = false;
    if (checkBox1.Checked == true) m = true;
    f1.Drawnewmap(m);

    f1.Show();//or ShowDialog()...
    this.Close();
}

Note that if the Form2 is the main form "you can check about that at the Main", if you close it, it will exit the whole application, so if that is the case either make the form1 is the main form, or call this this.Hide(); instead of this.Close();

Upvotes: 1

Related Questions