Jump to content

Get ComboBox value by using ENTER key


Valnurat
 Share

Recommended Posts

@Valnurat

Have you tried Send() key function?

Basically, value was get from the combobox when sending enter key.

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

_ComBox()

Func _ComBox()
    Local $hGUI = GUICreate("ComboBox", 300, 200)
    Local $idComboBox = GUICtrlCreateCombo("", 10, 10, 185, 20)
    GUICtrlSetData($idComboBox, "Item 1|Item 2|Item 3", "")
    GUISetState(@SW_SHOW, $hGUI)

    Local $sComboRead = ""

    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop

            Case $idComboBox
                $sComboRead = GUICtrlRead($idComboBox)
                If GUICtrlRead($idComboBox) = True Then
                   Send("{ENTER}")
                   MsgBox($MB_SYSTEMMODAL, "", "The combobox is currently displaying: " & $sComboRead, 0, $hGUI)
                EndIf
        EndSwitch
    WEnd
    GUIDelete($hGUI)
EndFunc

 

Edited by KickStarter15

Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare.

Link to comment
Share on other sites

Okay, I couldn't understand how I can use it in my code. I have this code:

#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.14.2
 Author:         myName

 Script Function:
    Template AutoIt script.

#ce ----------------------------------------------------------------------------

; Script Start - Add your code below here

#include <WindowsConstants.au3>
#include <ComboConstants.au3>
#include <GuiComboBox.au3>
#include <GUIConstantsEx.au3>
#include <GuiListView.au3>
#include <MsgBoxConstants.au3>
#Include <AD.au3>
#include <Array.au3>

Opt('MustDeclareVars', 1)

Global $aResult[0][2]
Local $aNames[4] = ["DKSO","DKKO","SELU","SEES"]
Local Const $iWidth = 300, $iHeight = 250, $iW = 85, $iH = 25
Local $hGUI = GUICreate("Find Computer Owner", $iWidth, $iHeight)
; Create a combobox control.
Global $idComboBox = GUICtrlCreateCombo("", 2, 2, 296, 20, BitOR($CBS_DROPDOWN, $CBS_AUTOHSCROLL, $WS_VSCROLL, $CBS_SORT)) ;$WS_VSCROLL
Local $idMylist = GUICtrlCreateListView("", 2, 114, 295, 100)
_GUICtrlListView_AddColumn($idMylist, "Computername", 296)
Local $idClos = GUICtrlCreateButton("Close", ($iWidth - $iW) / 2, ($iHeight - $iH) - 7 , $iW, $iH)
_AD_Open()
Local $sFill = ""
For $i = 0 to UBound($aNames) -1
    Local $aUserInfo = _AD_GetObjectsInOU("OU=Al,DC=ad,DC=al,DC=org","(&(objectCategory=Person)(objectclass=user)(sAMAccountName=" & $aNames[$i] & "*))","","distinguishedName,cn")
    if @error Then
        MsgBox(0,@error,@extended)
        Exit
    EndIf
    For $x = $aUserInfo[0][0] To 1 Step -1
        If $aUserInfo[$x][0] = "" Or StringInStr($aUserInfo[$x][0], "Resources") > 0 Or StringInStr($aUserInfo[$x][0], "Leavers") > 0 Or StringInStr($aUserInfo[$x][0], "Administration") > 0 Then
            _ArrayDelete($aUserInfo, $x)
        EndIf
    Next
    _ArrayConcatenate($aResult, $aUserInfo, 1)
Next
for $i = 0 To UBound($aResult) -1
    If $aResult[$i][0] <> "" Then
        $sFill &= $aResult[$i][1] & "|"
    EndIf
Next
;_ArrayDisplay($aResult, 'AD ' & UBound($aResult))
$sFill = StringTrimRight($sFill, 1)
_GUICtrlComboBox_BeginUpdate($idComboBox)
GUICtrlSetData($idComboBox, $sFill, "")
_GUICtrlComboBox_EndUpdate($idComboBox)
GUISetState(@SW_SHOW, $hGUI)
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")
Local $sComboRead = ""
; Loop until the user exits.
While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE, $idClos
            ExitLoop
        Case $idComboBox
            $sComboRead = GUICtrlRead($idComboBox)
            Local $iIdx = _ArraySearch($aResult,$sComboRead,0,0,0,0,1,1)
            Local $aComputerOwner = _AD_GetObjectsInOU("OU=al,DC=AD,DC=AL,DC=ORG","(&(objectclass=computer)(managedby=" & $aResult[$iIdx][0] & "))",Default,"cn")
;           _ArrayDelete($aComputerOwner,0)
;           _ArrayDisplay($aComputerOwner)
            _GUICtrlListView_SetItemCount($idMylist,UBound($aComputerOwner))
            If IsArray($aComputerOwner) Then
;           MsgBox(0,"",$sComboRead)
                For $i = 1 To UBound($aComputerOwner) - 1
                    GUICtrlCreateListViewItem($aComputerOwner[$i], $idMylist)
                Next
            Else
                GUICtrlSetData($idMylist, "No computer|")
            EndIf
    EndSwitch
WEnd
_AD_Close()

; Delete the previous GUI and all controls.
GUIDelete($hGUI)

Func WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg, $wParam
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndListView, $tInfo
    ; Local $tBuffer
    $hWndListView = $idMylist
    If Not IsHWnd($idMylist) Then $hWndListView = GUICtrlGetHandle($idMylist)

    $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")
    Switch $hWndFrom
        Case $hWndListView
            Switch $iCode
                Case $NM_DBLCLK ; Sent by a list-view control when the user double-clicks an item with the left mouse button
                    $tInfo = DllStructCreate($tagNMITEMACTIVATE, $lParam)
                    _DebugPrint("$NM_DBLCLK" & @CRLF & "--> hWndFrom:" & @TAB & $hWndFrom & @CRLF & _
                            "-->IDFrom:" & @TAB & $iIDFrom & @CRLF & _
                            "-->Code:" & @TAB & $iCode & @CRLF & _
                            "-->Index:" & @TAB & DllStructGetData($tInfo, "Index") & @CRLF & _
                            "-->SubItem:" & @TAB & DllStructGetData($tInfo, "SubItem") & @CRLF & _
                            "-->NewState:" & @TAB & DllStructGetData($tInfo, "NewState") & @CRLF & _
                            "-->OldState:" & @TAB & DllStructGetData($tInfo, "OldState") & @CRLF & _
                            "-->Changed:" & @TAB & DllStructGetData($tInfo, "Changed") & @CRLF & _
                            "-->ActionX:" & @TAB & DllStructGetData($tInfo, "ActionX") & @CRLF & _
                            "-->ActionY:" & @TAB & DllStructGetData($tInfo, "ActionY") & @CRLF & _
                            "-->lParam:" & @TAB & DllStructGetData($tInfo, "lParam") & @CRLF & _
                            "-->KeyFlags:" & @TAB & DllStructGetData($tInfo, "KeyFlags"))
                    ; No return value
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY

Func _Edit_Changed()
    _GUICtrlComboBox_AutoComplete($idComboBox)
EndFunc   ;==>_Edit_Changed

Func WM_COMMAND($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg
    Local $hWndFrom, $iIDFrom, $iCode, $hWndCombo
    If Not IsHWnd($idComboBox) Then $hWndCombo = GUICtrlGetHandle($idComboBox)
    $hWndFrom = $lParam
    $iIDFrom = BitAND($wParam, 0xFFFF) ; Low Word
    $iCode = BitShift($wParam, 16) ; Hi Word
    Switch $hWndFrom
        Case $idComboBox, $hWndCombo
            Switch $iCode
                Case $CBN_CLOSEUP ; Sent when the list box of a combo box has been closed
                    _DebugPrint("$CBN_CLOSEUP" & @CRLF & "--> hWndFrom:" & @TAB & $hWndFrom & @CRLF & _
                            "-->IDFrom:" & @TAB & $iIDFrom & @CRLF & _
                            "-->Code:" & @TAB & $iCode)
                    ; no return value
                Case $CBN_DBLCLK ; Sent when the user double-clicks a string in the list box of a combo box
                    _DebugPrint("$CBN_DBLCLK" & @CRLF & "--> hWndFrom:" & @TAB & $hWndFrom & @CRLF & _
                            "-->IDFrom:" & @TAB & $iIDFrom & @CRLF & _
                            "-->Code:" & @TAB & $iCode)
                    ; no return value
                Case $CBN_DROPDOWN ; Sent when the list box of a combo box is about to be made visible
                    _DebugPrint("$CBN_DROPDOWN" & @CRLF & "--> hWndFrom:" & @TAB & $hWndFrom & @CRLF & _
                            "-->IDFrom:" & @TAB & $iIDFrom & @CRLF & _
                            "-->Code:" & @TAB & $iCode)
                    ; no return value
                Case $CBN_EDITCHANGE ; Sent after the user has taken an action that may have altered the text in the edit control portion of a combo box
                    _DebugPrint("$CBN_EDITCHANGE" & @CRLF & "--> hWndFrom:" & @TAB & $hWndFrom & @CRLF & _
                            "-->IDFrom:" & @TAB & $iIDFrom & @CRLF & _
                            "-->Code:" & @TAB & $iCode)
                    _Edit_Changed()
                    ; no return value
                Case $CBN_EDITUPDATE ; Sent when the edit control portion of a combo box is about to display altered text
                    _DebugPrint("$CBN_EDITUPDATE" & @CRLF & "--> hWndFrom:" & @TAB & $hWndFrom & @CRLF & _
                            "-->IDFrom:" & @TAB & $iIDFrom & @CRLF & _
                            "-->Code:" & @TAB & $iCode)
                    ; no return value
                Case $CBN_ERRSPACE ; Sent when a combo box cannot allocate enough memory to meet a specific request
                    _DebugPrint("$CBN_ERRSPACE" & @CRLF & "--> hWndFrom:" & @TAB & $hWndFrom & @CRLF & _
                            "-->IDFrom:" & @TAB & $iIDFrom & @CRLF & _
                            "-->Code:" & @TAB & $iCode)
                    ; no return value
                Case $CBN_KILLFOCUS ; Sent when a combo box loses the keyboard focus
                    _DebugPrint("$CBN_KILLFOCUS" & @CRLF & "--> hWndFrom:" & @TAB & $hWndFrom & @CRLF & _
                            "-->IDFrom:" & @TAB & $iIDFrom & @CRLF & _
                            "-->Code:" & @TAB & $iCode)
                    ; no return value
                Case $CBN_SELCHANGE ; Sent when the user changes the current selection in the list box of a combo box
                    _DebugPrint("$CBN_SELCHANGE" & @CRLF & "--> hWndFrom:" & @TAB & $hWndFrom & @CRLF & _
                            "-->IDFrom:" & @TAB & $iIDFrom & @CRLF & _
                            "-->Code:" & @TAB & $iCode)
                    ; no return value
                Case $CBN_SELENDCANCEL ; Sent when the user selects an item, but then selects another control or closes the dialog box
                    _DebugPrint("$CBN_SELENDCANCEL" & @CRLF & "--> hWndFrom:" & @TAB & $hWndFrom & @CRLF & _
                            "-->IDFrom:" & @TAB & $iIDFrom & @CRLF & _
                            "-->Code:" & @TAB & $iCode)
                    ; no return value
                Case $CBN_SELENDOK ; Sent when the user selects a list item, or selects an item and then closes the list
                    _DebugPrint("$CBN_SELENDOK" & @CRLF & "--> hWndFrom:" & @TAB & $hWndFrom & @CRLF & _
                            "-->IDFrom:" & @TAB & $iIDFrom & @CRLF & _
                            "-->Code:" & @TAB & $iCode)
                    ; no return value
                Case $CBN_SETFOCUS ; Sent when a combo box receives the keyboard focus
                    _DebugPrint("$CBN_SETFOCUS" & @CRLF & "--> hWndFrom:" & @TAB & $hWndFrom & @CRLF & _
                            "-->IDFrom:" & @TAB & $iIDFrom & @CRLF & _
                            "-->Code:" & @TAB & $iCode)
                    ; no return value
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_COMMAND

Func _DebugPrint($s_Text, $sLine = @ScriptLineNumber)
    ConsoleWrite( _
            "!===========================================================" & @CRLF & _
            "+======================================================" & @CRLF & _
            "-->Line(" & StringFormat("%04d", $sLine) & "):" & @TAB & $s_Text & @CRLF & _
            "+======================================================" & @CRLF)
EndFunc   ;==>_DebugPrint

I know you can get the value by using the mouse, but I also want to use the ENTER key, because I'm using _GUICtrlComboBox_AutoComplete, so I can just write want I'm looking for.

Edited by Valnurat

Yours sincerely

Kenneth.

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...