Jump to content

onEvent: Run function when combobox selection made


Recommended Posts

Hello,

I've searched for this one and can't find the answer. Any help appreciated.

I would like to run a function when a selection is made in the combobox (called $combo_Selection) in code below.

#include <ComboConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

$Form1 = GUICreate("Example Program", 315, 149, 192, 114)
$combo_Selection = GUICtrlCreateCombo("", 24, 24, 241, 25)
$label_RelatedInfo = GUICtrlCreateLabel("label_RelatedInfo", 24, 64, 87, 17)
GUISetState(@SW_SHOW)

Opt("GUIOnEventMode", 1)
GUISetOnEvent($GUI_EVENT_CLOSE, "Terminate")
GUICtrlSetData($combo_Selection, "Dog|Cat|Goat|Chicken","Dog")

While 1
 Sleep(1000)
WEnd

Func Terminate()
    Exit
EndFunc
Link to comment
Share on other sites

  • Moderators

gruntydatsun,

A bit of a kludge, but it works: :alien:

#include <ComboConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GuiComboBox.au3>

$fCurrState = False
$fOpened = False

$Form1 = GUICreate("Example Program", 315, 149, 192, 114)
$combo_Selection = GUICtrlCreateCombo("", 24, 24, 241, 25)

$label_RelatedInfo = GUICtrlCreateLabel("label_RelatedInfo", 24, 64, 87, 17)
GUISetState(@SW_SHOW)

Opt("GUIOnEventMode", 1)
GUISetOnEvent($GUI_EVENT_CLOSE, "Terminate")

GUICtrlSetData($combo_Selection, "Dog|Cat|Goat|Chicken", "Dog")

While 1
    Sleep(10)

    $fState = _GUICtrlComboBox_GetDroppedState($combo_Selection)
    If $fState <> $fCurrState Then
        Switch $fState
            Case True
                $fOpened = True
            Case False
                If $fOpened Then
                    $fOpened = False
                    MsgBox(0, "Function", "Run it now using " & GUICtrlRead($combo_Selection))
                EndIf
        EndSwitch
        $fCurrState = $fState
    EndIf

WEnd

Func Terminate()
    Exit
EndFunc   ;==>Terminate

There should be a message from the combo somewhere we can intercept - I will look around tomorrow. :huh2:

M23

Edit: Found it:

#include <ComboConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GuiComboBox.au3>

$fFlag = False

$Form1 = GUICreate("Example Program", 315, 149, 192, 114)
$combo_Selection = GUICtrlCreateCombo("", 24, 24, 241, 25)

$label_RelatedInfo = GUICtrlCreateLabel("label_RelatedInfo", 24, 64, 87, 17)
GUISetState(@SW_SHOW)

Opt("GUIOnEventMode", 1)
GUISetOnEvent($GUI_EVENT_CLOSE, "Terminate")

GUICtrlSetData($combo_Selection, "Dog|Cat|Goat|Chicken", "Dog")

GUIRegisterMsg($WM_COMMAND, "_WM_COMMAND")

While 1
    Sleep(10)

    ; Look for the flag
    If $fFlag Then
        $fFlag = False
        MsgBox(0, "Function", "Run it now using " & GUICtrlRead($combo_Selection))
    EndIf

WEnd

Func Terminate()
    Exit
EndFunc   ;==>Terminate

Func _WM_COMMAND($hWnd, $msg, $wParam, $lParam)
    ; if it is our combo and it is closed after have been opened
    If BitAND($wParam, 0x0000FFFF) = $combo_Selection And BitShift($wParam, 16) = $CBN_CLOSEUP Then
        ; Set a flag
        $fFlag = True
    EndIf
    Return $GUI_RUNDEFMSG
EndFunc

You need to use a flag becasue you do not want to run long functions from within the message handler. :ph34r:

All clear? ;)

Edited by Melba23

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

Here my version:

#include <ComboConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

$Form1 = GUICreate("Example Program", 315, 149, 192, 114)
$combo_Selection = GUICtrlCreateCombo("", 24, 24, 241, 25)
GUICtrlSetOnEvent(-1, "Combo")
$label_RelatedInfo = GUICtrlCreateLabel("label_RelatedInfo", 24, 64, 87, 17)
GUISetState(@SW_SHOW)

Opt("GUIOnEventMode", 1)
GUISetOnEvent($GUI_EVENT_CLOSE, "Terminate")
GUICtrlSetData($combo_Selection, "Dog|Cat|Goat|Chicken","")

While 1
 Sleep(1000)
WEnd

Func Combo()
    Call(GUICtrlRead($combo_Selection))
EndFunc

Func Dog()
    ConsoleWrite("Running dog" & @CRLF)
EndFunc

Func Cat()
    ConsoleWrite("Running cat" & @CRLF)
EndFunc

Func Goat()
    ConsoleWrite("Running goat" & @CRLF)
EndFunc

Func Chicken()
    ConsoleWrite("Running chicken" & @CRLF)
EndFunc

Func Terminate()
    Exit
EndFunc

Br,

UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

Thanks Melba23. That was exactly what I was looking for. Implemented it my program and it works great. :huh2:

Thanks for your version too UEZ. It's news to me that I can call a function by the name of a element of a combobox.

You get better support for AutoIT than you do for 99% of paid products. Thanks guys.

Link to comment
Share on other sites

  • Moderators

gruntydatsun,

And here is an even more elegant way to code it - in my opinion, of course. :huh2:

#include <ComboConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GuiComboBox.au3>

Opt("GUIOnEventMode", 1)

$Form1 = GUICreate("Example Program", 315, 149, 192, 114)
GUISetOnEvent($GUI_EVENT_CLOSE, "Terminate")

$combo_Selection = GUICtrlCreateCombo("", 24, 24, 241, 25)
GUICtrlSetData($combo_Selection, "Dog|Cat|Goat|Chicken", "Dog")

$label_RelatedInfo = GUICtrlCreateLabel("label_RelatedInfo", 24, 64, 87, 17)

; Create a dummy control to run the function
$hDummy = GUICtrlCreateDummy()
GUICtrlSetOnEvent(-1, "Do_It")

GUISetState(@SW_SHOW)

GUIRegisterMsg($WM_COMMAND, "_WM_COMMAND")

While 1
    Sleep(10)
WEnd

Func Do_It()
    MsgBox(0, "Function", "Run it now using " & GUICtrlRead($combo_Selection))
EndFunc

Func Terminate()
    Exit
EndFunc   ;==>Terminate

Func _WM_COMMAND($hWnd, $msg, $wParam, $lParam)
    ; if it is our combo and it is closed after have been opened
    If BitAND($wParam, 0x0000FFFF) = $combo_Selection And BitShift($wParam, 16) = $CBN_CLOSEUP Then
        ; Fire the dummy
        GUICtrlSendToDummy($hDummy)
    EndIf
    Return $GUI_RUNDEFMSG
EndFunc

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