handling the RETURN key
-
Hi, When I press the enter key in a dialog box, the default is CDialog::OnOk (I guess). What I usually do is catch the enter key message and make it a tab key. But for this particular project, I want
OnButtonAdd
function to be executed if the focus is on an edit box and the Return key is pressed. I tried this:BOOL CApplyValue::PreTranslateMessage(MSG* pMsg)
{
if (pMsg->message== WM_KEYDOWN && pMsg->wParam==13)
{
CEdit *e;
e = (CEdit*)GetDlgItem(IDC_EDIT_All);
if (e->GetFocus() == this)
OnButtonAdd();
pMsg->wParam=9;
}
return CDialog::PreTranslateMessage(pMsg);
}No error come up with this, but it doesent work. So I must be doing it wrong. Please can you tell me the right way. Thanks, Tara_
Fortitudine Vincimus!_
-
Hi, When I press the enter key in a dialog box, the default is CDialog::OnOk (I guess). What I usually do is catch the enter key message and make it a tab key. But for this particular project, I want
OnButtonAdd
function to be executed if the focus is on an edit box and the Return key is pressed. I tried this:BOOL CApplyValue::PreTranslateMessage(MSG* pMsg)
{
if (pMsg->message== WM_KEYDOWN && pMsg->wParam==13)
{
CEdit *e;
e = (CEdit*)GetDlgItem(IDC_EDIT_All);
if (e->GetFocus() == this)
OnButtonAdd();
pMsg->wParam=9;
}
return CDialog::PreTranslateMessage(pMsg);
}No error come up with this, but it doesent work. So I must be doing it wrong. Please can you tell me the right way. Thanks, Tara_
Fortitudine Vincimus!_
-
Hi, When I press the enter key in a dialog box, the default is CDialog::OnOk (I guess). What I usually do is catch the enter key message and make it a tab key. But for this particular project, I want
OnButtonAdd
function to be executed if the focus is on an edit box and the Return key is pressed. I tried this:BOOL CApplyValue::PreTranslateMessage(MSG* pMsg)
{
if (pMsg->message== WM_KEYDOWN && pMsg->wParam==13)
{
CEdit *e;
e = (CEdit*)GetDlgItem(IDC_EDIT_All);
if (e->GetFocus() == this)
OnButtonAdd();
pMsg->wParam=9;
}
return CDialog::PreTranslateMessage(pMsg);
}No error come up with this, but it doesent work. So I must be doing it wrong. Please can you tell me the right way. Thanks, Tara_
Fortitudine Vincimus!_
When the edit control receives focus, use
CDialog::SetDefID()
on the Add button.
"Money talks. When my money starts to talk, I get a bill to shut it up." - Frank
"Judge not by the eye but by the heart." - Native American Proverb
-
When the edit control receives focus, use
CDialog::SetDefID()
on the Add button.
"Money talks. When my money starts to talk, I get a bill to shut it up." - Frank
"Judge not by the eye but by the heart." - Native American Proverb
-
CDialog::SetDefID()
Thats a wonderful thing I have learnt today. Thanks a lot._
Fortitudine Vincimus! -- modified at 10:52 Tuesday 18th July, 2006_
Tara14 wrote:
Thats a wonderful thing...
Only if it works, though. Let us know.
"Money talks. When my money starts to talk, I get a bill to shut it up." - Frank
"Judge not by the eye but by the heart." - Native American Proverb
-
Tara14 wrote:
Thats a wonderful thing...
Only if it works, though. Let us know.
"Money talks. When my money starts to talk, I get a bill to shut it up." - Frank
"Judge not by the eye but by the heart." - Native American Proverb
Yes!! It did. It worked. Because of this I also realised one more thing. In the settings for a button in a dialog box resource, there is an option 'Default button'. If you select that option for any button, when the enter key is pressed that particular button is activated. Earlier, I had thought that the option 'Default button' had to do with its appearence! Thanks. Tara_
Fortitudine Vincimus!_
-
Yes!! It did. It worked. Because of this I also realised one more thing. In the settings for a button in a dialog box resource, there is an option 'Default button'. If you select that option for any button, when the enter key is pressed that particular button is activated. Earlier, I had thought that the option 'Default button' had to do with its appearence! Thanks. Tara_
Fortitudine Vincimus!_
This is not the proper way of handling RETURN Key. If you have more than one edit box and if you want to handle RETURN in different ways for each edit box, then this method fails. Check the below code: BOOL CApplyValue::PreTranslateMessage(MSG* pMsg) { if (pMsg->message== WM_KEYDOWN && pMsg->wParam == VK_RETURN) { if (pMsg->hwnd == ::GetDlgItem(*this, IDC_EDIT1) //Replace with proper resource ID { //Do your processing for Edit box1 GetDlgItem(*this, IDC_EDIT2)->SetFocus(); //If you want to set focus to next edit box } else if(pMsg->hwnd == ::GetDlgItem(*this, IDC_EDIT2) { //Do your processing for Edit box2 } return TRUE; } return CDialog::PreTranslateMessage(pMsg); }
-
This is not the proper way of handling RETURN Key. If you have more than one edit box and if you want to handle RETURN in different ways for each edit box, then this method fails. Check the below code: BOOL CApplyValue::PreTranslateMessage(MSG* pMsg) { if (pMsg->message== WM_KEYDOWN && pMsg->wParam == VK_RETURN) { if (pMsg->hwnd == ::GetDlgItem(*this, IDC_EDIT1) //Replace with proper resource ID { //Do your processing for Edit box1 GetDlgItem(*this, IDC_EDIT2)->SetFocus(); //If you want to set focus to next edit box } else if(pMsg->hwnd == ::GetDlgItem(*this, IDC_EDIT2) { //Do your processing for Edit box2 } return TRUE; } return CDialog::PreTranslateMessage(pMsg); }