How do I make the same functionality for form X (on the top extreme right) and close button. These 2 need to behave alike
This is what I have in btnClose_Click
private void btnClose_Click(object sender, EventArgs 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.fInput = new frmInputFiles(gtId, gName);
Program.fInput.Show();
this.Close();
}
if (result == DialogResult.Yes)
{
return;
}
}
else
{
Program.fInput = new frmInputFiles(gPlantId, gPlantName);
Program.fInput.Show();
this.Close();
}
}
Even on clicking the X to close the form,it should behave the same way as btnClose_Click
private void frmData_FormClosing(object sender, FormClosingEventArgs e)
{
btnClose_Click(sender,e);//this doesnt seem to be working.
}
It is going in a infinite loop. I understand y it is doing that.. btnClose_Click() has this.Close() which calls frmData_FormClosing.. which inturn calls btnclose..
Thank u
Just put this.Close() in the btnClose_Click() event. Then move all the rest of your logic (you'll need to edit it some) into the frmData_FormClosing() event and call e.Cancel = true; if you want to cancel closing the form (in your case, if there are unsaved changes and the user clicks Yes on the prompt.
Here's an example (I just cut and pasted in notepad, so fair warning):
private void btnClose_Click(object sender, EventArgs e)
{
this.Close();
}
private void frmData_FormClosing(object sender, FormClosingEventArgs e)
{
if (DataDirty)
{
if (MessageBox.Show("You have unsaved data. Do you want to save the changes and exit the form?",
"Data Changed", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
Program.fInput = new frmInputFiles(gtId, gName);
Program.fInput.Show();
}
else
e.Cancel = true;
}
else
{
Program.fInput = new frmInputFiles(gPlantId, gPlantName);
Program.fInput.Show();
}
}
Related
How can I prevent window closing by showing a MessageBox? (Technology:WinForms with C#)
When the close event occurs I want the following code to be run:
private void addFile_FormClosing( object sender, FormClosingEventArgs e ) {
var closeMsg = MessageBox.Show( "Do you really want to close?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Question );
if (closeMsg == DialogResult.Yes) {
//close addFile form
} else {
//ignore closing event
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e) {
var window = MessageBox.Show(
"Close the window?",
"Are you sure?",
MessageBoxButtons.YesNo);
e.Cancel = (window == DialogResult.No);
}
Catch FormClosing event and set e.Cancel = true
private void AdminFrame_FormClosing(object sender, FormClosingEventArgs e)
{
var res = MessageBox.Show(this, "You really want to quit?", "Exit",
MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2);
if (res != DialogResult.Yes)
{
e.Cancel = true;
return;
}
}
A special twist might be to always prevent just the user closing the form:
private void Frm_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = (e.CloseReason == CloseReason.UserClosing);
// disable user closing the form, but no one else
}
Within your OnFormClosing event you can show the dialog and if answer is false (to not show) then set the Cancel property of the EventArgs to true.
For prevent or block the form closing in particular situation you can use this strategy:
private void MyForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (FLAG_CONDITION == true)
{
MessageBox.Show("To exit save the change!!");
e.Cancel = true;
}
}
Straight from MSDN:
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
// Determine if text has changed in the textbox by comparing to original text.
if (textBox1.Text != strMyOriginalText)
{
// Display a MsgBox asking the user to save changes or abort.
if(MessageBox.Show("Do you want to save changes to your text?", "My Application",
MessageBoxButtons.YesNo) == DialogResult.Yes)
{
// Cancel the Closing event from closing the form.
e.Cancel = true;
// Call method to save file...
}
}
}
In your case you don't need to do anything to explicitly close the form. Unless you cancel it it will close automatically, so your code would be:
private void addFile_FormClosing( object sender, FormClosingEventArgs e ) {
var closeMsg = MessageBox.Show( "Do you really want to close?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Question );
if (closeMsg == DialogResult.Yes) {
// do nothing
} else {
e.Cancel = true;
}
}
You can run any code you want when closing the form then, hide the form instead of closing it to prevent it from being disposed
yourFormName.FormClosing += (s, e) =>
{
// any code you want
yourFormName.Hide(); // this hides the form
e.Cancel = true; // this cancels the close event, you can put any boolean exprission
};
Okay Problem As Follows:
I call up a custom form as follows:
SomeCustomForm _newForm = new SomeCustomForm();
_newForm.ShowDialog();
//**SOME OTHER CODE**
Now lets say we have some custom event (Mine is on a DataGridView DoubleClick):
private void dgvSomeGrid_DoubleClick(object sender, EventArgs e)
{
string name = dgvSomeGrid.CurrentRow.Cells[5].Value.ToString();
DialogResult = MessageBox.Show(name, "Select this Merkmal?", MessageBoxButtons.YesNo);
if (DialogResult == DialogResult.Yes)
{
_someID = Convert.ToInt32(dgvMSomeGrid.CurrentRow.Cells[0].Value.ToString());
this.Close();
}
else if (DialogResult == DialogResult.No)
{
return;
}
}
The dialog works fine in that the no and yes buttons behave as expected. My problem though is that irrespective of which button is clicked, the code jumps back to //**SOMEOTHERCODE. So in effect, the _newForm is just closed.
I obviously don't want this to happen as I am not done on the other form yet if the "No" button is clicked.
Any help?
EDIT:
My apologies - for the sake of clarity. The grid mentioned above is on the _newForm. And the dialog is called from the _newForm.
This closes unexpectedly.
don't use DialogResult property of form for comparison. Set it only on successful close
private void dgvSomeGrid_DoubleClick(object sender, EventArgs e)
{
string name = dgvSomeGrid.CurrentRow.Cells[5].Value.ToString();
var result = MessageBox.Show(name, "Select this Merkmal?", MessageBoxButtons.YesNo);
if (result == DialogResult.Yes)
{
_someID = Convert.ToInt32(dgvMSomeGrid.CurrentRow.Cells[0].Value.ToString());
this.DialogResult = DialogResult.Yes;
this.Close();
}
}
somewhere in code type static bool formCloseFlag = false;
Let's shorten your event handler code a bit:
private void dgvSomeGrid_DoubleClick(object sender, EventArgs e)
{
string name = dgvSomeGrid.CurrentRow.Cells[5].Value.ToString();
DialogResult = MessageBox.Show(name, "Select this Merkmal?", MessageBoxButtons.YesNo);
if (DialogResult == DialogResult.Yes)
{
_someID = Convert.ToInt32(dgvMSomeGrid.CurrentRow.Cells[0].Value.ToString());
formCloseFlag = true;
}
return;
}
then change your code like this :
SomeCustomForm _newForm = new SomeCustomForm();
_newForm.ShowDialog();
//**SOME OTHER CODE**
if(formCloseFlag) { _newForm.Close(); formCloseFlag = false; }
//**SOME OTHER CODE**
A workaround on this is to add a handler for the FormClosing event of your form, so you can cancel it there.
[edit]
After some experimenting, it seems that the bug is somewhat detectable by checking FormClosingEventArgs.CloseReason. This is normally "UserClosing" on normal close (even programmatically with calling this.Close()), but with this bug it's set to "None", a value I would assume is some kind of default that should normally never be used.
private void form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.None)
{
e.Cancel = true;
return;
}
// any other OnClose code you may wish to execute.
}
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();
}
}
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();
}
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