Jump to content

Retrieve data typed in a combobox with enter


Recommended Posts

I am trying to make a combobox that will respond when typing something in it and pressing enter. The same way an inputbox works, but with a dropdown.

The reason I want this is for a sort of auto completion function. You type something in the combobox, press enter and then select the value you where looking for.

I got it to work with a "search" button and I got it to work by using an input box and replacing it with a combo, but the search button was not very intuïtive and replacing the input did not allow me to do more then one search.

I thought the $SS_NOTIFY style was going to save my day, but it doesn't seem to do anything.

Setting "enter" as a hotkey works, but I have multiple combo boxes and couldn't figure out how to check wich one has the focus.

Here is an example script of what I'm trying to make this work for:

#include <GUIConstants.au3>
GUICreate("test",90,70)
$combo = GUICtrlCreateCombo("",20,10,50,20,BitOR(Default,$SS_NOTIFY))
GUICtrlSetData(-1,"0|1|2|3|4|5|6|7|8|9")
GUISetState()

while 1
    $msg = GUIGetMsg()
    If $msg = -3 Then 
        Exit
    ElseIf $msg=$combo Then
        MsgBox(0,"Test","Combo pressed")    
    EndIf
WEnd

Any ideas would be greatly appreciated.

Link to comment
Share on other sites

Help file:

_GUICtrlComboBox_AutoComplete

--------------------------------------------------------------------------------

AutoComplete a ComboBox edit control

#Include <GuiComboBox.au3>

_GUICtrlComboBox_AutoComplete($hWnd)

Parameters

$hWnd Handle to control

Return Value

None.

The help example is a very nice one and it would help you alot.

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Link to comment
Share on other sites

Help file:

The help example is a very nice one and it would help you alot.

Hmm the only function I found in the helpfile was _GUICtrlComboAutoComplete( $h_combobox, ByRef $s_text ) perhaps we're running defferent versions.

In any case you're absolutely right, but I formulated my question wrong. The text I type in the combobox followed by the search button will download a page using InetGet that will hold the closest matches. These are added to the combobox dropdown. Autocomplete wouldn't work because the combobox has no (or old) data when I do a new search.

At the moment pressing the search button will call one function, while selecting one of the search results in the combo dropdown will call another one.

My idea was to make pressing enter or selecting one of the search results both call the same function and I'd put a check in that function to see if the GuiCtrlRead() for that combo is a value that was already in the dropdown to determine what to do next. (search for the new value, or load the data of a selected one)

If needed I can come up with an example sript tomorrow, but now I'm going to do some serious sleeping as it's pretty late over here. Thank you for your response though.

Link to comment
Share on other sites

OK, the case it a little complicated ... you will have to do alot of work ... good luck on that.

Here is an idea you can use (and develop further in your script)

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

Opt("GUIOnEventMode", 1)
HotKeySet("{ENTER}", "read_combo") 
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 366, 122, 193, 125)
GUISetOnEvent($GUI_EVENT_CLOSE, "Form1Close")
$Button1 = GUICtrlCreateButton("OK", 128, 84, 113, 25, 0)
GUICtrlSetOnEvent(-1, "Button1Click")
$Combo1 = GUICtrlCreateCombo("Combo1", 16, 24, 337, 25)
GUICtrlSetOnEvent(-1, "Combo1Change")
$Combo2 = GUICtrlCreateCombo("Combo2", 16, 50, 337, 25)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    Sleep(100)
WEnd

Func Button1Click()

EndFunc

Func read_combo()                      ;this function will detect which control has focus and call a function using that control ID as argument
    $ctrl = ControlGetFocus($Form1, "")
    MsgBox(0, "Control with focus", $ctrl)    ;this line is just to show you the name of the control - notice that the control returned is "Edit1" and "Edit2"
   Switch $ctrl
        Case "Edit1"
            ;MsgBox(0, "control", "Combo 1 is selected")
            update_content($Combo1)
        Case "Edit2"
            ;MsgBox(0, "control", "Combo 2 is selected")
            update_content($Combo2)
        Case Else
            MsgBox(0, "control", "No Idea")
    EndSwitch
EndFunc

Func update_content($selected)
    Switch GUICtrlRead($selected)
        Case "asd"
            GUICtrlSetData($selected, "|asdf|sdfg|adfg|asfg", "asdf")
        Case "asdf"
            MsgBox(0, "Test", "Item clicked")
        Case "qw"
            GUICtrlSetData($selected, "|qwer|wert|erty|rtyu", "qwer")
        Case Else
            MsgBox(0, "Test", "Unknown content")
    EndSwitch
EndFunc

Func Combo1Change()
    ;here you can put whatever you want to happen when user selects something in the combo box
EndFunc

Func Form1Close()
    Exit
EndFunc

I guess you have an idea now ... good luck :)

Edited by enaiman

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Link to comment
Share on other sites

OK, the case it a little complicated ... you will have to do alot of work ... good luck on that.

Here is an idea you can use (and develop further in your script)

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

Opt("GUIOnEventMode", 1)
HotKeySet("{ENTER}", "read_combo") 
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 366, 122, 193, 125)
GUISetOnEvent($GUI_EVENT_CLOSE, "Form1Close")
$Button1 = GUICtrlCreateButton("OK", 128, 84, 113, 25, 0)
GUICtrlSetOnEvent(-1, "Button1Click")
$Combo1 = GUICtrlCreateCombo("Combo1", 16, 24, 337, 25)
GUICtrlSetOnEvent(-1, "Combo1Change")
$Combo2 = GUICtrlCreateCombo("Combo2", 16, 50, 337, 25)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    Sleep(100)
WEnd

Func Button1Click()

EndFunc

Func read_combo()                      ;this function will detect which control has focus and call a function using that control ID as argument
    $ctrl = ControlGetFocus($Form1, "")
    MsgBox(0, "Control with focus", $ctrl)    ;this line is just to show you the name of the control - notice that the control returned is "Edit1" and "Edit2"
   Switch $ctrl
        Case "Edit1"
            ;MsgBox(0, "control", "Combo 1 is selected")
            update_content($Combo1)
        Case "Edit2"
            ;MsgBox(0, "control", "Combo 2 is selected")
            update_content($Combo2)
        Case Else
            MsgBox(0, "control", "No Idea")
    EndSwitch
EndFunc

Func update_content($selected)
    Switch GUICtrlRead($selected)
        Case "asd"
            GUICtrlSetData($selected, "|asdf|sdfg|adfg|asfg", "asdf")
        Case "asdf"
            MsgBox(0, "Test", "Item clicked")
        Case "qw"
            GUICtrlSetData($selected, "|qwer|wert|erty|rtyu", "qwer")
        Case Else
            MsgBox(0, "Test", "Unknown content")
    EndSwitch
EndFunc

Func Combo1Change()
    ;here you can put whatever you want to happen when user selects something in the combo box
EndFunc

Func Form1Close()
    Exit
EndFunc

I guess you have an idea now ... good luck :)

Awsome! I think I should be able to use the example. Thanks a lot!

Edit: Put a Send("{ENTER}") in function read_combo (did remember to disable the hotkey for the duration) to keep input controls and other programs working and put all but that line in a "If Winactive($Form1) Then........Endif" Statement to stop it from interfering when not active and it work flawless.

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