Jump to content

Need help for Checkbox & Slidebar script


Recommended Posts

Hello All,

I newbie here, can anyone help me for complete below script. my sliderbar script work fine, but i dunno how to make checkbox script to work. ( cause i dunno how to use while function )

Sorry my english no good, hope u all can understand.

#RequireAdmin
#include <GUIConstantsEx.au3>
#include <GDIPlus.au3>
#include <WinAPI.au3>
#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <Process.au3>
#NoTrayIcon

Global $slider1

GUICreate("Test Checkbox and Slider Bar", 370, 400, 100, 100)  ; will create a dialog box
GUISetBkColor(0xFFFBF0)  ; will change background color
GUISetState(@SW_SHOW)
$hTab = GUICtrlCreateTab(5, 10, 360, 380)

$hTab0 = GUICtrlCreateTabItem("Tab 01")
    GUICtrlCreateGroup("", 15, 35, 120, 80)
    $cb1 = GUICtrlCreateCheckbox("CheckBox 1", 25, 50, 100, 20)
    $cb2 = GUICtrlCreateCheckbox("CheckBox 2", 25, 70, 100, 20)
    GUICtrlCreateGroup("", -99, -99, 1, 1)  ;close group

    GUICtrlCreateGroup("", 140, 35, 215, 80)
    GUICtrlCreateLabel("Test InputBox ", 150, 50)
    $InputBox = GUICtrlCreateInput("0", 220, 50, 100, 20)
    GUICtrlCreateGroup("", -99, -99, 1, 1)  ;close group
    GUICtrlCreateLabel("Test Sliderbar ", 150, 80)
    
    GUICtrlCreateLabel("Status of CheckBox : ", 20, 130)
    GUICtrlCreateLabel("CheckBox 1 : ", 30, 145)
    GUICtrlCreateLabel("CheckBox 2 : ", 30, 160)
    
GUICtrlCreateTabItem("")

while 1
    $nmsg = GUIGetMsg()
    If $nmsg = $GUI_EVENT_CLOSE Then
        Exit
    EndIf   
    if $nmsg = $cb1 Then
    checkbox1()
    EndIf
    if $nmsg = $cb2 Then
    checkbox2()
    EndIf
    if $nmsg = $slider1 then
        Slidebar()  
    EndIf       
WEnd


Func Checkbox1()
if BitAnd(GUICtrlRead($cb1), $GUI_CHECKED) == $GUI_CHECKED Then
    GUICtrlCreateLabel("Checked ", 100, 145)
Else
    GUICtrlCreateLabel("Unchecked ", 100, 145)
EndIf
EndFunc

Func Checkbox2()
if BitAnd(GUICtrlRead($cb2), $GUI_CHECKED) == $GUI_CHECKED Then
    GUICtrlCreateLabel("Checked ", 100, 160)
Else
    GUICtrlCreateLabel("Unchecked ", 100, 160)
EndIf
EndFunc

Func Slidebar()
    $slider1 = GUICtrlCreateSlider(220, 80, 100, 20)
    GUICtrlSetLimit(-1, 500, 0)     ; change min/max value
    GUISetState()
    GUICtrlSetData($slider1, 0)     ; set cursor    
    Do
        $msgslider = GUIGetMsg()        
        If $msgslider = $slider1 Then
            GUICtrlCreateLabel(GUICtrlRead($slider1), 325, 80)
        EndIf
    Until $msgslider = $GUI_EVENT_CLOSE
EndFunc
Link to comment
Share on other sites

Some points:

1. There should not have been a control created every time you only want to change the text. Create it once and then change the text as required.

2. Your slider function trapped the script in the Do/Until loop.

3. When you find yourself with a stack of If/Then checking the same value over and over, think about Switch or Select instead.

4. Only one function is required to handle the checkboxes, since the same thing is done for either one.

5. You went crazy with the #include statements. Only one was actually required.

6. Nothing this script does needed #RequireAdmin.

Fixed demo:

#include <GUIConstantsEx.au3>

Global $slider1

GUICreate("Test Checkbox and Slider Bar", 370, 400, 100, 100) ; will create a dialog box
GUISetBkColor(0xFFFBF0) ; will change background color
$hTab = GUICtrlCreateTab(5, 10, 360, 380)

$hTab0 = GUICtrlCreateTabItem("Tab 01")

GUICtrlCreateGroup("", 15, 35, 120, 80)
$cb1 = GUICtrlCreateCheckbox("CheckBox 1", 25, 50, 100, 20)
$cb2 = GUICtrlCreateCheckbox("CheckBox 2", 25, 70, 100, 20)
GUICtrlCreateGroup("", -99, -99, 1, 1) ;close group

GUICtrlCreateGroup("", 140, 35, 215, 80)
GUICtrlCreateLabel("Test InputBox ", 150, 50)
$InputBox = GUICtrlCreateInput("0", 220, 50, 100, 20)
GUICtrlCreateLabel("Test Sliderbar ", 150, 80)
$slider1 = GUICtrlCreateSlider(220, 80, 100, 20)
GUICtrlSetLimit(-1, 500, 0) ; change min/max value
GUISetState()
GUICtrlSetData($slider1, 0) ; set cursor
$Label3 = GUICtrlCreateLabel("0", 325, 80, 25, 20)
GUICtrlCreateGroup("", -99, -99, 1, 1) ;close group

GUICtrlCreateLabel("Status of CheckBox : ", 20, 130)
GUICtrlCreateLabel("CheckBox 1 : ", 30, 145, 100, 20)
$Label1 = GUICtrlCreateLabel("Unchecked", 140, 145, 100, 20)
GUICtrlCreateLabel("CheckBox 2 : ", 30, 160, 100, 20)
$Label2 = GUICtrlCreateLabel("Unchecked", 140, 160, 100, 20)

GUICtrlCreateTabItem("")

GUISetState(@SW_SHOW)

While 1
    $Msg = GUIGetMsg()
    Switch $Msg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $cb1, $cb2
            checkbox($Msg)
        Case $slider1
    GUICtrlSetData($Label3, GUICtrlRead($slider1))
    EndSwitch
WEnd

Func Checkbox($iCtrlID)
    $sTxt = "Unchecked"
    If BitAND(GUICtrlRead($iCtrlID), $GUI_CHECKED) Then $sTxt = "Checked"

    Switch $iCtrlID
        Case $cb1
            GUICtrlSetData($Label1, $sTxt)
        Case $cb2
            GUICtrlSetData($Label2, $sTxt)
    EndSwitch
EndFunc   ;==>Checkbox

:mellow:

Edit: Added a couple more points.

Edited by PsaltyDS
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
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...