Jump to content

ComboBox - function on Enter press


mike2003
 Share

Recommended Posts

How to register a function when I enter text and press the Enter button?

I tried this Case $MyCombo in main GUI Loop. But it works even when I just select a new value from the list. And this is not necessary. Only on Enter in Combo field

Edited by mike2003
Link to comment
Share on other sites

  • Moderators

mike2003,

I would see if the combo edit box has focus when Enter is pressed - like this:

#include <GUIConstantsEx.au3>
#include <WinAPISysWin.au3>
#include <GuiComboBox.au3>
#include <Misc.au3>

Global $tInfo

$hDLL = DllOpen("user32.dll")

$hGUI = GUICreate("Test", 500, 500)

$cCombo = GUICtrlCreateCombo("", 10, 10, 200, 20)
GUICtrlSetData($cCombo, "1|2|3")

GUISetState()

; Get handle of combo edit box
If _GUICtrlComboBox_GetComboBoxInfo($cCombo, $tInfo) Then
    $hComboEdit = DllStructGetData($tInfo, "hEdit")
Else
    ConsoleWrite("Fail" & @CRLF)
    Exit
EndIf


While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            DllClose($hDLL)
            Exit
    EndSwitch

    ; Check for Enter press
    If _IsPressed("0D", $hDLL) Then
        ; Check if combo edit box has focus
        If _WinAPI_GetFocus() = $hComboEdit Then
            ConsoleWrite(GUICtrlRead($cCombo) & @CRLF)
        EndIf
    EndIf
    ; Wait for Enter release - try without this to see why you need it! 
    While _IsPressed("0D", $hDLL)
        Sleep(250)
    WEnd

WEnd

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Or a little different approach :

#include <GUIConstants.au3>
#include <GuiComboBox.au3>

Local $hGUI = GUICreate("Test")
Local $cCombo = GUICtrlCreateCombo("", 10, 10, 200, 20)
GUICtrlSetData($cCombo, "1|2|3")
GUICtrlCreateLabel ("Edit :", 10, 50,50,20)
Local $cEdit = GUICtrlCreateInput ("", 50, 50, 150, 20)

Local $cButton = GUICtrlCreateButton("OK", 10, 100, 100, 25)
Local $cDummy = GUICtrlCreateDummy()

Local $aAccelKeys[1][2] = [["{ENTER}", $cDummy]]
GUISetAccelerators($aAccelKeys)

GUISetState()

Local $tInfo
_GUICtrlComboBox_GetComboBoxInfo($cCombo, $tInfo)
Local $hCombo = DllStructGetData($tInfo, "hEdit")

While 1
  Switch GUIGetMsg()
    Case $GUI_EVENT_CLOSE, $cButton
      ExitLoop
    Case $cDummy
      If ControlGetHandle($hGUI, "", ControlGetFocus ($hGUI)) = $hCombo Then
        ; put your code here
        ConsoleWrite(GUICtrlRead($cCombo) & @CRLF)
      EndIf
  EndSwitch
WEnd

 

Link to comment
Share on other sites

19 hours ago, Nine said:

different approach

Good!

21 hours ago, Melba23 said:

I would see if the combo edit box has focus when Enter is pressed

I have error - no file WinAPISysWin.au3

updt: works without it

Edited by mike2003
Link to comment
Share on other sites

  • Moderators

mike2003,

It sounds as if you are running an older AutoIt release - there was a major reorganising of the WinAPI UDFs by jpm for the last one. So you can probably manage with including just WinAPI.au3. 

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...