Stopping the Cancel button

M

MaxMax

I have a dialog box with a Cancel button that I want to be activable with
the ESC key. In the form the Cancel button is connected to the CancelButton
property. Now my problem is that the OnClick event of the button must be
able to "stop" the closing of the form: I want to change the text of the
Cancel button (that stops some working done in the background) to "Close"
and only the second time it's pressed it will close the dialog.... But I'm
not able to do it. How should I do?

Thanks!
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Are you sure you want to do this?
The user expect certain functionality which is shared by all the window
apps. if you change this the user can feel disoriented.

Use the onClick of the button, and use a flag if the flag is set the onclick
will close it, somethig like:

bool clickedonce = false;
void button_click( ..)
{
if (!clickedOnce)
{ cancelButton.Text = "Close";
clickedonce = true;
//do the other stuff
}
else
this.Close()

}
 
P

Peter Duniho

I have a dialog box with a Cancel button that I want to be activable with
the ESC key. In the form the Cancel button is connected to the
CancelButton
property. Now my problem is that the OnClick event of the button must be
able to "stop" the closing of the form: I want to change the text of the
Cancel button (that stops some working done in the background) to "Close"
and only the second time it's pressed it will close the dialog....

I'll second the motion that it's not a good idea to have a dual-purpose
button.

If the semantics of your dialog box really include: aborting background
processing, canceling the results, and accepting the results, then you
should have three buttons. The "Okay" and "Cancel" buttons should be
disabled while background processing is happening, if you don't want the
dialog to be dismissed while the processing is going on. This provides
the user with clear feedback that the dialog cannot be dismissed while the
processing is still happening (don't forget to disable the "X" close
button for the window :) ). Likewise, once the background processing has
stopped (because the user stopped it or it completed) then the "abort
processing" button should be disabled and the "Okay" and "Cancel" buttons
can be enabled.

Then you can include a ProcessDialogKey() method that will watch for the
escape key if the processing is still going on, and map that key to
aborting the processing rather than dismissing the dialog if and when it
is pressed.

Pete
 
L

Linda Liu [MSFT]

Hi Max,

Based on my understranding, you have a button on a form and set the
CancelButton property of the form to this button. When you show the form
modally and press the Esc key, the button is activated and the form is
closed. What you want is to change the text of the button to "Close" for
the first time you press the Esc key or click the button, and close the
form for the second time you do the same thing. If I'm off base, please
feel free to let me know.

If you are using VS05, a solution is to handle the FormClosing event of the
form. We could make use of the CloseReason property of the
FormClosingEventArgs parameter to judge what causes the form to be closed.
When we press the Esc key to click the button on the form, the CloseReason
property is None.

The following is a sample.

public partial class Form1 : Form
{
public Form4()
{
this.FormClosing += new
FormClosingEventHandler(Form1_FormClosing);
}
void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.None)
{
if (this.cancelBtn.Text != "Close")
{
this.cancelBtn.Text = "Close";
e.Cancel = true;
}
}
}
}

Hope this helps.
If my suggestion is not appropriate to your practice, please feel free to
let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
M

MaxMax

If you are using VS05, a solution is to handle the FormClosing event of
the
form. We could make use of the CloseReason property of the
FormClosingEventArgs parameter to judge what causes the form to be closed.
When we press the Esc key to click the button on the form, the CloseReason
property is None.
Thanks! It works!!!

--- bye
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top