close warning keep poping up - c#

There is two forms in my program,The first one is a simple login form that after everything was right loginform hides and the other shows.
But the problem is if i try to close the program at loginform and press no then when i press signin button with correct values the closing messagebox shows up again with no reason!!!!
private void LoginForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
{
DialogResult DR= MessageBox.Show("do you want to exit?", "Exit", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button2);
if (DR == DialogResult.Yes)
Application.Exit();
else if (DR == DialogResult.No)
e.Cancel=true;
}
}
private void Sign_in_Button_Click(object sender, EventArgs e)
{
if (AccountExistStatus(UserNameLogin.Text)==true&&PasswordMatch(PasswordLogin.Text))
{
this.Hide();
}
else if (UserNameLogin.Text == "Echo" && PasswordLogin.Text == "XLVI")
{
this.Hide();
}
}

I think your problem is that you have just hidden that Login form, you have not closed it anywhere. Have a look at the following code which solves your problem. it will close your LoginForm also at the close of the next form
private void Sign_in_Button_Click(object sender, EventArgs e)
{
if (AccountExistStatus(UserNameLogin.Text)==true&&PasswordMatch(PasswordLogin.Text))
{
DisplayForm2();
}
else if (UserNameLogin.Text == "Echo" && PasswordLogin.Text == "XLVI")
{
DisplayForm2();
}
}
void DisplayForm2() {
this.Hide();
var form2 = new Form2();
form2.Closed += (s, args) => this.Close();
form2.Show();
}

Related

how to exit from application on click of (red X ) button right top on winform

i have two forms in my application. frmLogin and frmDash. after login. i am hiding frmLogin on click of login button. ad showing frmDash.
in frmDash, there is LogOut button. on click of LogOut, i am using this.Close() and showing login form. but now if i click (red X) button of frmLogin whole application is not terminating. plz give some suggestions.
i have tried this.:
private void btnLogin_Click(object sender, EventArgs e)
{
try
{
this.Hide();
string Log_API = "http://api.retailbutton.co/WS/Service.php?Service=employeeLogin";
if (LoginUser(Log_API))
{
logIn_Status = "true";
GlolbalUtil.LogIn_Status = logIn_Status;
frmDash frmDash = new frmDash();
frmDash.Owner = this;
frmDash.Show();
txtUsername.Text = "";
txtPassword.Text = "";
//GlolbalUtil.accept_status = "1";
}
else
{
MessageBox.Show("Please Check Username and password");
FrmLogin frmLogin = new FrmLogin();
frmLogin.Owner = this;
frmLogin.Show();
}
}
code for Logout button of frmDash:
private void button1_Click(object sender, EventArgs e)
{
GlolbalUtil.LogIn_Status = "false";
this.Close();
FrmLogin fl = new FrmLogin();
fl.Show();
}
You create a new instance of frmDash when you log in and hide the form.
Then when you are logging out, you say this.close() and create another new instance of FrmLogin. Not going back to the original instance of FrmLogin.
This means that you will always will have the hidden instance which you started with.
(If you close the new instance of FrmLogin, the hidden FrmLogin still exists.)
You can add the following in btnLogin_Click:
frmDash.ParentForm = this;
and button1_Click should look like this:
private void button1_Click(object sender, EventArgs e){
GlolbalUtil.LogIn_Status = "false";
FrmLogin fl = (FrmLogin)this.Parent; //Prior it said ParentForm
this.Close();
fl.Show();
}
If you implement this, you will show the initial login form and when you close it, you close the initial instance of the login form.
#Edit 10:52 25-06-2015
ParentForm cannot be assigned and is read only. A solution is to assign it to Parent or the following can also be applied in btnLogin_Click:
frmDash.Owner = this;
and button1_Click:
private void button1_Click(object sender, EventArgs e){
GlolbalUtil.LogIn_Status = "false";
FrmLogin fl = (FrmLogin)this.Owner
this.Close();
fl.Show();
}
#Edit 08:16 29-06-2015 (Next question)
private void btnLogin_Click(object sender, EventArgs e)
{
try
{
string Log_API = "http://api.retailbutton.co/WS/Service.php?Service=employeeLogin";
if (LoginUser(Log_API))
{
logIn_Status = "true";
GlolbalUtil.LogIn_Status = logIn_Status;
frmDash frmDash = new frmDash();
frmDash.Owner = this;
////If you hide here, you do not have to make
//a new instance when the if statement is not true.////
this.Hide();
frmDash.Show();
txtUsername.Text = "";
txtPassword.Text = "";
//GlolbalUtil.accept_status = "1";
}
else
{
MessageBox.Show("Please Check Username and password");
////Delete following////
//FrmLogin frmLogin = new FrmLogin();
//frmLogin.Owner = this;
//frmLogin.Show();
}
}
If you are using a custom button, do this
private void QuitButton_Click(object sender, EventArgs e)
{
DialogResult DialogResult = MessageBox.Show("Do you really want to exit?", "Confirmation",
MessageBoxButtons.YesNo, MessageBoxIcon.Hand);
if (DialogResult == DialogResult.Yes)
Application.Exit();//Here is the code to close everything
else
//Do stuff
}
If you are using the X button, add a FormClosing Event and the code look like this:
private void Form1_FormClosing(object sender, FormClosingEventArgs e){
DialogResult DialogResult = MessageBox.Show("Do you really want to exit?", "Confirmation",
MessageBoxButtons.YesNo, MessageBoxIcon.Hand);
if (DialogResult == DialogResult.Yes)
Application.Exit();//Here is the code to close everything
else
//Do stuff
}
use Application.Exit(); method when your button clicked
Add this to your Designer.cs
this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.SampleClassName_FormClosed);
then right click the SampleClassName_FormClosed and click "Go to Definition".
then inside the created class call the form that you want to close.
Try the following on your 2. Form:
public partial class Form2 : Form
{
private bool ForceClose = true;
public Form2()
{
InitializeComponent();
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
if (ForceClose)
Application.Exit();
}
private void button1_Click(object sender, EventArgs e)
{
GlolbalUtil.LogIn_Status = "false";
ForceClose = false;
this.Close();
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
Application.Exit();//close the whole program using x (red)button
}

Form close event

I had an problem with my application closing in c#. When I hit the close button it display twice or more times the message box. What should I do?
private void home_FormClosed(object sender, FormClosedEventArgs e)
{
DialogResult dialog = MessageBox.Show("Are you sure you want to really exit ? ",
"Exit",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question);
if (dialog == DialogResult.Yes)
{
System.Windows.Forms.Application.Exit();
}
else if (dialog == DialogResult.No)
{
this.Show();
}
}
Use Form1_FormClosing event and also don't use Application.Exit() like this:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
var x = MessageBox.Show("Are you sure you want to really exit ? ",
"Exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (x == DialogResult.No)
{
e.Cancel = true;
}
else
{
e.Cancel = false;
}
}
Or like this:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = MessageBox.Show("Are you sure you want to really exit ? ",
"Exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No;
}
you should use the Form.FormClosing event instead of the FormClosed event. in the arguments, you find a field e.Cancel. by setting this to false, you keep your form open
You can avoid the multiple prompts by checking to see if Application.Exit() has been called already from within the FormClosing event:
void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason != CloseReason.ApplicationExitCall)
{
DialogResult dialog = MessageBox.Show("Are you sure you want to really exit ? ", "Exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dialog == DialogResult.Yes)
{
System.Windows.Forms.Application.Exit();
}
else if (dialog == DialogResult.No)
{
e.Cancel = true;
}
}
}
Use the form closing event instead.
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
var confirmation = MessageBox.Show("Sure to close form", "Confirm", MessageBoxButtons.YesNo);
if (confirmation == System.Windows.Forms.DialogResult.No)
{
e.Cancel = true; //Even cancelled, form will not get closed now
}
}

Terminate an application with ContextMenuStrip

I want to hide my application in system tray when I click the Form Closing button(clasic red X button). I provided with this code;
private void Ana_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
this.Hide();
}
So, the application is stil running in the system tray. I have added a ContextMenuStrip and when I right click on mouse ContextMenuStrip a Close button comes up but when I click that Close button the application is still running.
I want to terminate the application when I click that Close button. Here is my code:
private void kapatToolStripMenuItem_Click(object sender, EventArgs e) //Close
{
DialogResult ext;
ext = MessageBox.Show("Çıkmak İstediğinizden Emin misiniz?", "Onay", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (ext == DialogResult.Yes)
{
Application.Exit();
}
}
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
this.Show();
}
Calling Application.Exit() will actually attempt to close all your forms, and because your FormClosing is hard coded to be cancelled, then it cannot complete the task.
One option with your current structure would be to create an AllowClose flag. This could be a property of the Form, or it could be a global static value.
For example:
//in your form
public bool AllowClose {get;set;}
private void Ana_FormClosing(object sender, FormClosingEventArgs e)
{
if(!AllowClose)
{
e.Cancel = true;
this.Hide();
}
}
//in your context menu event
private void kapatToolStripMenuItem_Click(object sender, EventArgs e) //Close
{
DialogResult ext;
ext = MessageBox.Show("Çıkmak İstediğinizden Emin misiniz?", "Onay", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (ext == DialogResult.Yes)
{
this.AllowClose = true;
Application.Exit();
}
}
Try this,
bool isClosing = false;
private void Ana_FormClosing(object sender, FormClosingEventArgs e)
{
if(!isClosing)
{
e.Cancel = true;
this.Hide();
}
}
private void kapatToolStripMenuItem_Click(object sender, EventArgs e) //Close
{
DialogResult ext;
isClosing = true;
ext = MessageBox.Show("Çıkmak İstediğinizden Emin misiniz?", "Onay", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (ext == DialogResult.Yes)
{
Application.Exit();
}
}

Form closing fire after: button.performclick

Kindly help me to solve this form closing fire after button perform click, since I only depend on windows close button (top right corner), I do not use additional button to close the form.
However this program is still working but not automatically close the form right after save the .ini file..
I want the form closed ...after button1.performclick(), but I don't know what to do..
I have following codes:
int beFore, afTer;
private void Form3_Load(object sender, EventArgs e)
{
beFore = this.checkedListBox1.CheckedIndices.Count +
this.checkedListBox2.CheckedIndices.Count +
this.checkedListBox3.CheckedIndices.Count +
this.checkedListBox4.CheckedIndices.Count;
}
//private Form4 subForm4 = new Form4();
private void Form3_FormClosing(object sender, FormClosingEventArgs e)
{
afTer = this.checkedListBox1.CheckedIndices.Count +
this.checkedListBox2.CheckedIndices.Count +
this.checkedListBox3.CheckedIndices.Count +
this.checkedListBox4.CheckedIndices.Count;
while (beFore != afTer)
{
if (MessageBox.Show("Changes have been made..\r\nSave to configuration file (.ini) ?", "Warning",
MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
{
this.button1.PerformClick(); //need to close this form after button.performclick..
this.UpdateForm1();
break;
}
else
{
this.UpdateForm1();
break;
}
}
beFore = afTer;
}
private void UpdateForm1()
{
Form4 subForm4 = new Form4();
subForm4.Show();
subForm4.Update();
try
{
int checkagain1 = this.checkedListBox1.CheckedIndices.Count; this.checkedListBox1.SetItemChecked(2, true);
int checkagain2 = this.checkedListBox2.CheckedIndices.Count; this.checkedListBox2.SetItemChecked(2, true);
int checkagain3 = this.checkedListBox3.CheckedIndices.Count; this.checkedListBox3.SetItemChecked(2, true);
int checkagain4 = this.checkedListBox4.CheckedIndices.Count; this.checkedListBox4.SetItemChecked(2, true);
Form1 myParentForm = (Form1)this.Owner;
if (myParentForm.comboBox1.Text.Length != 0)
{
//myParentForm.Enabled = false;
myParentForm.method1();
myParentForm.method2();
myParentForm.method3();
myParentForm.method4();
//myParentForm.Enabled = true;
subForm4.Close();
}
}
catch (Exception)
{
subForm4.Close();
return;
throw;
}
}
I'm not 100% sure what you want, but I'm pretty sure the answer you're looking for is in the DialogResult property of the form.
If your problem is a button is closing the form when you click it, then set DialogResult.None for the DialogResult property of the form.
DialogResult = DialogResult.None;
I didn't got you correctly but if you want to close the form if the result was yes you can do such this.
if (MessageBox.Show("Changes have been made..\r\nSave to configuration file (.ini) ?", "Warning",
MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
{
this.Dispose();
this.Close();
}
where Dispose cleans all resources used by the program and then close it, or Close close the form directly
or try doing this if you want such this:
private void button1_Click(object sender, EventArgs e)
{
this.Dispose();
this.Close();
}
if (MessageBox.Show("Changes have been made..\r\nSave to configuration file (.ini) ?", "Warning",
MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
{
button1.PerformClick();
}

Application Exit Collection was modified; enumeration operation may not execute

I am getting this error Collection was modified; enumeration operation may not execute.
I have 3 forms. These are the form closing events of all 3 I did some research and learnt that some from was modified/shown inturn causing this error
Form1
private void btnExitl_Click(object sender, EventArgs e)
{
this.Close();
}
private void frmPlant_FormClosing(object sender, FormClosingEventArgs e)
{
if (DataDirty)
{
if (DialogResult.Yes == MessageBox.Show("Are you sure you want to exit", "Data Changed", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
Application.Exit();
else
e.Cancel = true;
}
else
Application.Exit();
}
Form2:
private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
}
private void frmInputFiles_FormClosing(object sender, FormClosingEventArgs e)
{
int plantid = StaticClass.GlobalValue;
//Properties.Settings.Default.PlantId = plantid;
Program.fPlant = new frmPlant(plantid);
Program.fPlant.Show();
e.Cancel = false;
//this.Hide();
}
Form3:
private void btnClose_Click(object sender, EventArgs e)
{
this.Close();
}
private void frmVesselData_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult result;
int fileId = StaticClass.FileGlobal;
if (DataDirty)
{
string messageBoxText = "You have unsaved data. Do you want to save the changes and exit the form?";
MessageBoxButtons button = MessageBoxButtons.YesNo;
string caption = "Data Changed";
MessageBoxIcon icon = MessageBoxIcon.Question;
result = MessageBox.Show(messageBoxText, caption, button, icon);
if (result == DialogResult.No)
{
Program.fInputFiles = new frmInputFiles(gPlantId, gPlantName);
Program.fInputFiles.Show();
//e.Cancel=true;
}
if (result == DialogResult.Yes)
{
e.Cancel = true;
//return;
}
}
else
{
Program.fInputFiles = new frmInputFiles(gPlantId, gPlantName);
Program.fInputFiles.Show();
//e.Cancel = false;
}
}
It happens only when I view the third form(Form3). Form1, Form2 work well,. But if I view form3 and try to go back to form1
So somewhere in the closing event of form3, form1
My guess is the btnExit_close event of the form this.close()
Thank you
Simply call Environment.Exit(0);
While closing your First Form try this on FormClosingEvent
private void frmPlant_FormClosing(object sender, FormClosingEventArgs e)
{
if (DataDirty)
{
if (DialogResult.Yes == MessageBox.Show("Are you sure you want to exit", "Data Changed", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
{
this.Close();
Application.Exit();
}
else
e.Cancel = true;
}
else
Application.Exit();
}
Call this.Close() first and then Application.Exit(), Application.Exit() terminates all processes and Close() close your main form

Categories

Resources