Shikuri

How to make a toggle button? - I'm newbie :C

7 posts in this topic

Hello guys.

I want to make toggle button C(ON/OFF), but im totally green there.

Share this post


Link to post
Share on other sites



Did you try searching the forum?? A simple search of toggle button or on/off button returns a number of threads.


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

Shikuri,

Welcome to the AutoIt forums. :)

You could do it like this:

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

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

$cButton = GUICtrlCreateCheckbox("On", 10, 10, 80, 30, $BS_PUSHLIKE)

GUISetState()

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $cButton
            Switch GUICtrlRead($cButton)
                Case $GUI_CHECKED
                    GUICtrlSetData($cButton, "Off")
                    MsgBox($MB_SYSTEMMODAL, "Button", "ON")
                Case Else
                    GUICtrlSetData($cButton, "On")
                    MsgBox($MB_SYSTEMMODAL, "Button", "OFF")
            EndSwitch
    EndSwitch
WEnd
As you are a complete beginner to AutoIt (and we all were at one point, so no problem at all) reading the Help file (at least the first few sections - Using AutoIt, Tutorials and the first couple of References) will help you enormously. You should also look at this excellent tutorial - you will find other tutorials in the Wiki (the link is at the top of the page). ;)

M23

1 person likes this

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

M23, I'm assuming because they admit they're new you didn't want to blow their mind with a Ternary setup?

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

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

$cButton = GUICtrlCreateCheckbox("OFF", 10, 10, 80, 30, $BS_PUSHLIKE)

GUISetState()

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $cButton
            GUICtrlSetData($cButton,(GUICtrlRead($cButton) = $GUI_CHECKED) ? "ON" : "OFF")
    EndSwitch
WEnd

Blah, blah, blah... lip service... lip service.Working on a number of projects right now, just waiting for my time to post them here on AutoIt forums.

Share this post


Link to post
Share on other sites

Cynagen,

Not entirely - my suggested solution also allows for other code to be run when the button is actioned. Whereas merely allowing the script to rename the button makes the whole thing a bit of a "self-licking lollipop". ;)

M23

1 person likes this

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

Cynagen,

Not entirely - my suggested solution also allows for other code to be run when the button is actioned. Whereas merely allowing the script to rename the button makes the whole thing a bit of a "self-licking lollipop". ;)

M23

 

?

1262868901_useless-machine.gif

2 people like this

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Cynagen,

Not entirely - my suggested solution also allows for other code to be run when the button is actioned. Whereas merely allowing the script to rename the button makes the whole thing a bit of a "self-licking lollipop". ;)

M23

 

I get that M23, you can still include an IF afterwards, it's just switching the button's display can be a simple 1-liner with the way I've done mine.

?

1262868901_useless-machine.gif

 

What M23 means by "self-licking lollipop" is that my code does nothing more than actually change the button text (which is what the user was originally asking for) in the simplest means using a Ternary operation. M23's breaks down what mine does into the basics (Ternary operations are effectively self-contained IF statements) for how it actually works on the inside and allows for extra code to be inserted to react to the change not only visually but with any other code that is inserted into the IF statement. For somebody just learning AutoIt, mine is pretty and takes care of the visuals, but doesn't address how you want the button to react, M23's does. I just like to minimize the amount of code I actually write to do something is all.

Edited by Cynagen

Blah, blah, blah... lip service... lip service.Working on a number of projects right now, just waiting for my time to post them here on AutoIt forums.

Share this post


Link to post
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