I have two forms Form1 and Form2
I am opening Form2 from Form1 on button_Click
Form2 obj2 = new Form2();
this.Visible = false;
obj2.Show();
then I want to get back Form1 Visible (on disposing Form2) in same states of Controls on which I left.....
Your Form2 doesn't know anything about Form1. It will need a reference to it (you can do that by adding a Form type property on Form2 and assign Form1 to it after construction):
//In Form2
public Form RefToForm1 { get; set;}
//In Form1
Form2 obj2 = new Form2();
obj2.RefToForm1 = this;
this.Visible = false;
obj2.Show();
//In Form2, where you need to show Form1:
this.RefToForm1.Show();
Oded's answer will work perfectly well, another option with the same result will be to expose public event in Form2 called for example "AfterClose", invoke it when Form2 is disposing and have Form1 add event handler where it show itself. Let me know if you're interested and I'll give some sample code.
Related
I've hidden a form Form1 and have shown a new form Form2 using the following code:
form_Form2 f = new form_Form2();
this.Hide();
f.Show();
where this == Form1. How do I then make Form1 visible again from Form2? I do not want to create a new instance of Form1, I just want to make it show up again because the this.Hide() function keeps the data on the form instead of fully closing it.
Form2 will need a reference to Form1
form_Form2 f = new form_Form2();
f.OtherForm = this;
this.Hide();
f.Show();
You will have to add OtherForm property. Then later in Form 2
OtherForm.Show();
I have Form1 and Form2 simultaneously active, Form2 has a TabControl and I want button click event on Form1 to change 'Enable' property of tabControl on Form2
TabControl on Form2 is set to tabControl1.enabled = false; and Form1 acts as a login form for Form2, so I need a Login button on Form1 to enable 'tabControl1' on Form2
I can access the property by setting private System.Windows.Forms.TabControl tabControl1; to 'Public', still, using the following on button click event on Form1 doesn't do anything.
Form2 formnew2 = new Form2();
formnew2.tabControl1.Enabled = true;
Can someone please provide a simple example to help me understand or link to a previously answered question
It seems you are using the wrong reference of Form2. You probably have an open Form2, but you mistakenly create a new instance of Form2 again here in your Form1. So changing the new instance properties has no effect on previously opened instance.
You should pass an instance of Form2 to Form1 and use it.
Also you can find running instance of Form2 using Application.OpenForms
In Form1 have a reference to Form2.
In Form2 wrap the TabControl's Enable property in a public method and call the it from Form1
In Form1:
...
Form2 form2;
public Form1()
{
// initialize and show form2
this.form2 = new Form2();
this.form2.Show();
}
...
in Form2:
...
public void EnableTabControl()
{
this.tabControl1.Enabled = true;
}
...
then in Form1 when the button is clicked:
private void btnLogin_Click(object sender, EventArgs e)
{
// verify that it was initialized
if (form2 != null)
{
this.form2.EnableTabControl();
}
}
This question already has answers here:
Communicate between two windows forms in C#
(12 answers)
Closed 5 years ago.
I have the following:
I have a GUI with two forms. Form2 is opened via Form1 by a button. Both forms have Textboxes and I want them to communicate with each other (one form can grab the entries of textboxes from another form). What I did now in Form2 is:
private Form1 m_form = null;
public Form2(Form1 f)
{
InitializeComponent();
m_form = f;
}
and for the Textboxes functions like:
public String getLocation()
{
return LocationBox.Text;
}
That works fine. So Form2 can read the entries from Form1. Now I wanted Form1 to read the textbox entries from Form2 and tried the same thing (which is probably wrong):
private Form2 m_form2 = null;
public Form1(Form2 f2)
{
InitializeComponent();
m_form2 = f2;
}
and then some functions like the one I've posted but everytime I want to read a textbox with Form1 which is in Form2 I get "null" and "NullReference" Exception. Where is the error?
EDIT: Ok I solved a part. Adding
Form2 m_form2 = new Form2(this);
m_form2.Show();
solves the problem with the NullReferenceException. Without the line
m_form2.Show()
it passes empty strings but now everytime I hit a button the form2 appears.
I think the problem is that you never call the consturctor public Form1(Form2 f2) . When you open Form2 from the first one you have to save that instance within your instance of Form1:
void createForm2() {
Form2 frm2 = new Form2(this);
this.m_form2 = frm2;
}
try this in Form1:
this.TextBoxName.Text = m_Form2.getLocation();
or in Form2:
m_form.TextBoxName.Text = this.getLocation();
Note
in Form1 when call form 2:
m_Form2 = new Form2(this);
//do anything with your code
For sending values between two forms, you may
1-> Send the values in the constructor of the second form. You may create a paramterized constructor and send the values when you initialize the form as :
Form1 obj = new Form1(Object);
2-> You may take a reference in to your first form in the second form.
In second form,
public Form1 objForm1;
and in First Form,
Form2 objForm2=new Form2();
Form2.objForm1=this;
and then you can use Form2's objForm1 to refer to Form1's textbox or any control.
Consider you want to send all values from Form1 to Form2
In your second form you must have a variable of type Form1 that refers to the prev form. So in second form,
public Form1 objForm1;
and then you need to send the current instance of the Form1 to Form2 as
Form2 objForm2=new Form2();
Form2.objForm1=this;
i.e. the objForm1 that you created in Form2 refers to this instance of Form1.
Now in Form2 you may use any of Form1's control or variable as,
Form1.TextBox1 or Form1.Variable
I have three windows forms, I have a button in form1 and a button in the form2,
if the button on form1 has been clicked, then it shows form2:
form2 f2 = new form2();
f2.show();
The form1 is still in the background and visible. There is one more button on form2. When that button is clicked the same event happen that form2 is running in background and form3 shows up. When form3 is shown I want to hide form1.
How can I do that? Is there any property that can help me to know which form is running in behind?
You could use the Application.OpenForms collection to iterate on your open forms.
Then checking for the name of Form1 you could try to Hide it
foreach(Form f in Application.OpenForms)
{
if(f.Name == "Form1")
{
f.Hide();
break;
}
}
You can check if your form has Focus
bool foc = formX.Focused;
You can use: Application.OpenForms.
Form2 should fire a custom event when it opens Form3. Form1 shoudl subscribe to that custom event when it shows Form2, and then Form1 can hide itself in that event handler.
public class Form2
{
public event Action Form3Opened;
public void Bar
{
Form3 other = new Form3();
Form3Opened();
other.Show();
}
}
public class Form1
{
public void Foo()
{
Form2 other = new Form2();
other.Form3Opened += () => Hide();
other.Show();
}
}
Yes you can use Application.OpenForms property to find a collection of all opened forms of your application.
The other solution is could be passing parent form parameter like the caller form.
For example: for Form2 the parent is Form1, for Form3 the parent id Form2
i have 2 forms
Form1 the main form
Form2 the subform
When i open form2 i want to hide form1 <- the main
And when i close form2 i want to show form1
I tried
Form1 mform = new Form1();
mform.Show();
But its open new form not the hidden form
Found solution
In form 2
Form Main;
public Form2(Form MainForm)
{
Main = MainForm;
InitializeComponent();
}
in form 1
Form2 SubForm = new Form2(this);
SubForm.Show();
this.Hide();
So i can show the hidden form1 from form2 by Main.Show();
you have to keep a reference to the Main form in the Form2 instead of creating a new From.
in your Form2 you should have a property to keep a reference to the Form1 something like the blow:
public Form MainForm { get; set; }
Just pass the reference of main form to the Form1 and keep it to access it later.
Form1 mform = new Form1(MainForm mform);
mform.Show();
When you open a new subform FORM2, you just have to hide the main form using form1.Hide() and show the new one Form2.Show().
When you close the subform, you just call the form1.Show().
TO implement this, u gotto have reference of main form.
there is no need for keeping the references. Use the following code in the child form
if (this.ParentForm != null)
this.ParentForm.Show();