Jump to content

how update combo automatically after change a value


Go to solution Solved by ahmet,

Recommended Posts

Posted

mi my friend,

 

Just to ask how update combo automatically after change a value

i have this code

$Combo_template1 = GUICtrlCreateCombo(1,                            280,     170,     50,     21)

GUICtrlSetData($Combo_template1, "2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31|32|33|34|35|36|37|38|39|40|41|42|43|44|45|46|47|48")

 

and i have the code to detect changes

        case $Combo_template1

        $id_template = GUICtrlRead($Combo_template1)

 

the problem is when i send keys with paste or i write a value the

case $Combo_template1

not work

 

this code work only when i select the value of the scroll list

 

How can I add a code so that when I type a value or paste a value it goes through validation?

 

        case $Combo_template1

        $id_template = GUICtrlRead($Combo_template1)

 

best regards

Captura de pantalla 2024-12-15 004929.jpg

Posted

Hi, do you need something like following

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

Example()

Func Example()
    Global $sPreviousSelection="";new variable that holds info about last selection

    ; Create a GUI with various controls.
    Local $hGUI = GUICreate("Example", 300, 200)

    ; Create a combobox control.
    Local $idComboBox = GUICtrlCreateCombo("Item 1", 10, 10, 185, 20)
    Local $idButton_Close = GUICtrlCreateButton("Close", 210, 170, 85, 25)

    ; Add additional items to the combobox.
    GUICtrlSetData($idComboBox, "Item 2|Item 3", "Item 2")

    ; Display the GUI.
    GUISetState(@SW_SHOW, $hGUI)

    Local $sComboRead = ""

    ; Loop until the user exits.
    $sPreviousSelection=GUICtrlRead($idComboBox);update variable with the currently selected item
    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE, $idButton_Close
                ExitLoop

            Case $idComboBox
                $sComboRead = GUICtrlRead($idComboBox)
                ;MsgBox($MB_SYSTEMMODAL, "", "The combobox is currently displaying: " & $sComboRead, 0, $hGUI)
                If $sComboRead<>$sPreviousSelection Then
                    MsgBox(0,"Combobox","Selection changed")
                    $sPreviousSelection=$sComboRead
                EndIf
        EndSwitch
    WEnd

    ; Delete the previous GUI and all controls.
    GUIDelete($hGUI)
EndFunc   ;==>Example

When you ask for help it would be good to post runnable code.

Posted (edited)

This would add typed items into the combo box whenever something is typed/pasted after a certain amount of time.
The timer starts new whenever the data is different, and when it runs out, it writes the data into the combo box.
Currently i have set the timer on 2 seconds.

I took the base code from https://www.autoitscript.com/forum/topic/190196-add-element-in-a-combobox-if-its-not-in-the-combobox-data/
and added the timing functions.

 

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

; Create a Global variable to hold the combo data
Global $sComboData = ""

Global $hGui, $Cco, $cAdd

$hGui = GUICreate("test ", 250, 100, -1, -1, $GUI_SS_DEFAULT_GUI)
; Create combo and load with saved data
$Cco = GUICtrlCreateCombo("", 10, 10, 200, 20)
GUICtrlSetData($Cco, $sComboData)
; Create a button to add new data
$cAdd = GUICtrlCreateButton("Add", 10, 50, 80, 30)
GUISetState()

$oldC = GUICtrlRead($Cco)
$Ctimer = TimerInit()
$ccw = 0

While 1

    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $cAdd
            ; Add new data to the combo     
            _AddComboData()
                $oldC = GUICtrlRead($Cco)
            $ccw = 0
        Case $Cco
            ; Just to show that the data is selectable
            $ccw = 0
            $oldC = GUICtrlRead($Cco)
            ConsoleWrite("Selected " & $oldC)
    EndSwitch

    If $oldC <> GUICtrlRead($Cco) Then
        $oldC = GUICtrlRead($Cco)
        ConsoleWrite("Data is different" & @CRLF)
        $Ctimer = TimerInit()
        $ccw = 1
    EndIf

    If TimerDiff($Ctimer) > 2000 Then
        If $ccw > 0 Then
            ConsoleWrite("Writing data to the combo box" & @CRLF)
            _AddComboData()
            $oldC = GUICtrlRead($Cco)
            $ccw = 0
        EndIf
    EndIf
WEnd

Func _AddComboData()
    ; Read the content of the combo edit
    $sComboSel = GUICtrlRead($Cco)
    ; Check if in saved data
    If Not StringInStr($sComboData, "|" & $sComboSel) Then
        ; If not then add to saved data and load into the combo
        $sComboData &= "|" & $sComboSel
        GUICtrlSetData($Cco, $sComboData)
    EndIf

EndFunc   ;==>_AddComboData
Edited by Dan_555
typo

Some of my script sourcecode

Posted

Here is my contribution

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

Example()

Func Example()
    ; Create a GUI with various controls.
    Local $hGUI = GUICreate("Example", 300, 200)
    Local $Template1 = "1"

    Local $Combo_template1 = GUICtrlCreateCombo($Template1, 10, 10, 185, 20)
    Local $Button_Close = GUICtrlCreateButton("Close", 210, 170, 85, 25)
    Local $Button_Add1 = GUICtrlCreateButton("Set +1", 10, 170, 85, 25)
    GUICtrlSetData($Combo_template1, "2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31|32|33|34|35|36|37|38|39|40|41|42|43|44|45|46|47|48")
    GUISetState(@SW_SHOW, $hGUI)

    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE, $Button_Close
                ExitLoop

            Case $Button_Add1
                Local $Val = Int(GUICtrlRead($Combo_template1))
                GUICtrlSetData($Combo_template1, $Val >= 48 ? 48 : $Val + 1)

            Case $Combo_template1
                ConsoleWrite("$Combo_template1=" & GUICtrlRead($Combo_template1) & @CRLF)

        EndSwitch

         If $Template1 <> GUICtrlRead($Combo_template1) Then
            $Template1 = GUICtrlRead($Combo_template1)
            ConsoleWrite("$Template1=" & $Template1 & @CRLF)
         EndIf

    WEnd

    GUIDelete($hGUI)
EndFunc   ;==>Example

 

I know that I know nothing

Posted

Or based on notification :

#include <WinAPIConv.au3>
#include <GUIConstants.au3>
#include <Constants.au3>

Global $idComboBox

Example()

Func Example()
  Local $hGUI = GUICreate("Example", 300, 200)

  $idComboBox = GUICtrlCreateCombo("1", 10, 10, 185, 20)
  Local $idButton_Close = GUICtrlCreateButton("Close", 210, 170, 85, 25)

  GUICtrlSetData($idComboBox, "2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31|32|33|34|35|36|37|38|39|40|41|42|43|44|45|46|47|48")
  GUISetState()

  GUIRegisterMsg($WM_COMMAND, WM_COMMAND)

  While True
    Switch GUIGetMsg()
      Case $GUI_EVENT_CLOSE, $idButton_Close
        ExitLoop
    EndSwitch
  WEnd
EndFunc   ;==>Example

Func WM_COMMAND($hWnd, $iMsg, $wParam, $lParam)
  Local $idCtrl = _WinAPI_LoWord($wParam), $iCode = _WinAPI_HiWord($wParam)
  If $idCtrl = $idComboBox And ($iCode = $CBN_KILLFOCUS Or $iCode = $CBN_SELCHANGE) Then DoSomething($idCtrl)
  Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_COMMAND

Func DoSomething($idComboBox)
  Local Static $sSelection
  Local $sComboRead = GUICtrlRead($idComboBox)
  If $sSelection <> $sComboRead Then
    If GUICtrlSendMsg($idComboBox, $CB_FINDSTRINGEXACT, -1, $sComboRead) = -1 Then
      GUICtrlSendMsg($idComboBox, $CB_SETCURSEL, -1, 0)
      $sSelection = ""
      ConsoleWrite("Invalid data" & @CRLF)
    Else
      $sSelection = $sComboRead
      ConsoleWrite("The combobox is currently displaying: " & $sComboRead & @CRLF)
    EndIf
  EndIf
EndFunc   ;==>DoSomething

 

Posted
9 hours ago, ahmet said:

Hi, do you need something like following

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

Example()

Func Example()
    Global $sPreviousSelection="";new variable that holds info about last selection

    ; Create a GUI with various controls.
    Local $hGUI = GUICreate("Example", 300, 200)

    ; Create a combobox control.
    Local $idComboBox = GUICtrlCreateCombo("Item 1", 10, 10, 185, 20)
    Local $idButton_Close = GUICtrlCreateButton("Close", 210, 170, 85, 25)

    ; Add additional items to the combobox.
    GUICtrlSetData($idComboBox, "Item 2|Item 3", "Item 2")

    ; Display the GUI.
    GUISetState(@SW_SHOW, $hGUI)

    Local $sComboRead = ""

    ; Loop until the user exits.
    $sPreviousSelection=GUICtrlRead($idComboBox);update variable with the currently selected item
    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE, $idButton_Close
                ExitLoop

            Case $idComboBox
                $sComboRead = GUICtrlRead($idComboBox)
                ;MsgBox($MB_SYSTEMMODAL, "", "The combobox is currently displaying: " & $sComboRead, 0, $hGUI)
                If $sComboRead<>$sPreviousSelection Then
                    MsgBox(0,"Combobox","Selection changed")
                    $sPreviousSelection=$sComboRead
                EndIf
        EndSwitch
    WEnd

    ; Delete the previous GUI and all controls.
    GUIDelete($hGUI)
EndFunc   ;==>Example

When you ask for help it would be good to post runnable code.

Thanks for your responce.

when you write a value in the combo and the press enter or tab the code not working

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...