Forcing fucus on edittext?

New to FreeBASIC? Post your questions here.
Post Reply
softfoot
Posts: 54
Joined: Aug 31, 2020 3:45

Forcing fucus on edittext?

Post by softfoot »

I have a simple GUI (in window9) that consists of two editboxes (one readonly) a combo box and 4 buttons.

Whenever I click the COMBOBOX or one of the buttons I want the focus to return to the writeable EDITTEXT box.

What is the best way to achieve this ??????

TIA
Dave
demosthenesk
Posts: 293
Joined: Jul 15, 2021 7:23
Location: Greece
Contact:

Re: Forcing fucus on edittext?

Post by demosthenesk »

See SetFocus()

example with button

Code: Select all

ButtonGadget(1,10,10,60,30,"button")
SetFocus(Gadgetid(1)) ' focus on the button
Löwenherz
Posts: 202
Joined: Aug 27, 2008 6:26
Location: Bad Sooden-Allendorf, Germany

Re: Forcing fucus on edittext?

Post by Löwenherz »

Hello softfoot.. Not so clear what you are meaning? Do you are using winapi SDK Style for your gui with winmain / wndproc? Whats your Intension with the combobox and edibix?

WM_SETFOCUS and WM_KILLFOCUS
(wndproc)

hWndFocus = GetFocus ()
for simple gui exists too

SetFocus can be used for an edit-control-window... .
demosthenesk
Posts: 293
Joined: Jul 15, 2021 7:23
Location: Greece
Contact:

Re: Forcing fucus on edittext?

Post by demosthenesk »

Löwenherz wrote: Sep 24, 2024 10:17 Hello softfoot.. Not so clear what you are meaning? Do you are using winapi SDK Style for your gui with winmain / wndproc? Whats your Intension with the combobox and edibix?

WM_SETFOCUS and WM_KILLFOCUS
(wndproc)

hWndFocus = GetFocus ()
for simple gui exists too

SetFocus can be used for an edit-control-window... .
he said that he have used window9 lib
Löwenherz
Posts: 202
Joined: Aug 27, 2008 6:26
Location: Bad Sooden-Allendorf, Germany

Re: Forcing fucus on edittext?

Post by Löwenherz »

OK here's an example for an editbox created by Jose Roca with afx include Files

In winmain there is a setfocus hEdit and below in wndproc Part Case EN_Setfocus

'

Code: Select all

#define UNICODE
#INCLUDE ONCE "Afx/CWindow.inc"
USING Afx

CONST IDC_EDIT1 = 1001
CONST IDC_EDIT2 = 1002

DECLARE FUNCTION WinMain (BYVAL hInstance AS HINSTANCE, _
                          BYVAL hPrevInstance AS HINSTANCE, _
                          BYVAL szCmdLine AS ZSTRING PTR, _
                          BYVAL nCmdShow AS LONG) AS LONG

   END WinMain(GetModuleHandleW(NULL), NULL, COMMAND(), SW_NORMAL)

' // Forward declaration
DECLARE FUNCTION WndProc (BYVAL hwnd AS HWND, BYVAL uMsg AS UINT, BYVAL wParam AS WPARAM, BYVAL lParam AS LPARAM) AS LRESULT

FUNCTION WinMain (BYVAL hInstance AS HINSTANCE, _
                  BYVAL hPrevInstance AS HINSTANCE, _
                  BYVAL szCmdLine AS ZSTRING PTR, _
                  BYVAL nCmdShow AS LONG) AS LONG
   ' // Set process DPI aware
   AfxSetProcessDPIAware
   ' // Create the main window
   DIM pWindow AS CWindow
   pWindow.Create(NULL, "Edit controls - Auto selection of text", @WndProc)
   ' // Sizes it by setting the wanted width and height of its client area
   pWindow.SetClientSize(500, 320)
   ' // Centers the window
   pWindow.Center

   ' // Add an Edit control
   DIM hEdit AS HWND = pWindow.AddControl("Edit", , IDC_EDIT1, "First Textbox", 20, 35, 305, 23)
   ' // Add another Edit control
   pWindow.AddControl("Edit", , IDC_EDIT2, "Second Textbox", 20, 75, 305, 23)
   ' // Set the focus in the first edit control
   SetFocus hEdit
   ' // Highlight the text
   Edit_SetSel(hEdit, 0, -1)

   ' // Dispatch Windows messages
   FUNCTION = pWindow.DoEvents(nCmdShow)

END FUNCTION

FUNCTION WndProc (BYVAL hwnd AS HWND, BYVAL uMsg AS UINT, BYVAL wParam AS WPARAM, BYVAL lParam AS LPARAM) AS LRESULT
   SELECT CASE uMsg
      CASE WM_COMMAND
         SELECT CASE GET_WM_COMMAND_ID(wParam, lParam)
            CASE IDCANCEL
               ' // If ESC key pressed, close the application sending an WM_CLOSE message
               IF GET_WM_COMMAND_CMD(wParam, lParam) = BN_CLICKED THEN
                  SendMessageW hwnd, WM_CLOSE, 0, 0
                  EXIT FUNCTION
               END IF
            CASE EN_SETFOCUS
               SELECT CASE GET_WM_COMMAND_ID(wParam, lParam)
                  ' Note: To deselect, use EM_SETSEL, -1, 0
                  CASE IDC_EDIT1
                     Edit_SetSel(GetDlgItem(hWnd, IDC_EDIT1), 0, -1)
                  CASE IDC_EDIT2
                     Edit_SetSel(GetDlgItem(hWnd, IDC_EDIT2), 0, -1)
               END SELECT
               EXIT FUNCTION
         END SELECT

    	CASE WM_DESTROY
         ' // End the application
         PostQuitMessage(0)
         EXIT FUNCTION
   END SELECT
   ' // Default processing of Windows messages
   FUNCTION = DefWindowProcW(hWnd, uMsg, wParam, lParam)
END FUNCTION
' ========================================================================================
demosthenesk
Posts: 293
Joined: Jul 15, 2021 7:23
Location: Greece
Contact:

Re: Forcing fucus on edittext?

Post by demosthenesk »

window9 has its own SetFocus() function

example

Code: Select all

ButtonGadget(1,10,10,60,30,"button")
SetFocus(Gadgetid(1)) ' focus on the button
you provide the handle of gadget to SetFocus and that's it, just change the ButtonGadget with EditorGadget
softfoot
Posts: 54
Joined: Aug 31, 2020 3:45

Re: Forcing fucus on edittext?

Post by softfoot »

Worked a treat :-)
Dave
Post Reply