Jump to content

GUICtrlOnChangeRegister


Mat
 Share

Recommended Posts

It's on old trick using WM_COMMAND, but it's now nice and easy to use.

Its syntax is very similar to GUICtrlSetOnEvent, however it does have an extra parameter for parameters for the function.

example:

#Include "GUIonchangeRegister.au3"

GUICreate ("test")
$EditID = GUICtrlCreateEdit ("", 2, 2, 300, 300)
GUICtrlonchangeRegister (-1, "MyFunc", "42|53")
GUISetState ()

While GUIGetMsg () <> -3
   Sleep (10)
WEnd

Func MyFunc ($i, $n)
   Tooltip ("You sent data!! params: " & $i & ", " & $n)
EndFunc ; ==> MyFunc

#Include-once
#Include<Array.au3>

Global $INTERNAL_CHANGE_REGISTER[1][3] = [[0, 0, 0]]
GUIRegisterMsg (0x0111, "__GUI_CHANGE_REGISTER_WM_COMMAND")

; #FUNCTION# ;====================================================================================================================
;
; Name...........: GUICtrlonchangeRegister
; Description ...: Registers an edit or input control to trigger a function when edited.
; Syntax.........: GUICtrlonchangeRegister($hEdit, $sFunc [, $sParams] )
; Parameters ....: $hEdit              - The handle to the edit control (can be a control ID)
;                  $sFunc              - The string function, as used in functions like HotKeySet + GUISetOnEvent.
;                  $sParams            - a "|" seperated list of parameters for the function (Optional, default = "")
; Return values .: Success             - 1
;                  Failure             - 0 And Sets @Error to 1
; Author ........: Mat
; Modified.......:
; Remarks .......: When using the pipe, use "\|" to stop it from being a seperator.
; Related .......: GUICtrlonchangeUnRegister
; Link ..........:
; Example .......: Yes
;
; ;===============================================================================================================================

Func GUICtrlonchangeRegister ($hEdit, $sFunc, $sParams = "")
   If $hEdit = -1 Then $hEdit = GUICtrlGetHandle (-1)
   If Not IsHwnd ($hEdit) Then $hEdit = GUICtrlGetHandle ($hEdit)
   If $hEdit = 0 Then Return SetError (1, 0, 0) ; $hEdit does not exist

   If $sFunc = "" Then Return GUICtrlonchangeUnRegister ($hEdit)

   ReDim $INTERNAL_CHANGE_REGISTER[UBound ($INTERNAL_CHANGE_REGISTER) + 1][3]
   $INTERNAL_CHANGE_REGISTER[0][0] += 1
   $INTERNAL_CHANGE_REGISTER[$INTERNAL_CHANGE_REGISTER[0][0]][0] = $hEdit
   $INTERNAL_CHANGE_REGISTER[$INTERNAL_CHANGE_REGISTER[0][0]][1] = $sFunc
   $INTERNAL_CHANGE_REGISTER[$INTERNAL_CHANGE_REGISTER[0][0]][2] = $sParams

   Return 1
Endfunc ; ==> GUICtrlonchangeRegister

; #FUNCTION# ;====================================================================================================================
;
; Name...........: GUICtrlonchangeUnRegister
; Description ...: UnRegisters an edit or input control so it no longer triggers a function
; Syntax.........: GUICtrlonchangeUnRegister($hEdit)
; Parameters ....: $hEdit              - The handle to the edit control (can be a control ID)
; Return values .: Success             - 1
;                  Failure             - 0 And Sets @Error to 1
; Author ........: Mat
; Modified.......:
; Remarks .......:
; Related .......: GUICtrlonchangeRegister
; Link ..........:
; Example .......: Yes
;
; ;===============================================================================================================================

Func GUICtrlonchangeUnRegister ($hEdit)
   If $hEdit = -1 Then $hEdit = GUICtrlGetHandle (-1)
   If Not IsHwnd ($hEdit) Then $hEdit = GUICtrlGetHandle ($hEdit)
   If $hEdit = 0 Then Return SetError (1, 0, 0) ; $hEdit does not exist

   Local $i = __GUI_CHANGE_REGISTER_GETINDEX ($hEdit)
   If $i < 0 Then Return SetError (1, 0, 0)

   _ArrayDelete ($INTERNAL_CHANGE_REGISTER, $i)
   $INTERNAL_CHANGE_REGISTER[0][0] -= 1

   Return 1
Endfunc ; ==> GUICtrlonchangeUnRegister

; #INTERNAL# ;====================================================================================================================
;
; Name...........: __GUI_CHANGE_REGISTER_WM_COMMAND
; Description ...: Handles the WM_COMMAND message and triggers events.
; Syntax.........: __GUI_CHANGE_REGISTER_WM_COMMAND ($hWnd, $msgID, $wParam, $lParam)
; Parameters ....: $hEdit              - The handle to the edit control (can be a control ID)
; Return values .: "GUI_RUNDEFMSG"
; Author ........: Mat
; Modified.......:
; Remarks .......:
; Related .......: GUICtrlonchangeRegister
; Link ..........:
; Example .......: Yes
;
; ;===============================================================================================================================

Func __GUI_CHANGE_REGISTER_WM_COMMAND ($hWnd, $msgID, $wParam, $lParam)
   If (BitShift($wParam, 16) <> 768) Then Return "GUI_RUNDEFMSG"
   Local $i = __GUI_CHANGE_REGISTER_GETINDEX ($lParam)
   If $i < 0 Then Return "GUI_RUNDEFMSG"

   If $INTERNAL_CHANGE_REGISTER[$i][1] = "" Then
      Call ($INTERNAL_CHANGE_REGISTER[$i][1])
   Else
      Local $avParams = StringRegExp ($INTERNAL_CHANGE_REGISTER[$i][2], "(.*?[^\\])?(?:\||$)", 3)
      For $x = Ubound ($avParams) - 2 to 0 Step -1
         $avParams[$x + 1] = $avParams[$x]
      Next
      $avParams[0] = "CallArgArray"
      Call ($INTERNAL_CHANGE_REGISTER[$i][1], $avParams)
   EndIf

   Return "GUI_RUNDEFMSG"
EndFunc ; ==> __GUI_CHANGE_REGISTER_WM_COMMAND

; #INTERNAL# ;====================================================================================================================
;
; Name...........: __GUI_CHANGE_REGISTER_GETINDEX
; Description ...: Returns the array index for an Edit HWnd, or -1.
; Syntax.........: __GUI_CHANGE_REGISTER_GETINDEX($hWnd)
; Parameters ....: $hWnd               - The handle to the edit control (can NOT be a control ID)
; Return values .: The index or -1
; Author ........: Mat
; Modified.......:
; Remarks .......:
; Related .......: __GUI_CHANGE_REGISTER_WM_COMMAND
; Link ..........:
; Example .......: Yes
;
; ;===============================================================================================================================

Func __GUI_CHANGE_REGISTER_GETINDEX ($hWnd)
   For $i = 1 to $INTERNAL_CHANGE_REGISTER[0][0]
      If $hWnd = $INTERNAL_CHANGE_REGISTER[$i][0] Then Return $i
   Next
   Return SetError (1, 0, -1)
Endfunc ; ==> __GUI_CHANGE_REGISTER_GETINDEX

and for those who don't like copying + pasting:

http://m-a-t.googlecode.com/files/GUIonchangeRegister.au3

Mat

Edited by Mat
Link to comment
Share on other sites

This looks awesome for my pet project which has two combo boxes, where one is dependent upon the other. I will try this out in the next couple of days as I've been somewhat stumped as to how to link the two.

I haven't tried it with combo boxes so I'm unsure if it will trigger the same message, It might need a bit of tweaking.

I have been looking for a way to tell what type of control a handle is pointing to, but without much success. If I can get that sorted then this could become a lot bigger and a lot more useful.

Mat

Link to comment
Share on other sites

  • Moderators

Mat,

Try _WinAPI_GetClassName to find out the class of a handle.

nikink,

If you want some interim code for dependent combos while Mat is working on his UDF, please take a look here:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GUIComboBox.au3>

$sFirst_Combo_Data = ""
$sSecond_Combo_List_1 = "|1|2|3|4|5"
$sSecond_Combo_List_2 = "|A|B|C|D|E"

; Create GUI
Global $hMain_Win = GUICreate("Dependent Combos", 500, 500)

; Create combo lists
Global $hFirst_Combo= GUICtrlCreateCombo("", 10, 40, 230, 20, $CBS_DROPDOWNLIST)
    GUICtrlSetData(-1, "List 1|List 2")
Global $hSecond_Combo = GUICtrlCreateCombo("First Select From First Combo", 250, 40, 230, 20, $CBS_DROPDOWNLIST)
    GUICtrlSetState(-1, $GUI_DISABLE)

; Create labels
GUICtrlCreateLabel("First Combo", 10, 10, 80, 20)
$hSecond_Label = GUICtrlCreateLabel("Second Combo", 250, 10, 80, 20)
GUICtrlSetState(-1, $GUI_DISABLE)

GUISetState(@SW_SHOW, $hMain_Win)

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch

    ; Fill Second Combo if UDF folder chosen/changed
    If GUICtrlRead($hFirst_Combo) <> "" And GUICtrlRead($hFirst_Combo) <> $sFirst_Combo_Data And _GUICtrlComboBox_GetDroppedState($hFirst_Combo) = False Then
        GUICtrlSetState($hSecond_Combo, $GUI_ENABLE)
        GUICtrlSetState($hSecond_Label, $GUI_ENABLE)
        Switch GUICtrlRead($hFirst_Combo)
            Case "List 1"
                GUICtrlSetData($hSecond_Combo, $sSecond_Combo_List_1)
            Case "List 2"
                GUICtrlSetData($hSecond_Combo, $sSecond_Combo_List_2)
        EndSwitch
        $sFirst_Combo_Data = GUICtrlRead($hFirst_Combo)
    EndIf

WEnd

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

  • 7 years later...

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