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();
}
}
Related
I implement a form for handle excel file when click button "Start".
Event click Start button:
private void btnImport_Click(object sender, EventArgs e)
{
showFormSelectLanguage();
if (CheckSheetFile() == true) {
using (WaitingForm frm = new WaitingForm(handleExcel))
{
frm.ShowDialog(this);
}
var dialogMessage = new DialogMessage();
dialogMessage.ShowDialog(this);
} else
{
ShowDialogNotFoundSheet();
}
}
showFormSelectLanguage method display dialog for select language:
private void showFormSelectLanguage()
{
var formSelectLanguage = new FormSelectLanguage();
formSelectLanguage.ShowDialog(this);
}
ShowDialogNotFoundSheet function for check sheet excel exist:
private void ShowDialogNotFoundSheet()
{
var dialogNotFoundSheet = new DialogNotFoundSheet();
dialogNotFoundSheet.setTextContent("Not found sheet");
dialogNotFoundSheet.ShowDialog(this);
}
Event click confirm select language button at Select language form:
private void btnConfirmLanguage_Click(object sender, EventArgs e)
{
//close dialog
this.Close();
}
Event click Close button for close DialogNotFoundSheet form:
private void btnCloseDialogNotFoundSheet_Click(object sender, EventArgs e)
{
this.Close();
}
CheckSheetFile method:
private bool CheckSheetFile()
{
var isCorrectFile = false;
try
{
xlWorkBook = xlApp.Workbooks.Open(txtFilePath.Text, System.IO.FileMode.Open, System.IO.FileAccess.Read);
var xlWorkBook1 = xlWorkBook.Sheets["SheetName"];
isCorrectFile = true;
}
catch (Exception e)
{
return false;
}
return isCorrectFile;
}
Issue:
When I click Close button at DialogNotFoundSheet form. Then FormSelectLanguage from still display. It repeats. How can resolve it?
Expected 2 forms can close
Thanks!
Update:
All References btnImport_Click:
UI:
I don't exactly know what you did with btnImport_Click, but if your purpose is to disable the function of a button at a time and to enable it at another time, actually you don't have to register or unregister the click event, you can simply set button's Enabled propety.
//btnImport.Click += btnImport_Click;
btnImport.Enabled = true;
//btnImport.Click -= btnImport_Click;
btnImport.Enabled = false;
My guess of the reason of this loop is that you have called += btnImport_Click many times, but -= btnImport_Click is never (or less) run.
For instance if you do:
btnImport.Click += btnImport_Click;
btnImport.Click += btnImport_Click;
Each time btnImport is clicked, btnImport_Click will get invoked twice.
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.
}
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 have two Forms in my application. They way I call Form 2 is like this:
Form 1:
private void btnTest_Click(object sender, EventArgs e)
{
DialogResult result = new System.Windows.Forms.DialogResult();
Add_Link addLink = new Add_Link();
result=addLink.ShowDialog();
if (result == System.Windows.Forms.DialogResult.OK)
{
//
}
}
Form 2:
private void btnAdd_Click(object sender, EventArgs e)
{
if(validURL(txtSubLink.Text))
{
HyperLink add = new HyperLink(txtSubLink.Text,txtSubText.Text,"URL");
this.build = add;
}
else
{
MessageBox.Show("Valid URL Needed! " + txtSubLink.Text, "ERROR");
}
}
My problem is if the user clicks the Add button, the error message shows(because the data is invalid or the textboxes are empty) BUT it closes the form. I only want the user to close the form and pass the data back if the two textboxes contain the proper data. If the two textboxes don't contain the proper data OR is empty, when the user clicks Add, the error message should show, and the Form 2 should remain open, How do I get that to happen...?
I suspect your btnAdd has its DialogResult property set to OK. Unset that, and then add this.DialogResult = DialogResult.OK in your event handler when you're satisfied with the input.
private void btnAdd_Click(object sender, EventArgs e)
{
if(validURL(txtSubLink.Text))
{
HyperLink add = new HyperLink(txtSubLink.Text,txtSubText.Text,"URL");
this.build = add;
this.DialogResult = DialogResult.OK;
}
else
{
MessageBox.Show("Valid URL Needed! " + txtSubLink.Text, "ERROR");
}
}
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();
}
}