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

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

Related

I cant dispose the first form when calling second form using c#

how can i dispose the first form when calling second form using c#, Here is my code, :
Form2 f2 = new Form2();
f2.Show();
this.Dispose();
When you run the command new Form2() you are creating a new Form instance from Form1 as a child element. Therefore, you cannot dispose the parent, Form1 (which is this in your code).
Application will close if you dispose the main form.
Try this code
this.Visible = false;
Form2 f2 = new Form2();
f2.ShowDialog();
this.Visible = true;
This will just hide the main form as long as Form2 is open.
Why you are trying to dispose the main form, Instead You can simply hide the first form:
Form2 f2 = new Form2();
f2.Show(); //It shows the new form(Second Form)
this.Hide(); //It hides the current form
It will hide the Main form and shows the Second form.

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.

Show main hidden form from subfrom

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

How to load form

In c# I'm working on a project and I try to make the program go to another form it does but its blank
Code:
Form form2 = new Form();
form2.Show();
You're making an instance of Form class which is the base class and should be blank.
Define your Form object like this instead:
var form2 = new Form2();
form2.Show();
Or
Form form2 = new Form2();
form2.Show();
You have to create a object of the form 2 like this
Form2 frm2 = new Form2();
frm2.show();
hope this helps
you are creating a new instance of the base class Form from which your application forms are deriving.
saying you have two forms in your application, Form1 and Form2, to show Form2 from Form1 do this:
var myForm2 = new Form2();
myForm2.Show();
A new Form instance is just blank. You have to create an instance of your own customized Form (Form2?) and show.
May be like below.
// Create and display a modless Form2.
Form2 myForm = new Form2();
myForm.Show();
Look at here for more Form.Show() examples.

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