Jump to content

Using listbox data in the same GUI form


Recommended Posts

basically what I want to have, is a listbox with several options. Each of these options will change the value of the variable $text. The variable $text will be fed into a label(unless there is a better element to use?).

Ex: with this window open I have the choices 1,2,3,4 in a listbox. A little to the left of the listbox there is text that alerts me which element I have selected. eg: "You have 1 selected!", "You have 2 selected!", "You have 3 selected!"...

Any way this could be done?

EDIT: Sorry, I am very new to GUIs....

Edited by DoubleMcLovin
Link to comment
Share on other sites

  • Moderators

DoubleMcLovin,

I think this is what you want: ;)

#include <GUIConstantsEx.au3>

$sList = "Item 1|Item 2|Item 3|Item 4"

$hGUI = GUICreate("Test", 500, 500)

$hList = GUICtrlCreateList("", 10, 10, 200, 200)
GUICtrlSetData($hList, $sList)

$hLabel = GUICtrlCreateLabel("", 250, 10, 200, 20)

GUISetState()

$sCurrList = ""

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch

    ;---------------------------------
    $sActList = GUICtrlRead($hList)
    If $sActList <> $sCurrList Then
        $sCurrList = $sActList
        GUICtrlSetData($hLabel, "You have " & $sActList & " selected")
    EndIf
    ;---------------------------------
    
    ;GUICtrlSetData($hLabel, "You have " & GUICtrlRead($hList) & " selected")

WEnd

If you are wondering why we have all the $aActList <> $sCurrList code, try commenting out the lines between ----------- and use just the simple GUICtrlSetData($hLabel... line. That should make it clear! :P

I have deliberately not commented anything - try and understand what is going on and ask if there is anything that is not clear. :blink:

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

#include <GUIConstantsEx.au3>
Opt("MustDeclareVars", 1)

_GUI()

Func _GUI()
    Local $hForm1 = GUICreate("Form1", 252, 117, -1, -1)
    Local $hList1 = GUICtrlCreateList("", 8, 8, 121, 97)
    GUICtrlSetData(-1, "1. Choice One|2. Choice Two|3. Choice Three|4. Choice Four")
    Local $hLabel1 = GUICtrlCreateLabel("", 144, 48, 102, 17)
    GUISetState(@SW_SHOW)
    Local $sCurrentSelected, $sText

    While 1
        Local $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                Exit
            Case $hList1
                $sText = GUICtrlRead($hList1)
                If $sCurrentSelected <> "You have " & StringLeft($sText, 1) & " selected" Then
                    GUICtrlSetData($hLabel1, "You have " & StringLeft($sText, 1) & " selected")
                EndIf
        EndSwitch
    WEnd
EndFunc   ;==>_Gui

Edited by Yoriz
GDIPlusDispose - A modified version of GDIPlus that auto disposes of its own objects before shutdown of the Dll using the same function Syntax as the original.EzMySql UDF - Use MySql Databases with autoit with syntax similar to SQLite UDF.
Link to comment
Share on other sites

DoubleMcLovin,

I think this is what you want: :P

#include <GUIConstantsEx.au3>

$sList = "Item 1|Item 2|Item 3|Item 4"

$hGUI = GUICreate("Test", 500, 500)

$hList = GUICtrlCreateList("", 10, 10, 200, 200)
GUICtrlSetData($hList, $sList)

$hLabel = GUICtrlCreateLabel("", 250, 10, 200, 20)

GUISetState()

$sCurrList = ""

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch

    ;---------------------------------
    $sActList = GUICtrlRead($hList)
    If $sActList <> $sCurrList Then
        $sCurrList = $sActList
        GUICtrlSetData($hLabel, "You have " & $sActList & " selected")
    EndIf
    ;---------------------------------
    
    ;GUICtrlSetData($hLabel, "You have " & GUICtrlRead($hList) & " selected")

WEnd

If you are wondering why we have all the $aActList <> $sCurrList code, try commenting out the lines between ----------- and use just the simple GUICtrlSetData($hLabel... line. That should make it clear! :

I have deliberately not commented anything - try and understand what is going on and ask if there is anything that is not clear. ;)

M23

Thanks for the help! I had actually just assumed I wouldn't get a response so early in the morning, so I went back to reading tutorials and help files. After about 3.5hrs I was able to successfully build this myself :blink: (though your code is certainly cleaner by far!)

Here is what I ended up with, this is the entire window, which is just a rough draft, but there it is!

#include <GUIConstants.au3>

Global $CHName, $CHSex, $CHRace
Local $RD, $RDInline
$RDInline = "You Lose!"

#Region ; Character Creation Screen
$CHCreateForm = GUICreate("Create Your Character", 362, 325, 321, 212)
GUICtrlCreateGroup("Name", 0, 0, 185, 41)
$CHName = GUICtrlCreateInput("", 0, 16, 177, 21)
GUICtrlCreateGroup("", -99, -99, 1, 1)
GUICtrlCreateGroup("Sex", 0, 48, 185, 41)
$CHSexMale = GUICtrlCreateRadio("Male", 16, 64, 65, 17)
$CHSexFemale = GUICtrlCreateRadio("Female", 112, 64, 65, 17)
GUICtrlCreateGroup("", -99, -99, 1, 1)
GUICtrlCreateGroup("Race", 184, 0, 177, 265)
$CHRace = GUICtrlCreateList("", 192, 16, 145, 240)
GUICtrlSetData(-1, "Race1|Race2|Race3", "Race1")
GUICtrlCreateGroup("", -99, -99, 1, 1)
GUICtrlCreateGroup("Race Details", 0, 96, 185, 225)
$RD = GUICtrlCreateLabel($RDInline, 8, 109, 140, 209)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Next = GUICtrlCreateButton("Next", 216, 280, 113, 33, 0)

GUICtrlSetState($CHSexMale, $GUI_CHECKED)
GUICtrlSetState($Next, $GUI_DEFBUTTON)
#EndRegion ; Character Creation Screen
 
$CHSex = "Male" ; Sets Male as the default sex

GUISetState ()

While 1
   $msg = GUIGetMsg()
   Select
      Case $msg = $GUI_EVENT_CLOSE
         Exit
         
      Case $msg = $Next
         MsgBox(0, "Next", "You hit the button!" & @LF & $CHSex & @LF & $RD & @LF & $RDInline & @LF & $CHRace)
         
      Case $msg = $CHSexMale And BitAND(GUICtrlRead($CHSexMale), $GUI_CHECKED) = $GUI_CHECKED
         $CHSex = "Male" ; Changes sex to Male
         
      Case $msg = $CHSexFemale And BitAND(GUICtrlRead($CHSexFemale), $GUI_CHECKED) = $GUI_CHECKED
         $CHSex = "Female" ; Changes sex to Female
         
      Case $msg = $CHRace
         $RDInline = GUICtrlRead($CHRace)
         GUICtrlSetData($RD, $RDInline) 

   EndSelect
WEnd

Being that this is my first attempt at a GUI, I am kinda happy with it. I figured making a simple game would be a great way to learn how to use the GUI controls.

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