Jump to content

Slider + Updating Label Question


cvocvo
 Share

Recommended Posts

So I'm working with a slider, but the kicker here is that I want to constantly probe the Slider value (I know use GUICtrlRead) and push that value into a label next to the slider.

I eventually want to have multiple sliders (2) that depend on a main slider's current value.

Anyway, what I've got so far is just a modified version of the sample code, but I can't seem to get it to update the slider value in the label. Maybe I misunderstood what is happening in the sample code, but this should be relatively simple.

Here's the code I'm working with to get a working version that I can actually implement:

#include <GUIConstantsEx.au3>

Opt('MustDeclareVars', 1)

Example()

Func Example()
    Local $slider1, $button, $msg, $label, $state
    
    GUICreate("slider", 220, 300, 100, 200)
    GUISetBkColor(0x00E0FFFF)  ; will change background color

    $slider1 = GUICtrlCreateSlider(10, 10, 200, 20)
    GUICtrlSetLimit(-1, 200, 0)     ; change min/max value
    $button = GUICtrlCreateButton("Value?", 75, 70, 70, 20)
    $label = GUICtrlCreateLabel("Value=" & $state, 75, 100, 100, 20)
    GUISetState()
    GUICtrlSetData($slider1, 45)    ; set cursor

    Do
        $state = GUICtrlRead($slider1)
        $msg = GUIGetMsg()
        If $msg = $button Then
            MsgBox(0, "slider1", GUICtrlRead($slider1), 2)
        EndIf
    Until $msg = $GUI_EVENT_CLOSE
EndFunc   ;==>Example

If anyone has any suggestions as to how to do this or even a better way to do this, I'm open to suggestions and help.

Thanks

Link to comment
Share on other sites

Maybe..

#include <GUIConstantsEx.au3>

Opt('MustDeclareVars', 1)

Global $Slider_hold

Example()

Func Example()
    Local $slider1, $button, $msg, $label, $state

    GUICreate("slider", 220, 300, 100, 200)
    GUISetBkColor(0x00E0FFFF) ; will change background color

    $slider1 = GUICtrlCreateSlider(10, 10, 200, 20)
    GUICtrlSetLimit(-1, 200, 0) ; change min/max value
    $button = GUICtrlCreateButton("Value?", 75, 70, 70, 20)
    $label = GUICtrlCreateLabel("Value=" & $state, 75, 100, 100, 20)
    GUISetState()
    GUICtrlSetData($slider1, 45) ; set cursor

    Do
        $state = GUICtrlRead($slider1)
        If $state <> $Slider_hold Then
            GUICtrlSetData($label, $state)
            $Slider_hold = $state
        EndIf
        $msg = GUIGetMsg()
        If $msg = $button Then
            MsgBox(0, "slider1", GUICtrlRead($slider1), 2)
        EndIf
    Until $msg = $GUI_EVENT_CLOSE
EndFunc   ;==>Example

8)

NEWHeader1.png

Link to comment
Share on other sites

  • Moderators

cvocvo,

Two ways to get a reading of the slider value -

Firstly, polling in the While...WEnd loop:

#include <GUIConstantsEx.au3>

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

$hSlider = GUICtrlCreateSlider(10, 50, 480, 40)

$hLabel = GUICtrlCreateLabel("0", 10, 10, 100, 40)
GUICtrlSetFont(-1, 24)

GUISetState()

$iLastSlider = 0

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch

    $iCurrSlider = GUICtrlRead($hSlider)
    If $iCurrSlider <> $iLastSlider Then
        GUICtrlSetData($hLabel, $iCurrSlider)
        $iLastSlider = $iCurrSlider
    EndIf

WEnd

If you are wondering why we have the $iCurrSlider/$iLastSlider code then try this:

#include <GUIConstantsEx.au3>

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

$hSlider = GUICtrlCreateSlider(10, 50, 480, 40)

$hLabel = GUICtrlCreateLabel("0", 10, 10, 100, 40)
GUICtrlSetFont(-1, 24)

GUISetState()

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch

    GUICtrlSetData($hLabel, GUICtrlRead($hSlider))

WEnd

See how the label flickers? That is why you need to limit the GUICtrlSetData calls to when they are needed, not every time round the loop.

Secondly a much sexier version:

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

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

$hSlider = GUICtrlCreateSlider(10, 50, 480, 40)
$hSlider_Handle = GUICtrlGetHandle(-1)

$hLabel = GUICtrlCreateLabel("0", 10, 10, 100, 40)
GUICtrlSetFont(-1, 24)

GUISetState()

GUIRegisterMsg($WM_HSCROLL, "WM_H_Slider")

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $GUI_EVENT_PRIMARYUP
            ToolTip("")
    EndSwitch

WEnd

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

    #forceref $hWnd, $iMsg, $wParam
    If $lParam = $hSlider_Handle Then
        GUICtrlSetData($hLabel, GUICtrlRead($hSlider))
    EndIf

    Return $GUI_RUNDEFMSG

EndFunc

If you have never used GUIRegisterMsg there is a tutorial in the Wiki whcih explains what it is all about. :mellow:

I hope that helps.

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

  • 6 years later...
On 15.06.2010 at 6:25 PM, Melba23 said:

Secondly a much sexier version:

 

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

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

$hSlider = GUICtrlCreateSlider(10, 50, 480, 40)
$hSlider_Handle = GUICtrlGetHandle(-1)

$hLabel = GUICtrlCreateLabel("0", 10, 10, 100, 40)
GUICtrlSetFont(-1, 24)

GUISetState()

GUIRegisterMsg($WM_HSCROLL, "WM_H_Slider")

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $GUI_EVENT_PRIMARYUP
            ToolTip("")
    EndSwitch

WEnd

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

    #forceref $hWnd, $iMsg, $wParam
    If $lParam = $hSlider_Handle Then
        GUICtrlSetData($hLabel, GUICtrlRead($hSlider))
    EndIf

    Return $GUI_RUNDEFMSG

EndFunc

 

Hello @Melba23, I trying to do this with three sliders. so i got this:

3x slider
+
$hSlider_Handle1 = GUICtrlGetHandle($Slider1)
GUICtrlSetData($Slider1, 36)
GUIRegisterMsg($WM_HSCROLL, "WM_H_Slider1")

$hSlider_Handle2 = GUICtrlGetHandle($Slider2)
GUICtrlSetData($Slider2, 72)
GUIRegisterMsg($WM_HSCROLL, "WM_H_Slider2")

$hSlider_Handle3 = GUICtrlGetHandle($Slider3)
GUICtrlSetData($Slider3, 67)
GUIRegisterMsg($WM_HSCROLL, "WM_H_Slider3")
+
Func WM_H_Slider1($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg, $wParam
    If $lParam = $hSlider_Handle1 Then
        GUICtrlSetData($Label3, GUICtrlRead($Slider1))
    EndIf
    Return $GUI_RUNDEFMSG
EndFunc
Func WM_H_Slider2($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg, $wParam
    If $lParam = $hSlider_Handle2 Then
        GUICtrlSetData($Label4, GUICtrlRead($Slider2))
    EndIf
    Return $GUI_RUNDEFMSG
EndFunc
Func WM_H_Slider3($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg, $wParam
    If $lParam = $hSlider_Handle3 Then
        GUICtrlSetData($Label5, GUICtrlRead($Slider3))
    EndIf
    Return $GUI_RUNDEFMSG
EndFunc

And only 3rd slider is working corectly. (i double checked label and slider numbers).

What i doing wrong? :)

 

Greetings.

Link to comment
Share on other sites

You can't register the same windows message multiple times, it will only register the last one you set. Put all three of your slider checks inside the same function and use something like this.

Func WM_H_Slider($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg, $wParam
    Switch $lParam
        Case $hSlider_Handle1
            GUICtrlSetData($Label3, GUICtrlRead($Slider1))
        Case $hSlider_Handle2
            GUICtrlSetData($Label4, GUICtrlRead($Slider2))
        Case $hSlifer_Handle3
            GUICtrlSetData($Label5, GUICtrlRead($Slider3))
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_H_Slider1

This way you can register once, and check all three sliders in the same function.

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

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