Click to See Complete Forum and Search --> : how to set focus on an editbox


Rantic
March 16th, 2004, 09:57 AM
hi
I have a program which requires that a dialog appears with an edit box which takes a username, then ok is pressed and the dialog dissapears. My problem is that when the dialog box appears i have to click on the edit box before i can type anything into it. I want to have the cursor flashing and ready for input as soon as the dialog appears. how do i do this?

thanks,
r

Loada
March 16th, 2004, 10:52 AM
Try:

::SendMessage(hWnd, WM_SETFOCUS, reinterpret_cast<WPARAM>(hWnd), 0);

where hWnd is your editbox window handle.

Rantic
March 16th, 2004, 02:04 PM
thanks for the responce,
I tried putting that code at the start of my dialog proc code (the dialog with the edit box), but it didnt work. The dialog didn't appear and the program just exited.

there must be a simple way of doing this, but i cant find it newhere...
r

RussG1
March 16th, 2004, 05:35 PM
You should put that code in your WM_INITDIALOG handler in you DialogProc for your dialog with the edit control.

You could also use SetFocus() to do the same thing.

i.e.

// where hWnd is the HWND of your Edit control
SetFocus(hWnd);

VladimirF
March 16th, 2004, 08:54 PM
Is your dialog defind as a template in the resource file? Then the focus is set to the first enabled control, in the order of definition. Just place your edit box in the first position - no coding required.

Rantic
March 17th, 2004, 09:04 AM
my header file just has a list of resource id's . What are templates?

Rantic
March 17th, 2004, 09:38 AM
thanks 4 the help every1, what i ended up using was :

SetFocus(GetDlgItem(hWnd, IDC_EDIT1));

that worked perfectly. Thanks,
r

RussG1
March 17th, 2004, 09:51 AM
Templates would be the .rc file for your resources (if you are not creating your controls dynamically). The .rc is a text file and can be edited/created manually, but it usually created using a resource editor like Visual Studio's Dialog/Resource Editor. If you created the dialog in the the VS resource editor, than you can use CTRL-D to view/set the tab order. I think if you set the edit control to be the first in the tab order, than it will automatically get the focus after the dialog is created, and there would be no need to use SetFocus, etc.