Jump to content

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


Shikuri
 Share

Recommended Posts

  • Moderators

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

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

  • Moderators

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

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

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.

Link to comment
Share on other sites

  • Moderators

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

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

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

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

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

 

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.

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