Klick bei ListBox ermitteln

  • Hi

    Hab mal ne Frage.

    Ich hätte gern, dass, wenn man auf ein Item in einer Listbox klick, der index des item ermittelt wird, und etwas passiert.
    Man soll also nicht extra auf einen Button klicken müssen.


    Hab schon die Hilfe durschsucht, aber nix gefunden.

    hat da jemand ne Idee?

  • ich hab mal das Beispiel aus der Hilfe um die Func _DblClick erweitert...

    Spoiler anzeigen
    [autoit]

    #AutoIt3Wrapper_au3check_parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
    #include <GUIListBox.au3>
    #include <GuiConstantsEx.au3>
    #include <WindowsConstants.au3>
    #include <Constants.au3>

    [/autoit] [autoit][/autoit] [autoit]

    Opt('MustDeclareVars', 1)

    [/autoit] [autoit][/autoit] [autoit]

    $Debug_LB = False ; Check ClassName being passed to ListBox functions, set to True and use a handle to another control to see it work

    [/autoit] [autoit][/autoit] [autoit]

    Global $hListBox

    [/autoit] [autoit][/autoit] [autoit]

    _Main()

    [/autoit] [autoit][/autoit] [autoit]

    Func _Main()
    Local $hGUI

    [/autoit] [autoit][/autoit] [autoit]

    ; Create GUI
    $hGUI = GUICreate("(UDF Created) List Box Create", 400, 296)
    $hListBox = _GUICtrlListBox_Create($hGUI, "String upon creation", 2, 2, 396, 296)
    GUISetState()

    [/autoit] [autoit][/autoit] [autoit]

    MsgBox(4160, "Information", "Adding Items")

    [/autoit] [autoit][/autoit] [autoit]

    GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")

    [/autoit] [autoit][/autoit] [autoit]

    ; Add files
    _GUICtrlListBox_BeginUpdate($hListBox)
    _GUICtrlListBox_ResetContent($hListBox)
    _GUICtrlListBox_InitStorage($hListBox, 100, 4096)
    _GUICtrlListBox_Dir($hListBox, @WindowsDir & "\win*.exe")
    _GUICtrlListBox_AddFile($hListBox, @WindowsDir & "\Notepad.exe")
    _GUICtrlListBox_Dir($hListBox, "", $DDL_DRIVES)
    _GUICtrlListBox_Dir($hListBox, "", $DDL_DRIVES, False)
    _GUICtrlListBox_EndUpdate($hListBox)

    [/autoit] [autoit][/autoit] [autoit]

    ; Loop until user exits
    Do
    Until GUIGetMsg() = $GUI_EVENT_CLOSE
    EndFunc ;==>_Main

    [/autoit] [autoit][/autoit] [autoit]

    Func WM_COMMAND($hWnd, $iMsg, $iwParam, $ilParam)
    #forceref $hWnd, $iMsg
    Local $hWndFrom, $iIDFrom, $iCode, $hWndListBox
    If Not IsHWnd($hListBox) Then $hWndListBox = GUICtrlGetHandle($hListBox)
    $hWndFrom = $ilParam
    $iIDFrom = BitAND($iwParam, 0xFFFF) ; Low Word
    $iCode = BitShift($iwParam, 16) ; Hi Word

    [/autoit] [autoit][/autoit] [autoit]

    Switch $hWndFrom
    Case $hListBox, $hWndListBox
    Switch $iCode
    Case $LBN_DBLCLK ; Sent when the user double-clicks a string in a list box
    _DebugPrint("$LBN_DBLCLK" & @LF & "--> hWndFrom:" & @TAB & $hWndFrom & @LF & _
    "-->IDFrom:" & @TAB & $iIDFrom & @LF & _
    "-->Code:" & @TAB & $iCode)
    ; no return value
    _DblClick()
    Case $LBN_ERRSPACE ; Sent when a list box cannot allocate enough memory to meet a specific request
    _DebugPrint("$LBN_ERRSPACE" & @LF & "--> hWndFrom:" & @TAB & $hWndFrom & @LF & _
    "-->IDFrom:" & @TAB & $iIDFrom & @LF & _
    "-->Code:" & @TAB & $iCode)
    ; no return value
    Case $LBN_KILLFOCUS ; Sent when a list box loses the keyboard focus
    _DebugPrint("$LBN_KILLFOCUS" & @LF & "--> hWndFrom:" & @TAB & $hWndFrom & @LF & _
    "-->IDFrom:" & @TAB & $iIDFrom & @LF & _
    "-->Code:" & @TAB & $iCode)
    ; no return value
    Case $LBN_SELCANCEL ; Sent when the user cancels the selection in a list box
    _DebugPrint("$LBN_SELCANCEL" & @LF & "--> hWndFrom:" & @TAB & $hWndFrom & @LF & _
    "-->IDFrom:" & @TAB & $iIDFrom & @LF & _
    "-->Code:" & @TAB & $iCode)
    ; no return value
    Case $LBN_SELCHANGE ; Sent when the selection in a list box has changed
    _DebugPrint("$LBN_SELCHANGE" & @LF & "--> hWndFrom:" & @TAB & $hWndFrom & @LF & _
    "-->IDFrom:" & @TAB & $iIDFrom & @LF & _
    "-->Code:" & @TAB & $iCode)
    ; no return value
    Case $LBN_SETFOCUS ; Sent when a list box receives the keyboard focus
    _DebugPrint("$LBN_SETFOCUS" & @LF & "--> hWndFrom:" & @TAB & $hWndFrom & @LF & _
    "-->IDFrom:" & @TAB & $iIDFrom & @LF & _
    "-->Code:" & @TAB & $iCode)
    ; no return value
    EndSwitch
    EndSwitch
    ; Proceed the default Autoit3 internal message commands.
    ; You also can complete let the line out.
    ; !!! But only 'Return' (without any value) will not proceed
    ; the default Autoit3-message in the future !!!
    Return $GUI_RUNDEFMSG
    EndFunc ;==>WM_COMMAND

    [/autoit] [autoit][/autoit] [autoit]

    Func _DebugPrint($s_text)
    $s_text = StringReplace($s_text, @LF, @LF & "-->")
    ConsoleWrite("!===========================================================" & @LF & _
    "+===========================================================" & @LF & _
    "-->" & $s_text & @LF & _
    "+===========================================================" & @LF)
    EndFunc ;==>_DebugPrint

    [/autoit] [autoit][/autoit] [autoit]

    Func _DblClick()
    Local $k = _GUICtrlListBox_GetCurSel($hListBox)
    MsgBox(0, '', "Index: "& $k & @CRLF & "Inhalt: "&_GUICtrlListBox_GetText($hListBox, $k))
    EndFunc

    [/autoit]

    MfG Schnuffel

    "Sarkasmus ist die niedrigste Form des Witzes, aber die höchste Form der Intelligenz."
    Val McDermid

    ein paar Infos ...

    Wer mehr als "nur" Hilfe benötigt, kann sich gern im Forum "Programmieranfragen" an uns wenden. Wir helfen in allen Fällen, die die Forenregeln zulassen.

    Für schnelle Hilfe benötigen wir ein ! lauffähiges ! Script, dass wir als Demonstration des Problems testen können. Wer von uns erwartet ein Teilscript erstmal lauffähig zu bekommen, der hat
    1. keine wirkliche Not
    2. keinen Respekt vor Menschen die ihm in ihrer Freizeit Ihre Hilfe anbieten
    3. oder ist einfach nur faul und meint wir coden das für ihn

    In solchen Fällen erlaube ich mir, die Anfrage einfach zu ignorieren. ;)

  • Also Sry, aber ne erweiterung bringt mir relativ wenig, wenn ich nicht weis, wie ich da hin komm.

    Was ich mache, wenn der klick erkannt wurde, weis ich ja, nur was ich nicht weis ist, wie der klick erkannt wird.
    Soweit bin ich leider noch nicht mit meinem know how xD

    Brauch nur ne etwas einfachere erklärung, wie ich zu diesem $LBN_DBLCLK komme.
    Ist das ganze drum herum in dem Beispielscript nötig, oder nicht?

    • Offizieller Beitrag

    Du brauchst das Registrieren der GUIMessage und die registrierte Funktion:

    [autoit]

    GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")

    [/autoit][autoit][/autoit][autoit][/autoit][autoit]

    Func WM_COMMAND($hWnd, $iMsg, $iwParam, $ilParam)
    #forceref $hWnd, $iMsg
    Local $hWndFrom, $iIDFrom, $iCode, $hWndListBox
    If Not IsHWnd($hListBox) Then $hWndListBox = GUICtrlGetHandle($hListBox)
    $hWndFrom = $ilParam
    $iIDFrom = BitAND($iwParam, 0xFFFF) ; Low Word
    $iCode = BitShift($iwParam, 16) ; Hi Word
    Switch $hWndFrom
    Case $hListBox, $hWndListBox
    Switch $iCode
    Case $LBN_DBLCLK ; Sent when the user double-clicks a string in a list box
    ; Funktion für DoubleClick ausführen
    EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
    EndFunc ;==>WM_COMMAND

    [/autoit]
  • Oder man macht es auf die einfache Art und Weise :D

    [autoit]


    #include <GUIListBox.au3>
    #region - GUI Create
    Global $Selection = -1
    $GUI = GUICreate('Test')
    $Listbox = _GUICtrlListBox_Create($GUI,"",20,20)
    For $i = 0 to 9
    _GUICtrlListBox_AddString($Listbox,"Test "&$i)
    Next
    GUISetState()
    #endregion

    [/autoit][autoit][/autoit][autoit]

    #region - GUI SelectLoop
    While 1 * sleep(10)
    $msg = GUIGetMsg()
    Select
    Case $msg = -3
    Exit
    Case _GUICtrlListBox_GetCurSel($Listbox) <> $Selection
    MsgBox("","","Index: "&_GUICtrlListBox_GetCurSel($Listbox))
    $Selection = _GUICtrlListBox_GetCurSel($Listbox)
    EndSelect
    WEnd
    #endregion

    [/autoit][autoit][/autoit][autoit][/autoit]
  • den Doppelklick hab ich eingeführt :rofl:

    MfG Schnuffel

    "Sarkasmus ist die niedrigste Form des Witzes, aber die höchste Form der Intelligenz."
    Val McDermid

    ein paar Infos ...

    Wer mehr als "nur" Hilfe benötigt, kann sich gern im Forum "Programmieranfragen" an uns wenden. Wir helfen in allen Fällen, die die Forenregeln zulassen.

    Für schnelle Hilfe benötigen wir ein ! lauffähiges ! Script, dass wir als Demonstration des Problems testen können. Wer von uns erwartet ein Teilscript erstmal lauffähig zu bekommen, der hat
    1. keine wirkliche Not
    2. keinen Respekt vor Menschen die ihm in ihrer Freizeit Ihre Hilfe anbieten
    3. oder ist einfach nur faul und meint wir coden das für ihn

    In solchen Fällen erlaube ich mir, die Anfrage einfach zu ignorieren. ;)

  • push...

    keine lustn thread aufzumachen.

    würde gerne beim $LBN_SELCHANGE ermitteln welches item ich grad angeklickt habe. am besten direkt den text bekommen per selection id, aber irgendwie möchte das nicht.

    [autoit]

    Func WM_COMMAND($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg
    Local $hWndFrom, $iIDFrom, $iCode, $hWndListBox
    If Not IsHWnd($g_hListBox) Then $hWndListBox = GUICtrlGetHandle($g_hListBox)
    $hWndFrom = $lParam
    $iIDFrom = BitAND($wParam, 0xFFFF) ; Low Word
    $iCode = BitShift($wParam, 16) ; Hi Word
    Switch $hWndFrom
    Case $g_hListBox, $hWndListBox
    Switch $iCode
    Case $LBN_SELCHANGE ; Sent when the selection in a list box has changed
    $x = _GUICtrlListBox_GetSelItemsText($g_hListBox)
    msgbox(0,"",$x[1])
    EndSwitch
    EndSwitch
    ; Proceed the default AutoIt3 internal message commands.
    ; You also can complete let the line out.
    ; !!! But only 'Return' (without any value) will not proceed
    ; the default AutoIt3-message in the future !!!
    Return $GUI_RUNDEFMSG
    EndFunc ;==>WM_COMMAND

    [/autoit]

    hat sich erledigt ! habs.

    [autoit]

    Case $LBN_SELCHANGE ; Sent when the selection in a list box has changed)
    $id = _GUICtrlListBox_GetAnchorIndex($g_hListBox)
    $x =_GUICtrlListBox_GetText($g_hListBox,$id)
    msgbox(0,"",$x)

    [/autoit]

    Einmal editiert, zuletzt von Kev (22. April 2015 um 14:35)