Show main hidden form from subfrom - c#

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();

Related

Showing a hidden form (using this.Hide()) from another form

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();

Return to the main Form, and close the others, from a form that has other forms opened (C#)?

My problem it´s that I have Form1, that opens Form2, and then it opens another Form, I want to make a button on the last Form that returns to the main form and closes all the other.
Here´s how I programmed the Forms openings:
Form2 popup= new Form2();
this.Hide();
popup.ShowDialog();
This.Show();
And I did the same with the other Form, I tried doing something like this:
Form1 main= new Form1();
this.Close();
main.Show();
But it leaves all the other forms opened.
Thanks!
You can get a list of all open forms in your application using Application.OpenForms. Then you can loop through them and close all the ones you don't want. Something like this:
foreach (Form f in Application.OpenForms) {
if (f.Name != this.Name)
f.Close();
}
Form1 main= new Form1();
this.Close();
main.Show();
I have no means to test the code right now, but you can probably close the current form with all the others in the loop, so the code can be reduced to:
foreach (Form f in Application.OpenForms) {
f.Close();
}
Form1 main= new Form1();
main.Show();
You can pass a reference of the MainForm through the constructor of every form you instantiate.
Form2 popup = new Form2(this);
this.Hide();
popup.ShowDialog();
And then keep a global reference of the MainForm on your Form2
private Form _mainForm;
// constructor of Form2
public Form2(Form mainForm)
{
_mainForm = mainForm;
}
then with that reference you can call it anywhere. Also you can pass it to your third form
this.Close();
_mainForm.Show();
The benefits of doing it this way is that you do not lose any pre-defined settings on the main form because you're not creating a new instance, simply displaying it
Try this
Form1 f1 = new Form1();
f1.Show();
this.Hide();
this.Hide() to the form you want to hide.

How to change 'Enable' property of tabcontrol from Form1 on Form2, C#, Visual studio

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();
}
}

is there any property for knowing which form is running in background

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

get back hidden FORM from another FORM

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.

Categories