Open/show other Form with [X] Button Click C# - c#

I have 2 Forms, Startform is a Login Form (Form1) and a Form that opens after Login, Form2.
when Login is successful the form2 shows.
f2.Show(); //form2 show
this.Hide(); //login(f1) hide
This works.
Now i want that if i press the red X Button (right top) that Form2 close and the Login page shows again.
I tried this in Form2:
Form1 f1 = new Form1();
....
...
private void Main_FormClosing(object sender, FormClosingEventArgs e)
{
f1.show();
}
But this just close the Form2 and not open the From1 and the Program is still running in the Background

In my example Form1 does role of your LoginForm
problem is what you are killing a Form2 which actually have created instance of Form1 (here your login form). so when instance of Form2 will be gone with it all its local instance will be gone too.
you can do one thing, while creating an object of Form2 from you Form1 pass object of Form1 to Form2.
so you will not required to create an instace of Form1 in Form2 and while closing it you can simply call Form1's show method.
like below.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//passing current class' object
Form2 form2 = new Form2(this);
form2.Show();
this.Hide();
}
}
and Form 2 :
public partial class Form2 : Form
{
Form1 m_form1;
public Form2(Form1 form1)
{
InitializeComponent();
m_form1 = form1;
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
m_form1.Show();
}
}

Related

how to hide form1 by clicking button in form2

I designed a form for login that named as form1, and have 2 more form form2,form3
form2 items showing in panel from form1
and what I want to do
when I click the button in panel ( the item from form2 ) want to show form2 and hide form1 but the code isnt working
private void button1_Click(object sender, EventArgs e)
{
Form1 frm1 = new Form1();
Form3 frm3 = new Form3();
frm1.Hide();
frm3.Show();
};
form3 is opening but form1 isnt hiding
Its not hiding because you created a new instance for form1 which is already instantiated.
You must call the Hide() method on the same instance used to call the Show() method.
If you added this code inside form1 class ,then change it like this
private Form1 frm1
public Form2()
{
frm1 = new Form1()
}
private void button_show_form1_Click(object sender, EventArgs e)
{
frm1.Show();
};
private void button1_Click(object sender, EventArgs e)
{
Form3 frm3 = new Form3();
frm3.Show();
frm1.Hide();
};
You creating both forms in your codesnipped. Form1 is not the form you want to close, i think. frm1 is only another instance of Form1, but not the openend instance von Form1. You must have anywhere another instance of Form1. You must use the right referenz to the right instance.
It is important to know that WinForms create an instance of the startup form. In our case the startup form would be Form1. So, when you say
Form1 frm1 = new Form1();
You're actually creating a new (second) instance of Form1. This means that, in code, there are two different Form1's.
What we want to do is check with our application to get the instance of Form1 that already exists.
// This goes in Form2. It returns an instance of Form1, if it exists.
private Form getForm1()
{
// Application holds information about our application, such as which forms are currently open.
var formCollection = System.Windows.Forms.Application.OpenForms;
// Now we loop through the open forms in search of the form we want, Form1.
foreach (Form frm in formCollection)
{
if (frm.Name.Equals("Form1"))
{
return frm;
}
}
return null;
}
Now that we can get the existing instance of Form1 we can use it to make the form hidden.
private void button1_Click(object sender, EventArgs e)
{
var form1 = getForm1();
if (form1 != null) form1.Hide();
}
Something to know here is that when a form is hidden it is not closed. So, we need to make sure that Form1 becomes visible again. For example, we can set Form1 to be visible when Form2 closes.
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
// The question mark (?) checks to see if the result of
// getForm1() is null. Same thing that is happening in
// button1_click
getForm1()?.Show();
}
Complete Form2 Code
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var frm1 = getForm1();
var frm3 = new Form3();
if (frm1 != null) frm1.Hide();
frm3.Show();
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
// The question mark (?) checks to see if the result of getForm1() is null. Same thing that is happening in button1_click
getForm1()?.Show();
}
// This goes in Form2. It returns an instance of Form1, if it exists.
private Form getForm1()
{
// Application holds information about our application, such as which forms are currently open.
// Note that Open and Visible have different definitions.
var formCollection = System.Windows.Forms.Application.OpenForms;
// Now we loop through the open forms in search of the form we want, Form1.
foreach (Form frm in formCollection)
{
if (frm.Name.Equals("Form1"))
{
return frm;
}
}
return null;
}
}

Enable control of form based on form it was loaded?

How to enable control of a form based in form it was loaded?
For example if Form3 was open from Form1 the button that will be enable in Form3 is button1 and if Form3 was open from Form2 the button that will be enable in Form3 is button2.
Yes you can, just set the Owner of the Form3 when you Show it, then in Form3's Load EventHandler check the Type of the Owner to determine which button to enable. Something like this should work.
Form1
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form3 frm3 = new Form3();
frm3.Show(this);
}
private void Form1_Load(object sender, EventArgs e)
{
Form2 frm2 = new Form2(); //Show Form2 for Testing
frm2.Show();
}
}
Form2
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form3 frm3 = new Form3();
frm3.Show(this);
}
}
Form3
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
private void Form3_Load(object sender, EventArgs e)
{
if (Owner == null) return; //Check to make sure there is an Owner
if (Owner.GetType() == typeof(Form1))
button1.Enabled = true;
else if (Owner.GetType() == typeof(Form2))
button2.Enabled = true;
}
}
I think if (typeof(ParentForm) == typeof(Form1)) will do what you want. Keep in mind this check is based purely on the type so if you have multiple instances of Form1 you could run into some problems.
What he means is if you have a form that is of type Form than you wouldn't know which form is the parent if you only look at types.
Form1 opens Form3 and both forms are of type System.Windows.Forms
Form2 opens Form3 and both forms are of type System.Windows.Forms
If all three forms have different types than you can check if types are different.
Otherwise you have to check names.
form1.Name = "form1";
form2.Name = "form2";
form3.Name = "form3";
You will open child forms from within form1 with
form3.ShowDialog(this);
In form3 you can check for parent form and check for it's name. If it's form2 do something otherwise do something else.

Show this form from within new form instance

I have created two forms in C# using "Add new item". Form1 is my default opening form. I'm using the following code to switch to form2:
Form1 class:
Form form2= new form2();
this.Hide();
form2.Show();
Form2 class:
what should i do here to open the same form1 again without creating the new instant of form1?
You should pass an instance of this to form2 and have it .Show() it when the time comes.
A simple solution could be showing the second form modally, then making the first form visible when the second form closes, like this:
public partial class Form1: Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
this.Hide();
form2.ShowDialog();
this.Show();
}
}
public partial class Form2: Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Close();
}
}
Making this work non-modally is left as an exercise :)
When you are doing form2.hide() you are actually hiding not destroying it
so the instance you have created still exist so you can use to show it again
if you use form2.dispose() then you have to create a new instance
In the form2 code behind, add parameter in constructor
Form2(Form form1)
{
//use form1 object here
//you can declare a variable of Form1 in Form2 and use it everywhere in the scope of form2
}
Then while initialising object of form2:
Form form2 = new Form(this);
this.Hide();
form2.show();

A blocking/unblocking winforms call

I am currently facing this scenario and i need your help :
Having two winForms Form1 and Form2, a click button's event on form1 will launch form2.
I want to launch form2 and close (dispose) form1.
I have two ways to call form2 :
1) Using a blocking call with ShowDialog();
namespace programm
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void callForm2bt_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.ShowDialog();
this.Close();
}
}
}
In this case, once form2 is called i can't close (dispose) form1.
2) Using an unblocking call with Show() ;
namespace programm
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void callForm2bt_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.Show();
this.Close();
}
}
}
In this case, once form1 is closed ( disposed) it dispose automatically form2.
Any idea how to dispose form1 and keeping form2 functional ?
Thanks
Either do the inverse: run the Form2 as the main form, and set its visibility to false, and start Form1 from it, and when finished from Form1 close it and set the Form2 visibility to true. So:
static void Main()
{
...
Application.Run(new Form2());//instead of Form1
}
public class Form2 ...
{
//At From2.Load:
private void Form2_Load(object sender, EventArgs e)
{
this.Hide();//the form2 will hide and show the form 1.
Form1 form1 = new Form1(this);
form1.Show();
}
}
public class Form1...
{
private Form2 _form2 = null;
public Form1()
{ InitializeComponents();}
public Form1(Form2 form2) : this()
{
_form2 = form2;
}
private void callForm2bt_Click(object sender, EventArgs e)
{
if (_form2 != null)
{
_form2.Show();
}
this.Close();
}
}
Or use your current method but don't close the Form1, instead set its visability to false when you finished from it. by calling this.Hide(); or this.Visable = false; Like:
private void callForm2bt_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.Show();
this.Hide();//this will hide the control from the user but it will still alive.
}
Edit: At the first solution you can also use form1.ShowDialog() and get rid from passing Form2 instance to Form1 constructor, So:
//At From2.Load:
private void Form2_Load(object sender, EventArgs e)
{
this.Hide();//the form2 will hide and show the form 1.
Form1 form1 = new Form1();
form1.ShowDialog();
this.Show();//the form1 is closed so just show this again.
}
From MSDN for Application.Run(Form) method:
"This method adds an event handler to the mainForm parameter for the Closed event. The event handler calls ExitThread to clean up the application."
http://msdn.microsoft.com/en-us/library/ms157902(VS.90).aspx
Basically, when your main form exits, all message pumps are stopped.
I don't know what the forms do in your real-world application but you'll have to work around this behavior. Some ideas - and the correct one probably depends on what you're doing:
If Form1 is some kind of dialog, you could call ShowDialog on it before Application.Run. For example:
static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Form1 fm = new Form1();
fm.ShowDialog();
Application.Run(new Form2());
}
When Form1 is closed then Form2 will be opened. The return value of ShowDialog could be used to determine whether to proceed and open Form2, or exit the application.
Another idea might be to call Application.Run twice. Form1 could set a flag indicating whether to open Form2.
Usually in my applications, Form1 is typically some kind of dialog (e.g. a registration form, etc.) and so the first behavior is usually what I do. If the dialog is cancelled then I don't run the application.
Edit: If Form1 may branch out to a number of other forms, you could have it return the form for opening to Main via a field. For example:
Add a public field "Form FormToOpen" to Form1. Set it to null when the form is constructed.
When the button on Form1 is pressed, give FormToOpen a value. For example: "FormToOpen = new Form2()".
Change the Application.Run line as follows:
if (fm.FormToOpen != null) Application.Run(fm.FormToOpen);
At this point it would be trivial for Form1 to have more buttons that open other forms. The main function would not need special knowledge about each form, and the additional forms would not need special knowledge about Form1.
Consider injection of Form2 instance through constructor of Form1, it reduces a class coupling and increases a flexibility, in this way you can safely dispose form1 and keep form2 a live

How can I navigate between forms

I am a newbiest in c# and window form
i am doing a project and i meet some problem
how can i navigate forms within the window( i have a menu strip, when click it will show a item "Brand", so when i click it, it should open up within the window , i don't want something using the mdiparent/container, i have form1 and form2, then i put the menu strip in form1, which there is some thing inside form1, if use the mdiparent/container, the form1 content/thing will block the form2 )
2.i use the below code and the problem is i want to close the form1 which i click on " Brand" in the menu strip...but how???
public partial class Form1 : Form
{
// i put the menu strip in form1 design
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void Check_Click(object sender, EventArgs e)
{
Form2 Check = new Form2();
Check.Show();
}
}
You cannot just close the Form1 as it is the main form, but you can hide it. Use this.Hide().
private void Check_Click(object sender, EventArgs e)
{
Form2 Check= new Form2();
Check.Show();
Hide();
}
[EDIT]
Not sure if this is what is asked. But...
There are many ways to implement navigation between forms, for example:
In Form1:
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.Tag = this;
form2.Show(this);
Hide();
}
In Form2:
private void button1_Click(object sender, EventArgs e)
{
var form1 = (Form1)Tag;
form1.Show();
Close();
}
I think you should create usercontrols rather than different forms. Then you can add your usercontrols in your main panel according to the selection in the menu.
Initially something like below
this.panel.Controls.Clear();
this.panel.Controls.Add(new UserControl_For_Form1());
Once the user click some other selection in menu.
this.panel.Controls.Clear();
this.panel.Controls.Add(new UserControl_For_Form2());
If you really want to use the way that you are using at the moment. Below code will help.
Add a Form1 property for the Form2 and parse the form1 instance to the Form2 with its constructor.
public partial class Form2 : Form
{
private Form1 form1;
public Form2(Form1 myForm)
{
InitializeComponent();
form1 = myForm;
}
}
Show the form2 and hide the form1.
private void Check_Click(object sender, EventArgs e)
{
Form2 Check= new Form2(this);
Check.Show();
Hide();
}
In form2 closing event now you can show the form1 instance which is in the form2 and close the form2.
Using of MDI form is another option for you.
It has been 7 years since this question was asked but I wanna give an answer in case if someone is still looking for a solution. If you are using DevExpress, you can add Navigation Frame to your program. You can switch between different components such as GridControl, GroupBox and so on. So you dont have to create an extra form in order to navigate between forms.

Categories

Resources