|
-
September 29th, 2009, 01:58 PM
#1
Why does my app close ??
Hi All....
Simple question Im sure....
I have a simple dialog based app that has the usual OK and Quit buttons.
Ive just put a CEdit box on it and I find that when I press the return key, the app just exits.....
I need to be able to handle the text in the CEdit box when the user presses the 'return' key.
Can anyone tell me whats happening and how best to detect the 'return' key press in the edit box ??
Many Thanks all
Phill
-
September 29th, 2009, 02:22 PM
#2
Re: Why does my app close ??
Add the ES_WANTRETURN style to the edit box. Otherwise, the return is handled as an OK by the dialog.
Edit:
ES_WANTRETURN is only applicable to multiline edit controls. To handle RETURN with a simple edit box, you need to override OnOK() at the dialog level.
Last edited by hoxsiew; September 29th, 2009 at 02:25 PM.
-
September 30th, 2009, 07:13 AM
#3
Re: Why does my app close ??
You may want to remove the default property from the ok button too.
-
September 30th, 2009, 09:12 AM
#4
Re: Why does my app close ??
Hi hoxsiew
Thanks for your reply.
Yes.. your right, I need to over ride the OnOK() event.
Up till now I have used Visual studio 6, Im now using visual Studio 2008.
I have found the avaliable list of events for the dialog in the properties tab of the 'slide out' panel. However I cant find any way to add a handler for the OnOK() message.
Any idea how I do this please ??
Thanks again
phil
-
September 30th, 2009, 09:27 AM
#5
Re: Why does my app close ??
Any idea how I do this please ??
Add a button with identifier 'IDOK'.
-
September 30th, 2009, 09:46 AM
#6
Re: Why does my app close ??
 Originally Posted by Phill Heald
Hi hoxsiew
Thanks for your reply.
Yes.. your right, I need to over ride the OnOK() event.
Up till now I have used Visual studio 6, Im now using visual Studio 2008.
I have found the avaliable list of events for the dialog in the properties tab of the 'slide out' panel. However I cant find any way to add a handler for the OnOK() message.
Any idea how I do this please ??
Thanks again
phil
You can find OnOK in the Virtual Functions tab. OnOK and OnCancel are virtual functions and not message handlers.
-
September 30th, 2009, 10:29 AM
#7
Re: Why does my app close ??
Your dialog will typically also close upon hitting the Esc. key.
Override OnCancel() and make it do nothing to prevent this behaviour.
-
September 30th, 2009, 10:38 AM
#8
Re: Why does my app close ??
The question asked is how does he prevent it closing when he presses the enter key. Overriding OnOk won't help that problem as it will be triggered by the mouse too. He needs to remove the default property from the OK button, and as already suggested, add Want Return style to the edit control.
-
September 30th, 2009, 10:47 AM
#9
Re: Why does my app close ??
Overriding OnOK and OnCancel with empty functions, without calling CDialog::OnOK and CDialog::OnCancel, prevents dialog from closing by pressing Enter and Esc.
In this particular case, ES_WANTRETURN is enough.
-
September 30th, 2009, 11:00 AM
#10
Re: Why does my app close ??
Not entirely. ES_WANTRETURN changes the behaviour of the editcontrol in that you can actually use enter to generate a newline in your editcontrol. In a multiline edit, this can be an important behavioral change. It could be what he wants, but it could also NOT be what he wants.
It may appear to solve the issue, but it's not the 'right' solution to this problem. If the dialog gets changed to have another control that is not an editcontrol, you're back to square 1 since pressing enter when the other control is active will yet again close the dialog.
So the right solution:
If you have NO ok button -> override OnOk to do nothing.
if you have an OK button -> do not give OK (or any other button) the default button style
Esc closing the dialog has solutions also... albeit different ones...
If you have no Cancel button -> override OnCancel to do nothing
If you have a Cancel button -> Create an accellerator for Esc that maps to a dummy handler.
The above guideline will make sure normal behaviour still works when you have buttons and when those buttons have the focus and you hit enter (in which case you DO want them to execute the OK/Cancel handlers).
-
September 30th, 2009, 11:58 AM
#11
Re: Why does my app close ??
In YourClassDialog::OnOK, you can use GetFocus to determine if control is on OK button or not:
Code:
if ( GetFocus() == GetDlgItem(IDOK) )
;// process actual OK
else
;// Find if the focus was on desired control, and act appropriately.
If you have multiple controls, and want to move to next control (like we do using Tab key), you can use NextDlgCtrl.
-
September 30th, 2009, 12:26 PM
#12
Re: Why does my app close ??
guys.. thanks for your replys..
More of a discussion here than I was expecting !
Ive found the virtual functions and over ridden the OnOK and OnCancel functions.
It now seems that I cant close the app at all ! the 'X' button and the quit button as well as the 'esc' button are non responsive.
I need to handle the 'return' button press so I know when to parse the contents of the CEdit box (single line only).
Ajay, could I use your suggestion to trap the 'return' key press and handle the edit box contents there, also could I do something similar with the 'esc' key press ??
Thanks all
Phil
-
September 30th, 2009, 12:52 PM
#13
Re: Why does my app close ??
Don't override OnCancel - it is preventing you from closing the dialog box. If you must, you can:
Code:
void YourClass:OnCancel()
{
int nResponse = AfxMessageBox(...); // Ask the user for confirmation
if ( nResponse == confirm )
CDialog::OnCancel();
-
September 30th, 2009, 12:55 PM
#14
Re: Why does my app close ??
Ajay, could I use your suggestion to trap the 'return' key press and handle the edit box contents there, also could I do something similar with the 'esc' key press ??
OnOK and OnCancel implementations are for different purpose, though they close the dialog box eventually. The default implementation goes like this:
CDialog::OnOK() - EndDialog(IDOK)
CDialog::OnCancel() - EndDialog(IDCANCEL)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|