Jump to content

Up/Down counter that increments/decrements by 0.1


DickG
 Share

Recommended Posts

I am trying to get an Up/Down counter to change by 0.1 increments.

I can get it to go up OR down by 0.1. But if I go UP, then back down, it still goes up for a while, then it goes down from a higher number. The same thing happens when I go DOWN: If I go down, then back up, it still goes down for a while, then goes up again from a lower number. I'm baffled by that action.

This is the test code:

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

Local $IntRate_val = ""
Local $IntRate = 5.0
Local $Close_btn
Local $idMsg

Example()

Func Example()
    Local $ThisFunc = "Example"

    GUICreate("GUI Up/Down", 200, 200, -1, -1, $WS_SIZEBOX)

    Local $IntRate_val = GUICtrlCreateInput($IntRate, 20, 20, 60, 30)
    GUICtrlCreateUpdown($IntRate_val)

    $Close_btn = GUICtrlCreateButton("Close", 80, 120, 50, 30)

    While 1
        GUISetState(@SW_SHOW)

        $idMsg = GUIGetMsg()

        Switch $idMsg
            Case $GUI_EVENT_CLOSE
                ExitLoop

            Case $IntRate_val
                ;If clicked UP, increment by 0.1.
                If Number(GUICtrlRead($IntRate_val)) > $IntRate Then
                    $IntRate += 0.1
                    ConsoleWrite("  Up: IntRate: " & $IntRate & @CR)
                    GUICtrlSetData($IntRate_val, $IntRate)
                EndIf

                ;If clicked DOWN, decrement by 0.1.
                If Number(GUICtrlRead($IntRate_val)) < $IntRate Then
                    $IntRate -=  0.1
                    ConsoleWrite("  Down: IntRate: " & $IntRate & @CR)
                    GUICtrlSetData($IntRate_val, $IntRate)
                EndIf

            Case $Close_btn
                Exit

        EndSwitch
    WEnd

EndFunc   ;==>Example

I'd be grateful for some insight into this.

 

Link to comment
Share on other sites

  • Moderators

DickG,

You can play around with the UpDown increment, but I usually cheat like this:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <StaticConstants.au3>
#include <EditConstants.au3>

$iStart = 10
$iMax = 20
$iMin = 0

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

$cDummy_Input = GUICtrlCreateLabel(StringFormat("%.01f", $iStart) & " ", 40, 10, 50, 20, BitOr($SS_RIGHT, $SS_CENTERIMAGE))
GUICtrlSetBkColor($cDummy_Input, 0xFFFFFF)
$cInput = GUICtrlCreateInput(($iStart * 10), 90, 10, 20, 20)
$cUpDn = GUICtrlCreateUpdown($cInput)
GUICtrlSetLimit($cUpDn, $iMax * 10, $iMin * 10)

GUISetState()

GUIRegisterMsg($WM_COMMAND, "_WM_COMMAND")

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

Func _WM_COMMAND($hWnd, $iMsg, $wParam, $lParam)

    #forceref $hWnd, $iMsg, $lParam

    ; If it was an update message from the input
    If BitShift($wParam, 16) = $EN_CHANGE Then
        Switch BitAND($wParam, 0xFFFF)
            Case $cInput
                ; Set the label to the new total
                GUICtrlSetData($cDummy_Input, StringFormat("%.01f", GUICtrlRead($cInput) / 10) & " ")
        EndSwitch
    EndIf

EndFunc   ;==>_WM_COMMAND

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

Melba, your version certainly works well. I had to play around with it a lot to figure out how you were doing it. Now I think I got it. It looks like you are using a dummy label in place of the input control's text, and an input control with up/down control sized and positioned to see only the up/down arrow. Then you make it go up/down by 0.1 by dividing the label's value by 10. So you must then multiply the input value by 10. Am I right?

I managed to size/position the controls in my own scrip. But I still need to get it to set the label to the right value. Haven't figured that out yet. It changes from 5.0 to 0.1 instead of 5.1. But I'll keep working on it.

Thanks much for this.

So I guess there's really no other way to use an up/down control to change the value by 0.1?

Link to comment
Share on other sites

Melba,

I finally figured out how to do this without using the WM_COMMAND() function.

#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
#include <WindowsConstants.au3>
#include <EditConstants.au3>

Dim $IntRate_val = ""
Dim $IntRate = 5.0
Dim $Close_btn
Dim $idMsg

Example()

Func Example()
    Local $ThisFunc = "Example"

    GUICreate("GUI Up/Down", 200, 200, -1, -1, $WS_SIZEBOX)

    $IntRate_label = GUICtrlCreateLabel(StringFormat("%.01f", $IntRate) & " ", 40, 10, 50, 20)
    GUICtrlSetBkColor($IntRate_label, 0xFFFFFF)
    $IntRate_val = GUICtrlCreateInput($IntRate, 90, 10, 20, 20)
    GUICtrlCreateUpdown($IntRate_val)

    $Close_btn = GUICtrlCreateButton("Close", 80, 120, 50, 30)

    ;Save IntRate for comparison in While loop.
    $IntRate_cur = $IntRate

    While 1
        GUISetState(@SW_SHOW)

        $idMsg = GUIGetMsg()

        Switch $idMsg
            Case $GUI_EVENT_CLOSE
                ExitLoop

            Case $IntRate_val
                ;If clicked UP, increment by 0.1.
                If GUICtrlRead($IntRate_val) > $IntRate_cur Then;clicking control increments or decrements value by 1.
                    $IntRate += 0.1
                    GUICtrlSetData($IntRate_label, $IntRate)

                    ;Increment $IntRate_cur by 1.
                    $IntRate_cur += 1
                ;If clicked DOWN, decrement by 0.1.
                Else
                    $IntRate -=  0.1
                    GUICtrlSetData($IntRate_label, $IntRate)

                    ;Decrement $IntRate_cur by 1.
                    $IntRate_cur -= 1
                EndIf

            Case $Close_btn
                Exit

        EndSwitch
    WEnd
EndFunc

The thing that I discovered was that the up/down counter can only change by 1. So I used your "trick" of using a label to display the value, and the up/down counter to make the change. It first saves the starting value ($IntRate_cur = $IntRate), then checks of the up/down counter when up or down by one. If up, in increments $IntRate by 0.1 and increments $IntRate_cur by 1. Etc.

The tricky part is to size and position the label and up/down counter to appear as one control.

So your "trick" worked great. I would have never thought of that.

Thanks again

Edited by DickG
Removed superflous code.
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...