Jump to content

Input in Combobox


FlyAgain
 Share

Recommended Posts

Hello all,

Is it possible to insert new vallue in a combobox, directly by the interface, and stock it in a .ini?

I search on web since two hour... but i just find this type of exemple:

#include

Example()

Func Example()
Local $msg

GUICreate("My GUI") ; will create a dialog box that when displayed is centered

GUICtrlCreateCombo("", 10, 10)

GUICtrlSetData(-1, "item1|item2|item3", "item3")

GUISetState() ; will display an empty dialog box with a combo control with focus on

; Run the GUI until the dialog is closed
While 1
$msg = GUIGetMsg()

If $msg = $GUI_EVENT_CLOSE Then ExitLoop
WEnd
EndFunc ;==>Example

but there isn't input methode by user...

Think for your help.

Fly

Link to comment
Share on other sites

  • Moderators

FlyAgain,

Welcome to the AutoIt forum. :)

What you want is not that difficult - here is one rough and ready way to do it:

#include <GUIConstantsEx.au3>

#include <Array.au3>

Example()

Func Example()

    Local $sIni_File = "Combo.ini"
    ; Read the ini content
    Local $aIni_List = IniReadSection($sIni_File, "Combo")
    ; Convert it to a string
    $sCombo_List = ""
    For $i = 1 To $aIni_List[0][0]
        $sCombo_List &= "|" & $aIni_List[$i][1]
    Next

    ; Create the GUI
    GUICreate("My GUI")

    $cCombo = GUICtrlCreateCombo("", 10, 10)
    GUICtrlSetData(-1, $sCombo_List)

    $cButton_Add = GUICtrlCreateButton("Add", 10, 50, 80, 30)
    $cButton_Save = GUICtrlCreateButton("Save", 100, 50, 80, 30)

    GUISetState()

    ; Run the GUI until the dialog is closed
    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                Exit
            Case $cCombo
                MsgBox(0, "Combo", GUICtrlRead($cCombo))
            Case $cButton_Add
                ; Read the current value of the combo
                $sCombo_Item = GUICtrlRead($cCombo)
                ; Add it to the list if it is a new item
                $sCombo_List = _Add_To_Combo($cCombo, $sCombo_List, $sCombo_Item)
                GUICtrlSetData($cCombo, $sCombo_List, $sCombo_Item)
            Case $cButton_Save
                ; Save the current list to the ini file
                _Save_List($sIni_File, $sCombo_List)
        EndSwitch
    WEnd
EndFunc   ;==>Example

Func _Add_To_Combo($cCombo, $sCurr_List, $sSel_Item)

    ; If the item is new then add it to the list
    If Not StringInStr($sCurr_List, $sSel_Item) Then
        $sCurr_List &= "|" & $sSel_Item
    EndIf
    Return $sCurr_List

EndFunc

Func _Save_List($sIni_Name, $sIni_List)

    ; Delete the current ini section
    IniDelete($sIni_Name, "Combo")
    ; Convert the list to a suitable format
    $aIni_List = StringSplit($sIni_List, "|")
    $sIni_Content = ""
    For $i = 2 To $aIni_List[0]
        $sIni_Content &= $i & "=" & $aIni_List[$i] & @LF
    Next
    ; Add the new section to the ini
    IniWriteSection($sIni_Name, "Combo", $sIni_Content)

EndFunc

The ini I used was this:

[Combo]
1=Item 1
2=Item 2
3=Item 3

The script needs a lot of errorchecking added and it is not very elegantly coded, but it should give you the idea. :)

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

  • Moderators

FlyAgain,

Did you create an ini file named "Combo.ini" with the format I showed in the same folder as the script? If not you will get that error as the IniReadSection fails to return an array. ;)

I did tell you the script needed more errorchecking code. :D

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

In my own Scripts i use Enter to insert a Value to the Combo Box.

I modifie the Example from Melba23, to do this.

#include <GUIConstantsEx.au3>
#include <GuiComboBoxEx.au3>; Only include for the CueBanner of the ComboBox
#include <Array.au3>

Example()

Func Example()

Local $sIni_File = "Combo.ini"
; Read the ini content
Local $aIni_List = IniReadSection($sIni_File, "Combo")
; Convert it to a string
$sCombo_List = ""
For $i = 1 To $aIni_List[0][0]
$sCombo_List &= "|" & $aIni_List[$i][1]
Next

; Create the GUI
GUICreate("My GUI")

$cCombo = GUICtrlCreateCombo("", 10, 10,300)
GUICtrlSetData(-1, $sCombo_List)
_GUICtrlComboBox_SetCueBanner($cCombo, "Write your Text, Enter will insert it in the Combo ")
$cButton_Add = GUICtrlCreateButton("Add", 10, 50, 80, 30)
$cButton_Save = GUICtrlCreateButton("Save", 100, 50, 80, 30)
$return = GUICtrlCreateDummy()
ControlFocus("My GUI", "", $cButton_Save); Set Focus to a other Control than Combobox, the CUEText will show in the Combo
Local $AccelKeys[1][2] = [["{ENTER}", $return]]
GUISetAccelerators($AccelKeys)
GUISetState()

; Run the GUI until the dialog is closed
While 1
Switch GUIGetMsg()
Case $return
$Focus = ControlGetFocus("My GUI", "")
If $Focus = "Edit1" Then; The Edit Control of the Combo is "Edit1"
; Read the current value of the combo
$sCombo_Item = GUICtrlRead($cCombo)
; Add it to the list if it is a new item
$sCombo_List = _Add_To_Combo($cCombo, $sCombo_List, $sCombo_Item)
GUICtrlSetData($cCombo, $sCombo_List, $sCombo_Item)
EndIf
Case $GUI_EVENT_CLOSE
Exit
Case $cCombo
MsgBox(0, "Combo", GUICtrlRead($cCombo))
Case $cButton_Add
; Read the current value of the combo
$sCombo_Item = GUICtrlRead($cCombo)
; Add it to the list if it is a new item
$sCombo_List = _Add_To_Combo($cCombo, $sCombo_List, $sCombo_Item)
GUICtrlSetData($cCombo, $sCombo_List, $sCombo_Item)
Case $cButton_Save
; Save the current list to the ini file
_Save_List($sIni_File, $sCombo_List)
EndSwitch
WEnd
EndFunc ;==>Example

Func _Add_To_Combo($cCombo, $sCurr_List, $sSel_Item)

; If the item is new then add it to the list
If Not StringInStr($sCurr_List, $sSel_Item) Then
$sCurr_List &= "|" & $sSel_Item
EndIf
Return $sCurr_List

EndFunc ;==>_Add_To_Combo

Func _Save_List($sIni_Name, $sIni_List)

; Delete the current ini section
IniDelete($sIni_Name, "Combo")
; Convert the list to a suitable format
$aIni_List = StringSplit($sIni_List, "|")
$sIni_Content = ""
For $i = 2 To $aIni_List[0]
$sIni_Content &= $i & "=" & $aIni_List[$i] & @LF
Next
; Add the new section to the ini
IniWriteSection($sIni_Name, "Combo", $sIni_Content)

EndFunc ;==>_Save_List

Raupi

Edited by Raupi
Link to comment
Share on other sites

Hello,

Thanks, i asked to myself how to do with "enter" button.

If i understand (sorry for my english), when "case=$return", it's an action like "enter" in combobox, if someting do "enter" in other text editing, there is guigetmsg() will be equal to $return too?

If yes, how differencing there?

Hello, I made ​​a version with the google translator ^ ^.

So I apologize for the English, I'm originally from Belgium and I speak French.

I wondered what could make out the GUIGetMsg $ ​​return ().

Is the pressing "enter" Genaire this "return"?

I did not find information about it in the help here: http://www.autoitscript.com/autoit3/docs/functions/GUIGetMsg.htm

Is that if I create another object of type textbox, when I click on it, it will retroune it as "$ retrun"?

If yes, how to differentiate between different "$ return"

Again thank you for your invaluable help.

Fly

Link to comment
Share on other sites

  • Moderators

FlyAgain,

how to do with "enter" button

That gets a bit more complex - here is how I would do it: ;)

#include <GUIConstantsEx.au3>
#include <WinAPI.au3>
#include <GuiComboBox.au3>
#include <Constants.au3>

Example()

Func Example()

    Local $tInfo

    Local $sIni_File = "Combo.ini"
    ; Read the ini content
    Local $aIni_List = IniReadSection($sIni_File, "Combo")
    ; Convert it to a string
    $sCombo_List = ""
    For $i = 1 To $aIni_List[0][0]
        $sCombo_List &= "|" & $aIni_List[$i][1]
    Next

    ; Create the GUI
    GUICreate("My GUI")

    $cCombo = GUICtrlCreateCombo("", 10, 10)
    GUICtrlSetData(-1, $sCombo_List)

    ; Replace the button with a dummy control
    $cDummy_Add = GUICtrlCreateDummy()
    $cButton_Save = GUICtrlCreateButton("Save", 100, 50, 80, 30)

    ; Set an Accelerator key - ENTER will fire the dummy control
    Local $aAccelKeys[1][2] = [["{ENTER}", $cDummy_Add]]
    GUISetAccelerators($aAccelKeys)

    GUISetState()

    ; We mow get the handle of the edit box part of the combo
    If _GUICtrlComboBox_GetComboBoxInfo($cCombo, $tInfo) Then
        $hCombo_Edit = DllStructGetData($tInfo, "hEdit")
    Else
        MsgBox(0, "Ooops", "Error")
        Exit
    EndIf

    ; Run the GUI until the dialog is closed
    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                Exit
            Case $cCombo
                MsgBox(0, "Combo", GUICtrlRead($cCombo))
            Case $cDummy_Add
                ; Check edit combo has focus
                If _WinAPI_GetFocus() = $hCombo_Edit Then ConsoleWrite("Hit" & @CRLF)
                ; Read the current value of the combo
                $sCombo_Item = GUICtrlRead($cCombo)
                ; Add it to the list if it is a new item
                $sCombo_List = _Add_To_Combo($cCombo, $sCombo_List, $sCombo_Item)
                GUICtrlSetData($cCombo, $sCombo_List, $sCombo_Item)
            Case $cButton_Save
                ; Save the current list to the ini file
                _Save_List($sIni_File, $sCombo_List)
        EndSwitch
    WEnd
EndFunc   ;==>Example

Func _Add_To_Combo($cCombo, $sCurr_List, $sSel_Item)

    ; If the item is new then add it to the list
    If Not StringInStr($sCurr_List, $sSel_Item) Then
        $sCurr_List &= "|" & $sSel_Item
    EndIf
    Return $sCurr_List

EndFunc

Func _Save_List($sIni_Name, $sIni_List)

    ; Delete the current ini section
    IniDelete($sIni_Name, "Combo")
    ; Convert the list to a suitable format
    $aIni_List = StringSplit($sIni_List, "|")
    $sIni_Content = ""
    For $i = 2 To $aIni_List[0]
        $sIni_Content &= $i & "=" & $aIni_List[$i] & @LF
    Next
    ; Add the new section to the ini
    IniWriteSection($sIni_Name, "Combo", $sIni_Content)

EndFunc

Check up on all the new functions in the Help file and see if you can work out how it works. Please ask if you have problems. :)

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

To facilitate testing I put the code to create the ini file.

if not FileExists(@ScriptDir & "Combo.ini") then FileWriteLine(@ScriptDir & _
"Combo.ini","[Combo]" & @crlf &  "1 = Line 1" & @crlf & "2 = Line 2" & @crlf _
& "3 = Line 3" &  @crlf  & "4 = Line 4" & @crlf & "5 = Line 5"); creates a .ini
Link to comment
Share on other sites

  • 3 weeks later...

Hello,

Thanks a lot for your exemples, they help me very lot.

I tried to understand your example, but some things need to escape, as in the case of two combos, it does not work.

Could you please correct me?

#include <GUIConstantsEx.au3>
#include <WinAPI.au3>
#include <GuiComboBox.au3>
#include <Constants.au3>

Example()

Func Example()

    Local $tInfo
    if not FileExists(@ScriptDir & "Combo.ini") then FileWriteLine(@ScriptDir & _
        "Combo.ini","[Combo1]" & @crlf &  "1 = Line 1" & @crlf & "2 = Line 2" & @crlf _
        & "3 = Line 3" &  @crlf  & "4 = Line 4" & @crlf & "5 = Line 5"& @crlf & "[Combo2]" & @crlf &  "1 = toto1" & @crlf & "2 = toto2" & @crlf _
        & "3 = toto3" &  @crlf  & "4 = toto4" & @crlf & "5 = toto5"); creates a .ini


    Local $sIni_File = "Combo.ini"
    ; Read the ini content
    Local $aIni_List_combo1 = IniReadSection($sIni_File, "combo1")
    ; Convert it to a string
    $sCombo_combo1_List = ""
    For $i = 1 To $aIni_List_combo1[0][0]
        $sCombo_combo1_List &= "|" & $aIni_List_combo1[$i][1]
    Next

    Local $aIni_List_combo2 = IniReadSection($sIni_File, "combo2")
    ; Convert it to a string
    $sCombo_combo2_List = ""
    For $i = 1 To $aIni_List_combo2[0][0]
        $sCombo_combo2_List &= "|" & $aIni_List_combo2[$i][1]
    Next

    ; Create the GUI
    GUICreate("My GUI",500)

    $cCombo_combo1 = GUICtrlCreateCombo("", 10, 10)
    $cCombo_combo2 = GUICtrlCreateCombo("", 220, 10)
    GUICtrlSetData($cCombo_combo1, $sCombo_combo1_List)
    GUICtrlSetData($cCombo_combo2, $sCombo_combo2_List)
    $cEdit = GUICtrlCreateEdit("text", 100, 100)

    ; Replace the button with a dummy control
    $cDummy_Add = GUICtrlCreateDummy()
    $cButton_Save = GUICtrlCreateButton("Save", 100, 50, 80, 30)

    ; Set an Accelerator key - ENTER will fire the dummy control
    Local $aAccelKeys[1][2] = [["{ENTER}", $cDummy_Add]]
    GUISetAccelerators($aAccelKeys)

    GUISetState()

    ; We mow get the handle of the edit box part of the combo
    If _GUICtrlComboBox_GetComboBoxInfo($cCombo_combo1, $tInfo) Then
        $hCombo_combo1_Edit = DllStructGetData($tInfo, "hEdit")
    Else
        MsgBox(0, "Ooops", "Error")
        Exit
    EndIf

    If _GUICtrlComboBox_GetComboBoxInfo($cCombo_combo2, $tInfo) Then
        $hCombo_combo2_Edit = DllStructGetData($tInfo, "hEdit")
    Else
        MsgBox(0, "Ooops", "Error")
        Exit
    EndIf

    ; Run the GUI until the dialog is closed
    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                Exit
            Case $cCombo_combo1
                MsgBox(0, "Combo", GUICtrlRead($cCombo_combo1))
            Case $cDummy_Add
                ; Check edit combo has focus
                If _WinAPI_GetFocus() = $hCombo_combo1_Edit and GUICtrlRead($cCombo_combo1)<>"" Then
                    ConsoleWrite("Hitcombo1" & @CRLF)
                    ; Read the current value of the combo
                    $sCombo_Item = GUICtrlRead($cCombo_combo1)
                    ; Add it to the list if it is a new item
                    $sCombo_combo1_List = _Add_To_Combo($cCombo_combo1, $sCombo_combo1_List, $sCombo_Item)
                    GUICtrlSetData($cCombo_combo1, $sCombo_combo1_List, $sCombo_Item)
                ElseIf _WinAPI_GetFocus() = $hCombo_combo2_Edit and GUICtrlRead($cCombo_combo2)<>"" Then
                    ConsoleWrite("Hitcombo2" & @CRLF)
                    ; Read the current value of the combo
                    $sCombo_Item = GUICtrlRead($cCombo_combo2)
                    ; Add it to the list if it is a new item
                    $sCombo_combo2_List = _Add_To_Combo($cCombo_combo2, $sCombo_combo2_List, $sCombo_Item)
                    GUICtrlSetData($cCombo_combo1, $sCombo_combo1_List, $sCombo_Item)
                EndIf
            Case $cButton_Save
                ; Save the current list to the ini file
                _Save_List($sIni_File, $sCombo_combo1_List)
        EndSwitch
    WEnd
EndFunc   ;==>Example

Func _Add_To_Combo($cCombo_combo1, $sCurr_List, $sSel_Item)


    ; If the item is new then add it to the list
    If Not StringInStr($sCurr_List, "|"&$sSel_Item) Then
        $sCurr_List &= "|" & $sSel_Item
    EndIf
    Return $sCurr_List


EndFunc

Func _Save_List($sIni_Name, $sIni_List)

    ; Delete the current ini section
    IniDelete($sIni_Name, "Combo")
    ; Convert the list to a suitable format
    $aIni_List_combo1 = StringSplit($sIni_List, "|")
    $sIni_Content = ""
    For $i = 2 To $aIni_List_combo1[0]
        $sIni_Content &= $i & "=" & $aIni_List_combo1[$i] & @LF
    Next
    ; Add the new section to the ini
    IniWriteSection($sIni_Name, "Combo", $sIni_Content)

EndFunc
Edited by FlyAgain
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...