I'm having a problem in keeping the Parent Form to be opened till I close after using ShowDialog() .
I've been trying regarding this but couldn't get. I think there is something simple that I might have been missing. Can you please help me reg this?
The problem is,
I have Form 1, on pressing one button, Form 2 opens.
I do some validations in Form 2, and check for the validations. If the validation doesn't pass, I open a DialogBox form, with Retry and Cancel.
If I press Retry, The control should go back to the Form 2 and form 2 should not close.
If the press Cancel, both the DialogBox form and the Form 2 should close. Right now, regardless of what I press, both the forms close.
I have looked online and couldn't find any solution. Went through this solution, but both the forms are still closing for me.
Why does closing a nested child dialog also close the parent dialog?
My code:(Sample example scenario)
Form 1:
private void button1_Click(object sender, EventArgs e)
{
Form2 testForm = new Form2();
DialogResult dialogResult = new DialogResult();
dialogResult = testForm.ShowDialog(this);
if(dialogResult == DialogResult.OK)
{
//Do something
}
}
Form 2:
private void button1_Click(object sender, EventArgs e)
{
DialogResult validDataResult = MessageBox.Show("Invalid Data Entered. Please provide the correct data."
, "Data Management"
, MessageBoxButtons.RetryCancel);
if (validDataResult == DialogResult.Cancel)
{
this.Close();
}
}
in Form2.cs do your validation and then
(assuming validationOK is the true/false result of your checks)
if(validationOK == false)
{
// Ask retry or cancel to the user
if(DialogResult.Cancel == MessageBox.Show("Validation Fail", "Validation failed, press retry to do it againg", MessageBoxButtons.RetryCancel))
this.DialogResult.Cancel; // Set the dialog result on form2. This will close the form.
// if you have the validation done in a button_click event and that button has its
// property DialogResult set to something different than DialogResult.None, we need
// to block the form2 from closing itself.
// uncomment this code if the above comment is true
// else
// this.DialogResult = DialogResult.None;
}
You have to set the DialogResult of Form2 before you can call this.Close(). Otherwise, it remains the default. (The below code is only a guess of the actual double close logic since you did not specify that)
Form2.cs:
if (validDataResult == DialogResult.Cancel)
DialogResult = DialogResult.Cancel;
else
DialogResult = DialogResult.OK;
Close();
Form1.cs:
if(dialogResult == DialogResult.OK)
{
Close();
}
else
{
//Do something
}
Related
I am writing a WinForm application in C#. There is a Form A that opens Form C on Button click. Now, I want to add a password input screen Form B before opening Form C. Only if the password entered is correct will Form C open, and otherwise, show an error message. Form B just has a TextBox control and a Verify Button control.
/*Form A*/
FormB fb;
private void BtnClick(object sender, EventArgs e) {
DialogResult result;
//fb can open once at a time
if(fb == null){
fb = new FormB();
fb.FormClosed += new FormClosedEventHandler(FormB_FormClosed);
//This seems to be not working
result = fb.ShowDialog();
}
else //FormB already open. Do nothing
return;
//Only if password entered is correct, proceed
if(result == DialogResult.Yes){ //result == cancel
//Code to open Form C //Program never reaches here
}
}
//Upon closing FormB, reset it to null
private void FormB_FormClosed(object sender, FormClosedEventArgs e){
if(fb != null)
fb = null;
}
/* Form B */
private const string password = "xxxyyyzzz";
private void BtnPW_Click(object sender, EventArgs e){
bool result = Verify();
if(result){
BtnPW.DialogResult = DialogResult.Yes;
}
else{
MessageBox.Show("Error: Incorrect password");
BtnPW.DialogResult = DialogResult.No;
}
this.Close(); //Added
}
private bool Verify(){
if(TxtBox.Text == password)
return true;
else
return false;
}
Can someone tell me what is wrong with this code? It never reaches the second if statement in Form A.
Edit: Even if I enter the correct password and hit the button on Form B, result in Form A gets "DialogResult.Cancel`.
If you call the Form.Close method, then the DialogResult property of that form is set to DialogResult.Cancel even if you have set it to something else. To HIDE a form opened modally you just need to set the form's DialogResult property to anything but DialogResult.None.
Said that your code seems to be not the one usually used to handle a modal dialog.
ShowDialog is blocking your code, you don't exit from this call until the called form is closed or hidden, so you don't need to keep a global variable of FormB around and handle the FormClosed event handler of FormB in FormA.
private void BtnClick(object sender, EventArgs e)
{
using(FormB fb = new FormB())
{
// Here the code returns only when the fb instance is hidden
result = fb.ShowDialog();
if(result == DialogResult.Yes)
{
//open Form C
}
}
}
Now you should remove the call to Form.Close in the FormB code and set directly the DialogResult property of the FormB, do not try to change at this point the DialogResult property of the buttons, this will not work and you need a second click to hide the form, instead set directly the form's DialogResult property.
private const string password = "xxxyyyzzz";
private void BtnPW_Click(object sender, EventArgs e)
{
if(Verify())
this.DialogResult = DialogResult.Yes;
else
{
MessageBox.Show("Error: Incorrect password");
this.DialogResult = DialogResult.No;
}
}
In this way the form is hidden (not closed) and your code exits from the ShowDialog call in the FormA. Inside the using block you can still use the FormB instance to read its properties and take the appropriate paths. When your code exits from the using block the fb instance will be automatically closed and disposed out of existence.
I have a main form called Form_Main, when somebody wants to close it, it will close the entire application (by entire application I mean quitting other forms as well). Therefore I prepared a yes/no MessageBox that ask users if they really want to quit the form. Here's where I'm at:
private void Form_Main_FormClosed(object sender, FormClosedEventArgs e)
{
DialogResult result = MessageBox.Show("Are you sure?", "Confirmation", MessageBoxButtons.OKCancel);
if (result == DialogResult.OK)
{
Environment.Exit(1);
}
else
{
//does nothing
}
}
The "OK" button works. But when users clicks "Cancel", Form_Main is closed, but the application is still running (other forms are untouched). What should I replace //does nothing with?
Use the FormClosing event (instead of FormClosed), and then set e.Cancel = true:
private void Form_Main_FormClosing(object sender, FormClosingEventArgs e)
{
var result = MessageBox.Show("Are you sure?", "Confirmation", MessageBoxButtons.OKCancel);
e.Cancel = (result != DialogResult.OK);
}
The FormClosing event occurs before the form actually closes, so you still have a chance to cancel it. By the time you get to the FormClosed event, it's too late.
I have a program where I want it to ask a confirmation before close. It's just a simple form with the question and a yes and a no buttons. How can I send the information of which button was clicked back to the main form? All solutions I found were for communication with both forms opened, but when choosing a button in the second for it will close. Any tips or ideas?
The second type of form you described is similar to the MessageBox… You can use its direct implementation as a dialog. Untested Example :
DialogResult dr = MessageBox.Show("Are you Sure?",
"Confirm Exit?",
MessageBoxButtons.YesNo);
if (dr==DialogResult.Yes)
{
// Do work If Yes
}else //if( dr == DialogResult.No)
{
// Do work if No
}
See MSDN for MessageBox
I tend to do it this way.
Code for child form:
private bool _bDoSomething=false;
public bool showForm()
{
this.ShowDialog();
return _bDoSomething;
}
private void btnOK_Click(object sender, EventArgs e)
{
_bDoSomething=true;
this.Hide();
}
And then this sort of code for the parent form:
dlgMyForm dlgMyForm = new dlgMyForm();
if (dlgMyForm.showForm())
{
//do something
}
Declare a boolean value in main form as public
public Boolean check =false;
In the second form's FormClosing event do the following
private void Form2_FormClosing(Object sender, FormClosingEventArgs e)
{
DialogResult answer = MessageBox.Show("[Your text]",MessageBoxButtons.YesNo)
if(answer == DialogResult.Yes)
{
Form1.check=True; //if button yes is clicked
// set the form1 check variable to True and closes form2
}
else
{
Form1.check=False; //if button no is clicked
// set the form1 check variable to False and cancel form
// closing
e.Cancel=True;
}
}
Use the boolean variable check to do further processing in form1
I have this event in a new Form:
private void CrawlLocaly_FormClosed(object sender, FormClosedEventArgs e)
{
}
Im not sure if to use Closed or Closing.
The reason is that i want to check if the user shut down the program for example by just closing the application from the taskbar.
If he did close it from the taskbar mouse right click then close then i want it to close all the program and not only this Form.
How can i do it ?
Application.Exit();
Will shut down your application.
Im not really sure if you can detect if he closed it via rightmouse menu. As far as I know you can only see the reasons provided in the FormClosedEventArgs. FormClosing will provide you with same reasons.
Use this event:
private void Form_FormClosing(object sender, FormClosingEventArgs e)
{
if(e.CloseReason == CloseReason.UserClosing)
{
//close forms
Application.Exit();
}
}
There is no way to check whether user closed your form by clicking 'X' or through TaskBar or any other way as the result of CloseReason will always be CloseReason.UserClosing
Well I Stick with such issue also.
In mine app I have 2 forms, #1 main, #2 - for settings - if user close it i want to know save settings or not. Also if settings are null - close app not only form, if user click button save - I want to close (hide) #2 form.
So where is my solution we set tag value to 1 if click button save, so we will know "who" try to close form:
Predefined:
btnSave.Tag = 0;
On save button click event:
btnSave.Tag = 1;
this.Hide();
its will trigger onclose event:
private void frmLogin_FormClosing(object sender, FormClosingEventArgs e)
{
if (btnSave.Tag.ToString() == "0")
{
DialogResult dlg = MessageBox.Show("Do you want to exit without finished setup connection?", "Form", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (dlg == DialogResult.No)
{
e.Cancel = true;
}
else
{
e.Cancel = false;
this.Dispose();
Application.Exit();
}
}
else
{
this.Hide();
}
}
I am developing a application using windows forms. The project contains 3 forms: one login form which is the main form and two others which are child forms to the login form.
My problem is when want to close the total application by using Application.Exit() in form closing event my messagebox showing the dialog more than once.
1.This code in Login form i.e main form:
private void FrmLogIn_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult loginResult = MessageBox.Show("Do you want to close this application?","Close",MessageBoxButtons.YesNo,MessageBoxIcon.Warning);
if (loginResult == DialogResult.Yes)
{
Application.Exit();
}
}
2.AdminForm closing event which is child form to login form:
private void FrmAdmin_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult loginResult = MessageBox.Show("Do you want to close this application?","Close",MessageBoxButtons.YesNo,MessageBoxIcon.Warning);
if (loginResult == DialogResult.Yes)
{
Application.Exit();
}
}
3.Billoperations form closing event which is child form to login form:
private void FrmBillOperation_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult loginResult = MessageBox.Show("Do you want to close this application?","Close",MessageBoxButtons.YesNo,MessageBoxIcon.Warning);
if (loginResult == DialogResult.Yes)
{
Application.Exit();
}
}
When i click the close button in any form it will show MessageBox message only once. Please help me.
Make all FormClosing methods call a ApplicationShutdown function which handles this in a central place. You don't want to copy this code to every new form you create.
In this method you can check a boolean (watch for thread-safety) called for example IsShuttingDown. If it's already true, leave the method, otherwise you ask the question and start exiting.
The FormClosingEventArgs instance passed to the FormClosing event has a CloseReason property, which will be set to CloseReason.ApplicationExit when the Exit method of the Application class has been invoked: your handlers should check for this condition and if so then take no further action.
private void FrmLogIn_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.ApplicationExit)
return;
...
}
You can try with this code
FormCollection fc = Application.OpenForms;
if (fc!= null && fc.Count > 0)
{
for (int i = 1; i < fc.Count; i++)
{
if (fc!= null && fc.IsDisposed!= true)
{
fc.Dispose();
}
}
}
private void sh_interface_FormClosing(object sender, FormClosingEventArgs e)
{
if (MessageBox.Show("This will close down the whole application. Confirm?", "Close Application", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
foreach (Form f in Application.OpenForms)
{
if (!f.IsDisposed)
f.Dispose();
}
}
else
{
e.Cancel = true;
this.Activate();
}
}
This will close all forms including the hidden forms and the main form from Application.Run(new something())...Also this method works when invoked in inherited classes while coded in template class Form Closing event.