Jump to content

Need combobox to accept enter (Solved)


Recommended Posts

I  possibly misunderstood the value of combo boxes, but I thought you could type a value, press enter and the combo box would store that value.

The reason for wanting this is to store a history of typed commands into the combo box (accepted by pressing enter).

 

After hours of trying to trap enter on a combobox I realized that the enter event was not registering.

I had code like this (which used to work on a input box), but it does not for a combobox (because it does not accept enter)

unc WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
    Local $nNotifyCode = BitShift($wParam, 16)
    Local $nID = BitAND($wParam, 0x0000FFFF)
    If $hWnd = $CmdForm Then
        If $nID = $ComboInput Then ; useed to work on a normal input box
            If _IsPressed('0D') Then
                ; enter was pressed, respond
                DoCmd()

Anyone able to help?

Edited by aiter
grammar
Link to comment
Share on other sites

Maybe something like:

#include <GUIConstantsEx.au3>
#include <Misc.au3>
#include <MsgBoxConstants.au3>

Global $idComboBox
Example()

Func Example()
    Local $hGUI = GUICreate("Example", 300, 200)
    $idComboBox = GUICtrlCreateCombo("Item 1", 10, 10, 185, 20)
    Local $idClose = GUICtrlCreateButton("Close", 210, 170, 85, 25)
        GUICtrlSetData($idComboBox, "Item 2|Item 3", "Item 2")
    GUISetState(@SW_SHOW, $hGUI)
    While 1
        If ControlGetFocus("Example", "") = "Edit1" Then
            If _IsPressed("0D") Then DoCmd()
        EndIf
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE, $idClose
                ExitLoop
        EndSwitch
    WEnd
    GUIDelete($hGUI)
EndFunc   ;==>Example

Func DoCmd()
    GUICtrlSetData($idComboBox, GUICtrlRead($idComboBox))
    MsgBox(4096, "ComboBox", "Enter Pressed")
EndFunc

 

Link to comment
Share on other sites

That works as I want Subz.  However will the combo box always be "Edit1"?  If I have multiple combo boxes or input boxes how can I reliably know what the name is?

Edited by aiter
Link to comment
Share on other sites

Cool, thanks a lot.  Saved me from a lot of work.  I was going to make the combo box an input box and have a list box below to show the history.  On enter in the input box, I would have intercepted the enter event and added to the list.  The user could recall the history by clicking and selecting on the list box which would populate the input box. 

Why will this not work though?

#include <GUIConstantsEx.au3>
#include <Misc.au3>
#include <MsgBoxConstants.au3>
#include <EditConstants.au3>


Global $idComboBox
Example()

Func Example()
    Local $hGUI = GUICreate("Example", 300, 200)
    $idComboBox = GUICtrlCreateCombo("Item 1", 10, 10, 185, 20)
    Local $idClose = GUICtrlCreateButton("Close", 210, 170, 85, 25)
        GUICtrlSetData($idComboBox, "Item 2|Item 3", "Item 2")
    GUISetState(@SW_SHOW, $hGUI)
    While 1
;        If ControlGetFocus($hGUI, "") = "Edit1" Then
;            If _IsPressed("0D") Then DoCmd()
;        EndIf
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE, $idClose
                ExitLoop
            case _IsPressed("0D")
                    if _GuiCtrlGetFocus($hGUI) = $idComboBox then
                        DoCmd()
                    endif
        EndSwitch
    WEnd
    GUIDelete($hGUI)
EndFunc   ;==>Example

Func DoCmd()
    GUICtrlSetData($idComboBox, GUICtrlRead($idComboBox))
    MsgBox(4096, "ComboBox", "Enter Pressed")
EndFunc

Func _GuiCtrlGetFocus($GuiRef)
    Local $hwnd = ControlGetHandle($GuiRef, "", ControlGetFocus($GuiRef))
    Local $result = DllCall("user32.dll", "int", "GetDlgCtrlID", "hwnd", $hwnd)
    Return $result[0]
EndFunc

Still, can't be greedy, I will use your working solution

Edit -

Actually this works, just had to reverse some code

 

#include <GUIConstantsEx.au3>
#include <Misc.au3>
#include <MsgBoxConstants.au3>
#include <EditConstants.au3>


Global $idComboBox
Example()

Func Example()
    Local $hGUI = GUICreate("Example", 300, 200)
    $idComboBox = GUICtrlCreateCombo("Item 1", 10, 10, -1, -1)
    Local $idClose = GUICtrlCreateButton("Close", 210, 170, 85, 25)
        GUICtrlSetData($idComboBox, "") ;Item 2|Item 3", "Item 2")
    GUISetState(@SW_SHOW, $hGUI)
    While 1
;        If ControlGetFocus($hGUI, "") = "Edit1" Then
;            If _IsPressed("0D") Then DoCmd()
;        EndIf
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE, $idClose
                ExitLoop
            case  _GuiCtrlGetFocus($hGUI) = $idComboBox
                if _IsPressed("0D")  then
                    DoCmd()
                endif
        EndSwitch
    WEnd
    GUIDelete($hGUI)
EndFunc   ;==>Example

Func DoCmd()
    local $val
    $val = GUICtrlRead($idComboBox)
    GUICtrlSetData($idComboBox, $val)
    ConsoleWrite("executing " & $val)
    sleep(100)  ; Important - thanks Mikell , otherwise you get repeats
  ;  MsgBox(4096, "ComboBox", "Enter Pressed")
EndFunc

Func _GuiCtrlGetFocus($GuiRef)
    Local $hwnd = ControlGetHandle($GuiRef, "", ControlGetFocus($GuiRef))
    Local $result = DllCall("user32.dll", "int", "GetDlgCtrlID", "hwnd", $hwnd)
    Return $result[0]
EndFunc

Thanks mikell for your contribution

 

Edited by aiter
Correction
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

×
×
  • Create New...