Jump to content
Sign in to follow this  
gottygolly

Stopping a Label from going below 0 (Goes with my latest post)

Recommended Posts

gottygolly

Hello, Melba23 if you're reading this you might be able to answer this a little better than someone else because this is your code.

Heres the code.

#include <GUIConstantsEx.au3>

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

$cLabel = GUICtrlCreateLabel("100", 10, 10, 400, 40)
GUICtrlSetFont($cLabel, 24)

$cButton_Add5 = GUICtrlCreateButton("Add 5", 10, 100, 80, 30)
$cButton_Min10 = GUICtrlCreateButton("Minus 10", 110, 100, 80, 30)

GUISetState()

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $cButton_Add5
            GUICtrlSetData($cLabel, Number(GUICtrlRead($cLabel)) + 5)
        Case $cButton_Min10
            GUICtrlSetData($cLabel, Number(GUICtrlRead($cLabel)) - 10)
    EndSwitch
WEnd

Now when you hit 0 you can keep going below 0, such as -10, -20, -30, -40, -50.

Is there a way to prevent this?

In my code I managed to make it popup a msgbox when you hit 0 or below and it exits the scripts but you can keep going below 0 if you don't click the button to set off the msgbox function.

Any help would be amazing :) (It's probably something stupid)

Edited by gottygolly

Share this post


Link to post
Share on other sites
JLogan3o13

Why not just add an If statement to keep it above 0?

#include <GUIConstantsEx.au3>

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

$cLabel = GUICtrlCreateLabel("100", 10, 10, 400, 40)
GUICtrlSetFont($cLabel, 24)

$cButton_Add5 = GUICtrlCreateButton("Add 5", 10, 100, 80, 30)
$cButton_Min10 = GUICtrlCreateButton("Minus 10", 110, 100, 80, 30)

GUISetState()

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $cButton_Add5
            GUICtrlSetData($cLabel, Number(GUICtrlRead($cLabel)) + 5)
        Case $cButton_Min10
            If GUICtrlRead($cLabel) > 10 Then
                GUICtrlSetData($cLabel, Number(GUICtrlRead($cLabel)) - 10)
            Else
                GUICtrlSetData($cLabel, Number(0))
            EndIf
    EndSwitch
WEnd
  • Like 1

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

Share this post


Link to post
Share on other sites
gottygolly

Thought it was something dumb :P

Thanks.

Share this post


Link to post
Share on other sites
gottygolly

Sorry for posting here again I figured it's better just to repost on here instead of making a whole new one.

with this code

#include <GUIConstantsEx.au3>

 

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

 $amount = "100"

$cLabel = GUICtrlCreateLabel("%" & $amount, 10, 10, 400, 40)

GUICtrlSetFont($cLabel, 24)

 

$cButton_Add5 = GUICtrlCreateButton("Add 5", 10, 100, 80, 30)

$cButton_Min10 = GUICtrlCreateButton("Minus 10", 110, 100, 80, 30)

 

GUISetState()

 

While 1

    Switch GUIGetMsg()

        Case $GUI_EVENT_CLOSE

            Exit

        Case $cButton_Add5

            GUICtrlSetData($cLabel, Number(GUICtrlRead($cLabel)) + 5)

        Case $cButton_Min10

            If GUICtrlRead($cLabel) > 10 Then

                GUICtrlSetData($cLabel, Number(GUICtrlRead($cLabel)) - 10)

            Else

                GUICtrlSetData($cLabel, Number(0))

            EndIf

    EndSwitch

WEnd

If I were to change the "100" in the label to ("%" & $amount) why doe it change it back to "0" without the "%"?

I don't care about how it changes back to 0, I just want to be able to change the variable $amount without deleting the "%" in front of it.

Any ideas?

Share this post


Link to post
Share on other sites
BrewManNH

Because GUICtrlSetData on a label erases the previous contents with what you're writing to the label. If you want it formatted the same, you have to write it in exactly the same format every time you update the label.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

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
Sign in to follow this  

×