Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I have a form. In that I got to show a dialog (on some circumstances) with Text and a Cancel button. I want to catch the event of that button in my form Or know if that cancel button was clicked.

How can this be done ? I believe this should be possible but can't make out how ?

UPDATE : Actually not necessary that the user got to click "Cance" button. The dialog appears till the main form performs an activity. If the activity is finished, the dialog should be closed by main. If user clicks cancel then other code has to be performed by the main form. This is the exact scenario.

Thanks
Posted
Updated 31-May-11 6:21am
v2

if(DialogResult.Cancel != form.ShowDialog()) { ... }

Set the DialogResult property of the Cancel button to Cancel. You will also want an OK button (with a DialogResult of OK). And set the form's AcceptButton and CancelButton properties, too, so you can type in the field and press Enter to hit OK (or Esc for Cancel).
 
Share this answer
 
Comments
All Time Programming 31-May-11 12:03pm    
Actually not necessary that the user got to click "Cance" button. The dialog appears till the main form performs an activity. If the activity is finished, the dialog should be closed by main. If user clicks cancel then other code has to be performed by the main form. This is the exact scenario.
BobJanova 31-May-11 14:25pm    
It's not really a 'dialog' in the way the term is used in .Net, then. You need a non-modal form for that, if you really want 'the main form' to perform an activity. Actually, what you probably want is a thread (or a BackgroundWorkerTask or a .Net 4 Task) to do the processing, and when it is done it can signal some event that the dialog listens for to close itself (with a result other than Cancel).
All Time Programming 1-Jun-11 7:38am    
BobJanova, I got te backgroundworker thread running and also showing the dialog. Ofcourse,when the task called by DoWork is completed, it goes to RunWorkerCompleted and I close the child form over there. But, I also want the privilege to cancel the DoWork task when the Cancel button is clicked on the childForm. I tried thru EventHandler handling in the parentForm. But in childForm, in cancel_btn_click event if I write event DialogCanceled(this, new EventArgs()); it gives me error during compile time. How do I handle this ?
BobJanova 1-Jun-11 17:57pm    
If the cancel button has a DialogResult property (set to Cancel), the dialog will close, and control will be returned to the caller. Check the result of ShowDialog, and if it's Cancel, send a cancel message to the worker thread.
All Time Programming 2-Jun-11 3:54am    
Yes Bob that worked out. I just had to set a fld on cancelbtn as on closing the window normally (without clicking any thign) also the results received by ShowDialog was DialogReselt.Cancel. The flag helped me check if cancelbtn was pressed on the event is cauht normally. Thanks alot.
You can set AcceptButton[^] and CancelButton[^] property the button in your Form that handles the default Enter and ESC action.

AcceptButton and CancelButton property makes sense when used with Form.ShowDialog().
Remember that just setting the AcceptButton/CancelButton is not enough. This just tell which button should be invoked on ENTER/ESC. We have to set the DialogResult in the Button handler.

C#
button1.DialogResult = System.Windows.Forms.DialogResult.OK;
button2.DialogResult = System.Windows.Forms.DialogResult.Cancel;
form1.AcceptButton = button1;
form1.CancelButton = button2;


If you want to perform some validation before the Form close, you then handle Click[^] event on the button.

And when the form is closed you can check on the result.

C#
LoginForm login = new LoginForm();

DialogResult result = login.ShowDialog();

if (result == DialogResult.OK)
{
  MessageBox.Show("User has loggedin");
}
else
{
  MessageBox.Show("User failed validation");
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900