I need to close a form on a button click event.Here in my example I am hiding the form.Think this is not a good way.When I do only Close() the form is disposed forever and need to rerun the programme to retrieve it.
private void buttonClose_Click(object sender, EventArgs e)
{
this.Close(); //closing frmCalender
}
private void frmCalender_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
{
this.Hide();
e.Cancel = true;
}
}
Give me the best way to close a C# Windows Form.
If you want to close a form, call .Close().
When I do only Close() the form is disposed forever and need to rerun the programme to retrieve it.
When you close the form, I assume you have no references to it. If so, you can create a new copy of your form via the constructor (var form = new MyForm();).
Otherwise, after closing the form, I believe you should be able to call .Show() on it again, as long as something still has a reference to your form.
I think, the best approach would be:
private void buttonClose_Click(object sender, EventArgs e)
{
this.Hide();
}
Related
I'm newbie of winform. I have opened form2 form a linklabel in form1 using :
private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
FrmAddMov frmAddMov = new FrmAddMov();
if(frmAddMov.ShowDialog() == DialogResult.OK)
{
this.Invalidate();
//or
this.Refresh();
}
}
I thought form1 will reload after I submit form2, but not. Please tell me the right way to do it. Thanks a lot, and sorry if my english is too bad.
Gentleman's answer will work, but it can be improved.
When showing a form using ShowDialogthan it is best practice to dispose of that form, and the easiest way to do that is by the using statement
private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
using (FrmAddMov frmAddMov = new FrmAddMov())
{
if (frmAddMov.ShowDialog() == DialogResult.OK)
{
FormLoad();
}
}
}
This way you are always 100% sure that all resources for frmAddMov will be cleaned up.
Move everything in your form load event to a method say FormLoad. You may want to add few other statements which you are expecting form reload will do for you. Call this method when your 2nd form closes.
Something like this
private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
FrmAddMov frmAddMov = new FrmAddMov();
if(frmAddMov.ShowDialog() == DialogResult.OK)
{
FormLoad();
}
}
I am working in compact framework 3.5,i am working on windows mobile application.
form1.cs
internal void menuItem_Click(object sender, EventArgs e)
{
this.Close();
form2 obj = new form2();
obj.ShowDialog();
/////other code
}
form2.cs
public void backbutton_Click(object sender, EventArgs e)
{
form1 obj = new form1();
obj.ShowDialog();
/////other code
}
Initially i am in form1 and i am navigating to form2.In form2,a back button is there and on clicking backbutton i need to show the from1.Now i can show the form1 on backbutton click,but i need to close the previous opened form1.
I tried with this.Close() and this.Dispose(). This makes me exist from my application.
How to do this in compact framework.
Best way to work with that kind of things is using a singleton. When you show your first form, you instance the first form, then you show the second creating it instance and hiding the first one (use hide(), not close(), then your instance will still on memory). On back button click, you only have to hide() the second, and show() the first.
I would recommend instead of creating new form1 from form2, just close form2 and go back to form1:
form2.cs:
public void backbutton_Click(object sender, EventArgs e)
{
this.Close();
}
form1.cs:
internal void menuItem_Click(object sender, EventArgs e)
{
form2 obj = new form2();
obj.ShowDialog();
/////other code
}
I am developing a simple app using c# Windows forms. The main form open another form, but i dont want the both forms. I want it that when the second form opens the first form closes. Since the first form is the main using
this.Close();
after showing the second form will close the both. So i used this instead
private void btnSubmit_Click(object sender, EventArgs e)
{
frmData QS = new frmData();
QS.Show();
this.WindowState = FormWindowState.Minimized;
this.ShowInTaskbar = false;
}
private void frmData_FormClosed(object sender, FormClosedEventArgs e)
{
Application.Exit();
}
I want to know if there is any other way to do this.
Any help will be appreciated.
Do not pass your main form as argument to Application.Run:
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
MainForm frmMain = new MainForm();
frmMain.Show();
Application.Run();
Thus you will be able to close it when showing another form:
private void btnSubmit_Click(object sender, EventArgs e)
{
frmData QS = new frmData();
QS.Show();
this.Close();
}
To close application you should use
Application.Exit();
Hide the first form.
private void btnSubmit_Click(object sender, EventArgs e)
{
frmData QS = new frmData();
QS.Show();
this.Hide();
}
private void frmData_FormClosed(object sender, FormClosedEventArgs e)
{
Application.Exit();
}
Actually there's no possible way to close the main form if you close the main form it closes all forms of the same project. You can only hide it.
BTW you can open and show a form in only one simple line like this: new FormName().Show();
I have a MDI form. Within this MDI there is multiple button to open new forms. Let buttons are btn1, btn2, btn3, btn4.... When I press btn1, form1 is load. when I press btn2, form2 is load... Now I press btn1, And form1 is loaded. If I press again btn1 then another form1 is open. Simultaneously let form1 is open, if I press btn2 form2 is open. But I want to open a form at a time. How I prevent this?
all the answers you got are good so i'm not going to repeat them, just give you an example of the member and method you can use to prevent that from happening:
private Form frm;
private void button1_Clicked(object sender, EventArgs e)
{
if (frm != null)
{
frm.Close();
frm.Dispose();
}
frm = new Form1();
frm.Show();
}
private void button2_Clicked(object sender, EventArgs e)
{
if (frm != null)
{
frm.Close();
frm.Dispose();
}
frm = new Form2();
frm.Show();
}
You can read up about mutual exclusion http://msdn.microsoft.com/en-us/library/system.threading.mutex.aspx
It is a general solution to make sure you only have 1 thing (thread, process, form, whatever) of something at the same time. You can even use it inter application.
An example is shown here: http://www.dotnetperls.com/mutex
You can create multiple mutexes, one for each form. Or one for a set of forms, in what ever combination suits you.
Example Scenario:
Form1 creates a mutex with name X
Form2 is being loaded checks whether mutex X is created, if so it closes itself.
Of course you will need to make sure the mutex is Disposed / released when the creator (Form1 in this example) closes, to allow other forms to show.
You can use some flag for this purpose OK, like this:
bool formOpened;
private void buttons_Click(object sender, EventArgs e){
if(!formOpened){
//Show your form
//..............
formOpened = true;
}
}
//This is the FormClosed event handler used for all your child forms
private void formsClosed(object sender, FormClosedEventArgs e){
formOpened = false;
}
At least this is a simple solution which works.
In general case, you need a int variable to count the opened forms, like this:
int openedForms = 0;
//suppose we allow maximum 3 forms opened at a time.
private void buttons_Click(object sender, EventArgs e){
if(openedForms < 3){
//Show your form
//..............
openedForms++;
}
}
private void formsClosed(object sender, FormClosedEventArgs e){
openedForms--;
}
Does this mean while you have Form1 open, you want to still be able to open a Form2 and 3 and etc?
It you don't want that, you can use the form1Instance.SHowDialog() instead of Show()...
But that generally means you can't access the parent form while form1 is open...
But King King's anwser might be more useable to you.
I have an application which have 2 forms, a main and an other form.
I have an exit button in the other form (when pressed Application.Exit()) but how can i call this, when somebody click the x on this form?
Call it on form closing or form closed event of your form.
In OtherForm.Closed(sender, e) or OtherForm.Closing(sender, e)
Application.Exit();
Simple; in Form1.cs
private void CloseApplicationButton_Click(object sender, EventArgs e)
{
Form2 m= new Form2();
m.X_Click(this, null);
}
so in form2.cs make click event public
public void X_Click(object sender, EventArgs e)
{
Application.Exit();
}
Why not just have both do
Application.Exit();
Unless you have some common code you want to run as part of the exiting scenario?