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;
}
}
Related
I have two form: Form1 and Form2. Both of it already have data.
After I scan barcode in Form1, Form1 will be hidden, Form2 will show and somethings.
After Form2 finish, Form1 will show again and Form2 will be hidden.
It's cycle.
I tried with:
///In Form1
Frm2 Form2 = new Frm2();
Form2.show();
this.Hide();
///In Form2
Frm1 Form1 = new Frm1();
Form1.show();
this.Hide();
But I think it make a new form with no data. So, how can I unhide form1 in form2?
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 want in display child form on application load with parent form disable behind child form. How can i achive this in windows application c#?
If you want to show form2 from form1 you can follow this method also
f2 = new Form2();
f2.ShowDialog(this);
private void Form1_Shown(Object sender, EventArgs e)
{
f2 = new Form2();
f2.ShowDialog(this);
}
You can hide or show forms using form1.Show() or form1.Hide().
But before hiding i will tell you to Preserve the instance of Form1
and use it to Show or Hide
Form2 parent= new Form2();
parent.Hide();
Form1 child= new Form1();
child.Show();
or You can minimize parent Form and show child
using this
Form2 parent= new Form2();
Form2.WindowState = FormWindowState.Minimized;
I have total 3 forms (Form1, Form2 and Form3) in my Windows Forms Application.
Form2 is log-in page. When user clicks on sign-in button in Form1, Form2 must be open and if user provides accurate username and password then Form3 is needed to open and to close both Form1 and Form2.
How to code such thing in C#? I am using Microsoft Visual Studio 2012.
So far I have done following procedure:
double click the Form1 to get at the coding window and wrote-
Form2 secondForm = new Form2();
Just outside of the Form Load event
& inside the button, i wrote-
secondForm.Show();
So when I run the solution, Form2 is opened by clicking a button in Form1 (Works Perfectly!). But I have no idea how to close Form1 and Form2 when user enter correct username and password combination in Form2 to open Form3.
Form1 firstForm = new Form1();
firstForm.Close();
isn't closing the form.
Please guide me.
You should add a new Class with a Main() method. (OR) There will be Program.cs in your project.
And, start the application from the Main() method. You can change "Startup object:" property of your project to "<YourApplicationName>.Program".
You Main() method should show Form1 and Form2 as Dialog.
If DialogResult is OK then run the Form3 by Application.Run() method.
DialogResult r;
r = (new Form1().ShowDialog());
if( r == DialogResult.OK )
Application.Run(new Form3());
You cannot close the main form (the one that you used to start the message loop) if you do that will end/close the entire application. What you can do however instead is this:
Form1 code
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var f2 = new Form2();
var res = f2.ShowDialog();
if (res == DialogResult.OK)
{
var f3 = new Form3();
this.Visible = false;
f3.ShowDialog();
this.Close();
}
}
}
Form2 code:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void buttonLogin_Click(object sender, EventArgs e)
{
// if username and password is ok set the dialog result to ok
this.DialogResult = DialogResult.OK;
this.Close();
}
}
I have created a C# application.
Here I have two forms, form1 and form2.
form2 is called from form1.
Later form2 is made hidden.
Now I want to show form2 from form1.
Please give me some idea.
You need to keep reference to Form2 object, and when you want it to be visible, just call frm2.Show() - don't construct the new Form2 object with new Form2() - use the existing one.
// You need to contruct Form2 before calling Show().
Form2 frm2 = new Form2();
// Some handler somewhere
void btnShowForm2_Click(..., ...)
{
frm2.Show();
}
Edit: As Micah pointed out, you will want to hide Form2 instead of closing it:
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
this.Hide();
e.Cancel = true; // this cancels the close event.
}
You will want to use form.hide() when hiding form2 instead of form.close
keep a reference to form2 and call form.show when you want to show it again
take form2 instance variable at class level
example
Public Class Form1
{
Form frm2;
//Show form here
protected void Button1_Clik
{
frm2=new Form2();
frm2.Show();
}
//Even the form is hidden, you may show the same instance /same state of form again
protected void Button2_Click()
{
frm2.Show();
}
}