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.
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();
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.
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.
I have two forms. I need to open a second form with a button. When I open form2 I hide form1. However when I try to show form1 again from form2 with a button it doesn't work. My form1 code is:
Form2 form2 = new Form2();
form2.ShowDialog();
Inside form2 code:
Form1.ActiveForm.ShowDialog();
or
Form1.ActiveForm.Show();
or
form1.show(); (form1 doesn't exist in the current context)
doesn't work.
I do not want to open a new form
Form1 form1 = new Form1();
form1.ShowDialog();
I want show the form which I hided before.
Alternatively I can minimize it to taskbar
this.WindowState = FormWindowState.Minimized;
and maximize it from form2 again.
Form2.ActiveForm.WindowState = FormWindowState.Maximized;
however the way I am trying is again doesn't work.
What is wrong with these ways?
You could try (on Form1 button click)
Hide();
Form2 form2 = new Form2();
form2.ShowDialog();
form2 = null;
Show();
or (it should work)
Hide();
using (Form2 form2 = new Form2())
form2.ShowDialog();
Show();
Preserve the instance of Form1 and use it to Show or Hide.
You can access Form1 from Form2 throught the Owner property if you show form2 like this:
form2.ShowDialog( form1 )
or like this:
form2.Show( form1 )
Notice this way you are not forced to use ShowDialog cause hide and show logic can be moved inside Form2
This method is the one that I find works the best for me
Primary Form
Form2 form2 = new Form2(this);
Secondary Form
private Form Form1
public Form2(Form Form1)
{
InitializeComponent();
this.Form1 = Form1;
Form1.Hide();
}
Later on when closing
private void btnClose_Click(object sender, EventArgs e)
{
Form1.Show();
this.Close();
}
FormCollection frm = Application.OpenForms;
foreach(Form f in frm)
{
if(f.Name=="yourformname")
{
f.Show();
this.Close();
this.Dispose();
return;
}
}
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();