How to create a form with the equivalent of a Cancel Button

  • Thread starter Thread starter Roy Chastain
  • Start date Start date
R

Roy Chastain

If I just create a new form in CF, I get the default OK in the upper
right corner. That is fine, but what if I want the user to be able to
cancel his request.

What is the correct design and what is the correct mechanics to
provide a :"I don't really want to do this" type operation?

Thanks
 
I take it you are developing with a device running PPC 2000, as starting
with devices running PPC 2002 you must set MinimizeBox = False on the Form
in order to get the Ok button shown. So if you rely on the Ok button being
in the caption bar for your Form then you will need to set MinimizeBox =
False so that the default behavior for your app is the same on newer PPC
devices as well. But to answer your question (hopefully) you can handle the
Closing event for the Form and set the Cancel property on the event args
object that is passed to your event handler.

private void Form1_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{
if (MessageBox.Show("Do you want to close?", "Close",
MessageBoxButtons.OKCancel, MessageBoxIcon.Question,
MessageBoxDefaultButton.Button2) == DialogResult.Cancel)
{
e.Cancel = true;
}
}
 
You can add a button to the form and in its click handler set the
DialogResult of the form to DialogResult.Cancel to indicate a cancel
operation to a calling form. Unlike the OK button which is unique there is
no specific cancel equivalent.

Peter
 
Thanks to both of you.

After I posted, I found the DialogResult property of the button.
I simply set my cancel button to DialogResult.Cancel and my OK button
to DialogResult.OK, in the constructor for the form after the
InitializeComponent was called.

Then I do a ShowDialog and check result to DialogResult.OK or .cancel.
This appears to work. Not click handlers required etc. Is this BAD?
Did I miss something?

BTW. I am currently using emulator for 2002 and the OK appears by
default on the new form. It also returns a DialogResult.OK from the
ShowDialog.

Thanks
 
No, that sounds like the way to go. I think I just misunderstood exactly
what you were trying to do. I thought that you wanted to perhaps ask the
user if he/she wanted to cancel after the Ok was clicked. Something like
"Are you sure that you want to save changes?". This is when you would
require a Closing event handler.
If you are showing the Form with ShowDialog then you will get the Ok. But if
you show the Form using the Show method, with PPC 2002 and later, you will
get a "smart minimize button" by default instead. I wasn't sure what call
you were making (Show or ShowDialog) so I thought that I would at least
point out the difference in behavior from PPC 2000 to PPC 2002. In this case
it doesn't really sound like it is an issue.
 
One way for a consistent appearance on all versions (and I think also to
obey one of the design guidelines) if you have both an OK and Cancel button
you can disable the ControlBox of the form and neither the OK or X button
will be shown - you can then have your Ok and Cancel buttons at the bottom
of the form setup with the relevant DialogResults. A number use this sort of
approach (e.g. the new email account wizard)

Peter
 
And another that I might suggest, if it makes sense in the context of the
application, is to create two top level entries on the MainMenu - one that
says "Ok" and one that says "Cancel". You could also use the ToolBar and
specify pictures on the menu, if that might mean more to the end-user.
 
Again thanks to both Tim and Peter.

Tim,
The application has a main form that has a tab control (at least for
now). The form in question comes up in response to use action on the
main form. Are you suggesting that I add a menu to the secondary form
with an OK and Cancel on it?

I am not sure that I have seen this design on other programs, but I
will admit that I have very few add on programs on my PPC. Is this a
interface paradigm that the users are getting used to?
 
Is this a interface paradigm that the users
are getting used to?
Not that I know of :) This is just a way that I have used before as it does
free up some space on the Form for other things. If the Form is really tight
on space then it's an option to move the Ok/Cancel from Button's to the
MainMenu. I didn't mean to imply that this is standard, or even becoming
standard. It's simply an option that might be considered depending on what
you feel is best for the end-user in the given situation. Standards and
guidelines are there as a recommendation that allows users to sometimes
reduce the learning curve associated with an application. However, depending
on your target audience, you might decide to differ from the
recommendations. The end-user (customer) is the one that needs to live with
the application. So whatever seems right for them is probably the best
solution - and that is not always the standard way. I just wanted to present
another option that you might consider.
 
Again thanks Tim.
I understand and agree with what you are saying. My concept if there
is not a good reason then don't stray too far from the design
guidelines. It does reduce the learning curve.

I appreciate the idea of the OK and Cancel on the menu. As you say
that could be a good trick for a crowded form. I will keep that one
in the back of my mind. At this point I don't need it, this form is
very empty. I am sure that will change as time goes on.
 
Cool. I too, as most people, would choose standards if they're an option. I
would say go with standards if you can.
 
Back
Top