Form closing fire after: button.performclick - c#

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();
}

Related

WinForms - Alert When User Leaves Page Without Saving [duplicate]

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
};

C# Windows Forms Application Closing

I making a C# Windows Forms Application. I'm trying to have a message box popup when the "X" or Close Button is pressed to exist out of the application. This is what I have so far, and I don't know whats wrong with it. When I run the message box doesn't show up when I click on the Close Button. Any help would be appreciated. Thanks.
private void MainWindow_FormClosing(object sender, FormClosingEventArgs e)
{
if (this.DialogResult == DialogResult.Cancel)
{
if (MessageBox.Show("Do you want to save changes to the data?",
"MktAuthorizationData",
MessageBoxButtons.YesNo) == DialogResult.Yes)
{
e.Cancel = true;
// Do Something
}
}
}
You need to remove below condition
if (this.DialogResult == DialogResult.Cancel)
The below code should work
private void MainWindow_FormClosing(object sender, FormClosingEventArgs e)
{
if (MessageBox.Show("Do you want to save changes to the data?",
"MktAuthorizationData",
MessageBoxButtons.YesNo) == DialogResult.Yes)
{
e.Cancel = true;
// Do Something
}
}
In case you haven't subscribed to the event you need to do this by having this after InitializeComponent();
this.FormClosing += MainWindow_FormClosing;
You can add a Button Close event to close your window form with message box.
private void btnClose_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Do you want to save changes to the data?",
"MktAuthorizationData",
MessageBoxButtons.YesNo) == DialogResult.Yes)
{
e.Cancel = true;
// Do Something
}
}

Built in MessageBox Dialog Closing custom Form.ShowDialog()

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.
}

Click on "X" button to save file

I am trying to make WTF application were user clicks on "X" button to make save file, now this can be done by Message.Show or to ask directly to save. I have created one code already but when user click on save or cancel, error window shows up thatprogram start working and it wants to send information to Microsoft.
private void Window_Closing(object sender, CancelEventArgs e)
{
Microsoft.Win32.SaveFileDialog saveDlg = new Microsoft.Win32.SaveFileDialog();
saveDlg.DefaultExt = ".rtf";
saveDlg.Filter = "RTF Documents (.rtf)|*rtf";
Nullable<bool> rezultat = saveDlg.ShowDialog();
if (rezultat == true)
{
string filename = saveDlg.FileName;
System.IO.File.Create(filename);
}
{
this.Close();
}
}
I think that you might have intended there to be an else
else
{
this.Close();
}
Secondly, calling this.Close(); inside the Window_Closing event is just asking for a Stack Overflow exception.
You don't need to close the window again. It is already closing.
modify with your code with proper else statement with your if condition
private void Window_Closing(object sender, CancelEventArgs e)
{
Microsoft.Win32.SaveFileDialog saveDlg = new Microsoft.Win32.SaveFileDialog();
saveDlg.DefaultExt = ".rtf";
saveDlg.Filter = "RTF Documents (.rtf)|*rtf";
Nullable<bool> rezultat = saveDlg.ShowDialog();
if (rezultat == true)
{
string filename = saveDlg.FileName;
System.IO.File.Create(filename);
}
else
{
this.Close();
}
}

C# form X close and btnclose to function the same

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();
}
}

Categories

Resources