Reputation: 1
I want to make Form1.Visable=true and Form2.Visable=false FROM Form2
What i have tried:
Form1 FRM1 = New Form1;
FRM1.Show();
What I want: I want to make form1 visible = true from form2 code without creating a new form1
I can do it with VB.NET but i can't do it with C#
Upvotes: 0
Views: 142
Reputation: 378
Pass a reference of the form you want to close to the form you want to be able to close it from.
Explicitly passing reference:
public class Form1
{
public Form1()
{
new Form2(this).Show();
}
}
public class Form2
{
Form1 form1;
public Form2(Form1 form1)
{
this.form1 = form1;
form1.Hide();
}
}
Passing the reference by setting Form2's owner:
public class Form1
{
public Form1()
{
new Form2().Show(this); // show Form2 with Form1 as it's owner
}
}
public class Form2
{
public Form2()
{
this.Owner.Hide(); // hide this form's owner, in this case Form1
}
}
Some things you might want to read about:
the 'this' keyword | Value vs Reference
Upvotes: 1