can't set input focus
-
Hi, I have a CDialog which contains several CEdit fields. I would like to be able to set one of those CEdit fields to have initial input focus. I'm trying to use the following code but it does nothing. I've made sure editField1 is valid at that point.
BOOL MyDialog::OnInitDialog() { CDialog::OnInitDialog(); editField1.SetFocus(); return true; }
Thoughts/hints? Thanks Hua-Ying -
Hi, I have a CDialog which contains several CEdit fields. I would like to be able to set one of those CEdit fields to have initial input focus. I'm trying to use the following code but it does nothing. I've made sure editField1 is valid at that point.
BOOL MyDialog::OnInitDialog() { CDialog::OnInitDialog(); editField1.SetFocus(); return true; }
Thoughts/hints? Thanks Hua-YingCalling SetFocus() in a dialog (or anywhere else) can be problematic. You could try editField1.PostMessage( WM_SETFOCUS ... ) which will have a far better chance of working. You need to test stuff like this on all patforms your app will run on. Neville Franks, Author of ED for Windows www.getsoft.com and Surfulater www.surfulater.com "Save what you Surf"
-
Calling SetFocus() in a dialog (or anywhere else) can be problematic. You could try editField1.PostMessage( WM_SETFOCUS ... ) which will have a far better chance of working. You need to test stuff like this on all patforms your app will run on. Neville Franks, Author of ED for Windows www.getsoft.com and Surfulater www.surfulater.com "Save what you Surf"
-
When I tried what you suggested, the CEdit field stops accepting user input(just beeps when you type)? It does put the caret in the edit box when the dialog starts up though. :confused:
editField1.PostMessage(WM_SETFOCUS);
Hua-Yinghyling wrote: When I tried what you suggested, the CEdit field to stop accepting input at all? It does put the caret in the edit box when the dialog starts up though. Have you specified the correct vaues for the PostMessage() wParam and lParam params. Look at WM_SETFOCUS in the Help for details. Neville Franks, Author of ED for Windows www.getsoft.com and Surfulater www.surfulater.com "Save what you Surf"
-
Hi, I have a CDialog which contains several CEdit fields. I would like to be able to set one of those CEdit fields to have initial input focus. I'm trying to use the following code but it does nothing. I've made sure editField1 is valid at that point.
BOOL MyDialog::OnInitDialog() { CDialog::OnInitDialog(); editField1.SetFocus(); return true; }
Thoughts/hints? Thanks Hua-YingWhen posting code snippets, it's beneficial to post them verbatim rather than typing them in manually. This is evident in the fact that you are returning a
bool
from aBOOL
method. SinceOnInitDialog()
is created by AppWizard or ClassWizard, you've no doubt noticed the comment that gets placed near the bottom of theOnInitDialog()
method:return TRUE; // return TRUE unless you set the focus to a control
"When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen
-
When posting code snippets, it's beneficial to post them verbatim rather than typing them in manually. This is evident in the fact that you are returning a
bool
from aBOOL
method. SinceOnInitDialog()
is created by AppWizard or ClassWizard, you've no doubt noticed the comment that gets placed near the bottom of theOnInitDialog()
method:return TRUE; // return TRUE unless you set the focus to a control
"When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen
AppWizard didn't create a OnInitDialog function for this class, I had to add it in by hand and didn't see the comment before. You're right, I should have been returning false for this function. The following code works for me:
BOOL MyDialog::OnInitDialog() { CDialog::OnInitDialog(); editField1.SetFocus(); return FALSE; }
Thanks! Hua-Ying -
hyling wrote: When I tried what you suggested, the CEdit field to stop accepting input at all? It does put the caret in the edit box when the dialog starts up though. Have you specified the correct vaues for the PostMessage() wParam and lParam params. Look at WM_SETFOCUS in the Help for details. Neville Franks, Author of ED for Windows www.getsoft.com and Surfulater www.surfulater.com "Save what you Surf"
Would you explain why you said this: Calling SetFocus() in a dialog (or anywhere else) can be problematic. I couldn't find anything discussions about this with google. Also, I could never get PostMessage to work right. I tried this:
mIDField.PostMessage(WM_SETFOCUS, (WPARAM)m_hWnd, 0);
According to the docs: Parameters -wParam Handle to the window that has lost the keyboard focus. This parameter can be NULL. -lParam This parameter is not used. Thanks Hua-Ying -
When posting code snippets, it's beneficial to post them verbatim rather than typing them in manually. This is evident in the fact that you are returning a
bool
from aBOOL
method. SinceOnInitDialog()
is created by AppWizard or ClassWizard, you've no doubt noticed the comment that gets placed near the bottom of theOnInitDialog()
method:return TRUE; // return TRUE unless you set the focus to a control
"When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen
;) Neville Franks, Author of ED for Windows www.getsoft.com and Surfulater www.surfulater.com "Save what you Surf"
-
Would you explain why you said this: Calling SetFocus() in a dialog (or anywhere else) can be problematic. I couldn't find anything discussions about this with google. Also, I could never get PostMessage to work right. I tried this:
mIDField.PostMessage(WM_SETFOCUS, (WPARAM)m_hWnd, 0);
According to the docs: Parameters -wParam Handle to the window that has lost the keyboard focus. This parameter can be NULL. -lParam This parameter is not used. Thanks Hua-Yinghyling wrote: Would you explain why you said this: Calling SetFocus() in a dialog (or anywhere else) can be problematic. I couldn't find anything discussions about this with google. You can and do get into race conditions where your code is trying to SetFocus and Windows is also trying to SetFocus(). A common mistake people make where this is evident is validating a control in its OnKillFocus() handler and calling SetFocus() when it is invalid to set the focus back to the control loosing focus. This won't work because upon return from OnKillFocus() windows will set the focus to the next control etc. PostMessage() is a way to handle this scenario. David has pointed out the correct answer however.:) Neville Franks, Author of ED for Windows www.getsoft.com and Surfulater www.surfulater.com "Save what you Surf"