Jump to content

Changing a GUI list box to select one item


Recommended Posts

Could I please get some help in changing this code to:

1. Allow selection of only one item. (Currently it allows multiple selections)

2. Close the GUI after the selection button is pushed if one item has been selected. If one item hasn't been selected put a message out saying "You must select one"

; Original author PsaltyDS
#include <GUIConstants.au3>
#include <GuiList.au3>

Opt("GuiOnEventMode", 1)
$CreditCardCharge = "BROPHY BROS. VENTURA VENTURA CA          "
GUICreate("Select an expense account for - " & $CreditCardCharge, 550, 210)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Quit")
$List_1 = GUICtrlCreateList("Car/Truck Expense:Gas", 10, 10, 290, 180, BitOR($GUI_SS_DEFAULT_LIST, $LBS_MULTIPLESEL))
$hList_1 = GUICtrlGetHandle($List_1)
GUICtrlSetData(-1, "Travel & Entertainment:Meals|Hardware")
GUICtrlCreateButton("Select", 310, 50, 80, 30)
GUICtrlSetOnEvent(-1, "_Button")
GUICtrlCreateButton("Clear", 310, 130, 80, 30)
GUICtrlSetOnEvent(-1, "_Button")
GUISetState()

While 1
    Sleep(20)
WEnd

Func _Button()
    Local $avSel = _GUICtrlListGetSelItems($hList_1)
    If IsArray($avSel) Then
        Switch ControlGetText(@GUI_WinHandle, "", @GUI_CtrlHandle)
            Case "Select" 
                Local $sMsg = "Selected: " & $avSel[0] & @CRLF
                For $n = 1 To $avSel[0]
                    $sMsg &= _GUICtrlListGetText($hList_1, $avSel[$n]) & @CRLF
                Next
                MsgBox(64, "Selected", $sMsg)
            Case "Clear" 
                For $n = 1 To $avSel[0]
                    _GUICtrlListSetSel($hList_1, 0, $avSel[$n])
                Next
        EndSwitch
    EndIf
EndFunc   ;==>_Button

Func _Quit()
    Exit
EndFunc   ;==>_Quit

Thank you,

Docfxit

Link to comment
Share on other sites

@Docfxit...

1. Remove this parameter...BitOR($GUI_SS_DEFAULT_LIST, $LBS_MULTIPLESEL)

2. Look in the help file for _GUICtrlListGetSelItems and _GUICtrlListGetText.

That is really great. I chose the _GUICtrlListGetText and it's working perfectly. :-)

After I make the selection in the following code for one item in the listbox I need a GUI to select part of the characters in $CreditCardCharge.

An example would be:

From "BROPHY BROS. VENTURA VENTURA CA "

I would like a way to highlight and return to a field in AutoIt "BROPHY BROS"

#include <GUIConstants.au3>
#include <GuiList.au3>

$CreditCardCharge = "BROPHY BROS. VENTURA VENTURA CA          "
                GUICreate("Select an expense account for - " & $CreditCardCharge, 550, 250, -1, -1)
                $listbox = GUICtrlCreateList("", 125, 40, 180, 120)
                GUICtrlSetData($listbox, "Car/Truck Expense:Gas|Travel & Entertainment:Meals|Hardware")
                ;                   $label = GUICtrlCreateLabel("Account", 150, 210, 120)

                GUISetState()
                While 1
                    $msg = GUIGetMsg()
                    Select
                        Case $msg = $GUI_EVENT_CLOSE
                            ExitLoop
                        Case $msg = $listbox
                            $ret = _GUICtrlListGetText($listbox, _GUICtrlListSelectedIndex($listbox))
                            If ($ret == $LB_ERR) Then
                                MsgBox(16, "Error", "Unknown error from _GUICtrlListGetText")
                            Else
                                ;                               GUICtrlSetData($label, "Item : " & $ret)
                                $ExpenseAccount = '"' & $ret & '"' 
                                ExitLoop
                            EndIf
                    EndSelect
                WEnd

Can it be done to display a string, have the user highlight part of it and return what was highlighted?

Thank you,

Docfxit

Link to comment
Share on other sites

I think this may solve the problem but I can't figure out how to display the string:

$CreditCardCharge = "BROPHY BROS. VENTURA VENTURA CA "

in a GUI so I can select what I want and then call this function.

Func _GUICtrlEditReadSel($nCID)
    Local $aSel = _GUICtrlEditGetSel($nCID)
    If $aSel = -1 Then Return SetError(1, 0, -1)
    Return StringMid(GUICtrlRead($nCID), $aSel[1] + 1, ($aSel[2] - $aSel[1]) + 1)
EndFunc

Thank you,

Docfxit

Link to comment
Share on other sites

I have figured it out. This is my code to select part of a string and return that to my script:

Func UpdateVendors()
Dim $myedit, $Status, $msg, $Btn_GET, $a_sel
GUICreate("Vendor Name Selection", 302, 140)

$myedit = GUICtrlCreateEdit($asFields[5], 20, 32, 270, 27, $ES_AUTOVSCROLL)
GUICtrlSetLimit($myedit, 1500)
$Status = GUICtrlCreateLabel("", 30, 120, 200, 20, BitOR($SS_SUNKEN, $SS_CENTER))
$Btn_GET = GUICtrlCreateButton("Highlight Vendor Press to select", 100, 65, 90, 40, $BS_MULTILINE)

GUISetState()

; Run the GUI until the dialog is closed
While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
        Case $msg = $Btn_GET
            $a_sel = _GUICtrlEditGetSel ($myedit)
            If ($a_sel == $EC_ERR) Then
                GUICtrlSetData($Status, "Error getting sel positions")
            ElseIf (IsArray($a_sel)) Then
                $NewVendor = StringMid(GUICtrlRead($myedit), $a_Sel[1] + 1, ($a_Sel[2] - $a_Sel[1]) + 1)
                GUICtrlSetData($Status, $NewVendor)
                ReDim $Vendor[UBound($Vendor) + 1]
                ReDim $Account[UBound($Account) + 1]
                $Vendor[Ubound($Vendor) -1] = $NewVendor
                $Account[Ubound($Account) -1] = $AccountSelected
           Else
                GUICtrlSetData($Status, "")
            EndIf
    EndSelect
WEnd
EndFunc   ;==>UpdateVendors

Thank you,

Docfxit

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