Jump to content

Combobox question (only do something if the element has changed)


Recommended Posts

Hi

I have a GUI setup (in OnEvent mode) with a combobox with 3 elements. I want to invoke some function only if the user clicked on a different control in the combobox then before.

I use the  $CBN_SELCHANGE constant in my WM_COMMAND function to check for the selection but i noticed something strange:

- when i click on the arrow top open the combobox, the $CBN_SELCHANGE is triggered

- when i select a value from the combobox, the $CBN_SELCHANGE is triggered 2 times

 

 

Here is my code:

 

GUIRegisterMsg($WM_COMMAND, "MY_WM_COMMAND")

Global Const $idcLastDataType = GUICtrlCreateCombo("",110,161,80,21,67,-1)
GUICtrlSetData(-1,"Value1|Value2")

Func MY_WM_COMMAND($hWnd, $iMsg, $iwParam, $ilParam)
    Local $idControl = _LoWord($iwParam)        ; LoWord - this gives the ControlID of the control which sent the message
    Local $nNotifyCode = _HiWord($iwParam)      ; HiWord - this gives the submessage that was sent
    Local $sWinTitle=WinGetTitle($hWnd)
    ;Local $hCtrl = $ilParam
    If $GUI_DEBUG Then
        ConsoleWrite("Window handle: " & $hWnd & " | Window title: " & $sWinTitle & " | Control ID: " & $idControl & @CRLF)
    EndIf

    Switch $idControl
        Case $idcLastDataType
                If $CBN_SELCHANGE Then _GUI_LastDataUpdate()
    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

Func _GUI_LastDataUpdate()
    MsgBox(0,"Test","Test)
    Return 1
EndFunc

 

Like i said i want the function to only be invoked if the item is a different one then the last time it was selected.

Link to comment
Share on other sites

  • Moderators

Thomymaster,

The only way I can see to do this would be to store the selection and compare when a new one is made:

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

Opt("GUIOnEventMode", 1)

$sSel = ""

$hGUI = GUICreate("Test", 500, 500)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")

$cCombo = GUICtrlCreateCombo("", 10, 10, 200, 20)
GUICtrlSetData($cCombo, "One|Two|Three|Four|Five")
GUICtrlSetOnEvent($cCombo, "_Combo_Event")

GUISetState()

While 1
    Sleep(10)
WEnd



Func _Combo_Event()
    $sNewSel = GUICtrlRead($cCombo)
    If $sNewSel <> $sSel Then
        $sSel = $sNewSel

        MsgBox($MB_SYSTEMMODAL, "Selection", $sSel)
    EndIf

EndFunc



Func _Exit()
    Exit
EndFunc

And why are you using Windows messaging to check for a selection? You can do it easily with standard OnEvent coding as I have shown.

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

Hi

 

Thanks for that. I already know that i can use OnEvent coding, but is there a reason not to select for events like button clicks or combox changes using the MY_WM_COMMAND function?

 

Is there an explanation why the combobox throws the $CB_SELCHANGE one time for opening and then 2 times after selecting a value?

Edited by Thomymaster
Link to comment
Share on other sites

  • Moderators

Thomymaster,

is there a reason not to select for events like button clicks or combox changes using the MY_WM_COMMAND function?

I would suggest that the answer is given by your question in the last line:

 Is there an explanation why the combobox throws the $CB_SELCHANGE one time for opening and then 2 times after selecting a value?

Why use message handlers and make life difficult for yourself when you can use native functions and make it easy? if you use a high level language then using its functions must be a better way.

As to the answer to that question, I have no idea - try looking on MSDN and see if there is an explanation.

M23

Edited by Melba23
Typo

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

  • 1 year later...

OK I realized this "Opt("GUIOnEventMode", 1)" is causing the UI to not recognize that other buttons are being pressed.

When I comment it out, my different Cases in While 1 are working, but then the _Combo_Event doesn't get triggered.

Any ideas???

Edited by XinYoung
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...