Setting the Trackbar Slider's Position

Applies to TestComplete 14.60, last modified on April 22, 2021

While testing trackbar controls, you can use specific properties and methods of the corresponding program object to perform certain actions and obtain data stored in controls. You can call these methods and properties from your keyword tests, as well as from scripts. This topic describes how to work with the needed properties and methods from your scripts. However, when testing a control from your keyword test, you can use the same methods and properties calling them from keyword test operations. For more information, see Keyword Tests Basic Operations.

Working with a trackbar control usually implies moving its slider, that is setting it to a position that corresponds to certain numeric value. To set the needed slider position, you can use several approaches. All of them require that you use the Win32TrackBar object. TestComplete uses this object to work with standard Win32 trackbars whose class names are listed in the Object Mapping options list of your TestComplete project. You can set the slider position using specific properties and methods of this object:

  • You can specify the slider position by using the wPosition property. If you need to move the slider to a specific tick mark, you can determine its position via the wTicPosition property. This property returns the position, which corresponds to the specified tick mark. The following example obtains the position of the third tick mark and moves the slider to it:

    JavaScript, JScript

    function Main()
    {
    var p, w, TrackBar, Tick;

      // Obtain the trackbar object
      p = Sys.Process("Explorer").Window("Shell_TrayWnd").Window("TrayNotifyWnd").Window("SysPager").Window("ToolbarWindow32", "Notification Area");
      p.ClickItemR("Volume", false);
      p.PopupMenu.Click("Open Volume Control");
      w = Sys.Process("SNDVOL32").Window("Volume Control", "Master Volume");
      TrackBar = w.Window("msctls_trackbar32", "", 4);

      // Determine the logical position of the third tick
      Tick = TrackBar.wTicPosition("2");
      
      // Move the slider to the needed position
      TrackBar.wPosition = Tick;
    }

    Python

    def Main():
    
      # Obtain the trackbar object
      p = Sys.Process("Explorer").Window("Shell_TrayWnd").Window("TrayNotifyWnd").Window("SysPager").Window("ToolbarWindow32", "Notification Area")
      p.ClickItemR("Volume", False)
      p.PopupMenu.Click("Open Volume Control")
      w = Sys.Process("SNDVOL32").Window("Volume Control", "Master Volume")
      TrackBar = w.Window("msctls_trackbar32", "", 4)
    
      # Determine the logical position of the third tick
      Tick = TrackBar.wTicPosition["2"]
      
      # Move the slider to the needed position
      TrackBar.wPosition = Tick

    VBScript

    Sub Main
    Dim p, w, TrackBar, Tick

      ' Obtain the trackbar object
      Set p = Sys.Process("Explorer").Window("Shell_TrayWnd").Window("TrayNotifyWnd").Window("SysPager").Window("ToolbarWindow32", "Notification Area")
      Call p.ClickItemR("Volume", False)
      Call p.PopupMenu.Click("Open Volume Control")
      Set w = Sys.Process("SNDVOL32").Window("Volume Control", "Master Volume")
      Set TrackBar = w.Window("msctls_trackbar32", "", 4)

      ' Determine the logical position of the third tick
      Tick = TrackBar.wTicPosition(2)
      
      ' Move the slider to the needed position
      TrackBar.wPosition = Tick
    End Sub

    DelphiScript

    procedure Main();
    var 
      p, w, TrackBar, Tick: OleVariant;
    begin
      // Obtain the trackbar object
      p := Sys.Process('Explorer').Window('Shell_TrayWnd').Window('TrayNotifyWnd').Window('SysPager').Window('ToolbarWindow32', 'Notification Area');
      p.ClickItemR('Volume', false);
      p.PopupMenu.Click('Open Volume Control');
      w := Sys.Process('SNDVOL32').Window('Volume Control', 'Master Volume');
      TrackBar := w.Window('msctls_trackbar32', '', 4);

      // Determine the logical position of the third tick
      Tick := TrackBar.wTicPosition('2');
      
      // Move the slider to the needed position
      TrackBar.wPosition := Tick;
    end;

    C++Script, C#Script

    function Main()
    {
    var p, w, TrackBar, Tick;

      // Obtain the trackbar object
      p = Sys["Process"]("Explorer")["Window"]("Shell_TrayWnd")["Window"]("TrayNotifyWnd")["Window"]("SysPager")["Window"]("ToolbarWindow32", "Notification Area");
      p["ClickItemR"]("Volume", false);
      p["PopupMenu"]["Click"]("Open Volume Control");
      w = Sys["Process"]("SNDVOL32")["Window"]("Volume Control", "Master Volume");
      TrackBar = w["Window"]("msctls_trackbar32", "", 4);

      // Determine the logical position of the third tick
      Tick = TrackBar["wTicPosition"](2);
      
      // Move the slider to the needed position
      TrackBar["wPosition"] = Tick;
    }

  • You can simulate arrow keystrokes via the Keys action to move the slider in the needed direction. Simulation of keystrokes increases or decreases the current slider position by the value specified by the wLineSize property. For more information on how to simulate keystrokes, see Simulating Keystrokes. Here is an example:

    JavaScript, JScript

    function Main()
    {
    var p, w, TrackBar, Amount, Slider;

      // Obtain the trackbar object
      p = Sys.Process("Explorer").Window("Shell_TrayWnd").Window("TrayNotifyWnd").Window("SysPager").Window("ToolbarWindow32", "Notification Area");
      p.ClickItemR("Volume", false);
      p.PopupMenu.Click("Open Volume Control");
      w = Sys.Process("SNDVOL32").Window("Volume Control", "Master Volume");
      TrackBar = w.Window("msctls_trackbar32", "", 3);

      // Determine the increment and the current position
      Amount = TrackBar.wLineSize;
      Slider = TrackBar.wPosition;
      Log.Message(3*Amount + "+" + Slider);
        
      // Move the slider and post the new position to the log
      TrackBar.Keys("[Up][Up][Up]");
      Log.Message("=" + TrackBar.wPosition);
    }

    Python

    def Main():
    
      # Obtain the trackbar object
      p = Sys.Process("Explorer").Window("Shell_TrayWnd").Window("TrayNotifyWnd").Window("SysPager").Window("ToolbarWindow32", "Notification Area")
      p.ClickItemR("Volume", False)
      p.PopupMenu.Click("Open Volume Control")
      w = Sys.Process("SNDVOL32").Window("Volume Control", "Master Volume")
      TrackBar = w.Window("msctls_trackbar32", "", 3)
    
      # Determine the increment and the current position
      Amount = TrackBar.wLineSize
      Slider = TrackBar.wPosition
      Log.Message(3*Amount + "+" + Slider)
        
      # Move the slider and post the new position to the log
      TrackBar.Keys("[Up][Up][Up]")
      Log.Message("=" + TrackBar.wPosition)

    VBScript

    Sub Main
    Dim p, w, TrackBar, Amount, Slider

      ' Obtain the trackbar object
      Set p = Sys.Process("Explorer").Window("Shell_TrayWnd").Window("TrayNotifyWnd").Window("SysPager").Window("ToolbarWindow32", "Notification Area")
      Call p.ClickItemR("Volume", False)
      Call p.PopupMenu.Click("Open Volume Control")
      Set w = Sys.Process("SNDVOL32").Window("Volume Control", "Master Volume")
      Set TrackBar = w.Window("msctls_trackbar32", "", 3)

      ' Determine the increment and the current position
      Amount = TrackBar.wLineSize
      Slider = TrackBar.wPosition
      Log.Message(3*Amount & "+" & Slider)

      ' Move the slider and post the new position to the log
      TrackBar.Keys "[Up][Up][Up]"
      Log.Message("=" & TrackBar.wPosition)
    End Sub

    DelphiScript

    procedure Main();
    var 
      p, w, TrackBar, Amount, Slider: OleVariant;
    begin

      // Obtain the trackbar object
      p := Sys.Process('Explorer').Window('Shell_TrayWnd').Window('TrayNotifyWnd').Window('SysPager').Window('ToolbarWindow32', 'Notification Area');
      p.ClickItemR('Volume', false);
      p.PopupMenu.Click('Open Volume Control');
      w := Sys.Process('SNDVOL32').Window('Volume Control', 'Master Volume');
      TrackBar := w.Window('msctls_trackbar32', '', 3);

      // Determine the increment and the current position
      Amount := TrackBar.wLineSize;
      Slider := TrackBar.wPosition;
      Log.Message(aqConvert.VarToStr(3*Amount) + '+' + aqConvert.VarToStr(Slider));
        
      // Move the slider and post the new position to the log
      TrackBar.Keys('[Up][Up][Up]');
      Log.Message('=' + aqConvert.VarToStr(TrackBar.wPosition));
    end;

    C++Script, C#Script

    function Main()
    {
    var p, w, TrackBar, Amount, Slider;

      // Obtain the trackbar object
      p = Sys["Process"]("Explorer")["Window"]("Shell_TrayWnd")["Window"]("TrayNotifyWnd")["Window"]("SysPager")["Window"]("ToolbarWindow32", "Notification Area");
      p["ClickItemR"]("Volume", false);
      p["PopupMenu"]["Click"]("Open Volume Control");
      w = Sys["Process"]("SNDVOL32")["Window"]("Volume Control", "Master Volume");
      TrackBar = w["Window"]("msctls_trackbar32", "", 3);

      // Determine the increment and the current position to the log
      Amount = TrackBar["wLineSize"];
      Slider = TrackBar["wPosition"];
      Log["Message"](3*Amount + "+" + Slider);
        
      // Move the slider and post the new position to the log
      TrackBar["Keys"]("[Up][Up][Up]");
      Log["Message"]("=" + TrackBar["wPosition"]);
    }

    Note: Trackbars can have either horizontal or vertical orientation. If the tested trackbar is horizontally aligned, the Up or Right keystroke moves the slider to the right and the Left or Down keystroke moves the slider to the left. If the tested trackbar is vertically aligned, the Up or Right keystroke moves the slider up and the Left or Down keystroke moves the slider down.
  • You can estimate the coordinates of the needed slider position and simulate clicking on this point to move the slider to the needed position. You can simulate clicking via the Click action. The following code snippet moves the trackbar’s slider by clicking on the needed point:

    JavaScript, JScript

    function Main()
    {
    var p, w, TrackBar;

      // Obtain the trackbar object
      p = Sys.Process("Explorer").Window("Shell_TrayWnd").Window("TrayNotifyWnd").Window("SysPager").Window("ToolbarWindow32", "Notification Area");
      p.ClickItemR("Volume", false);
      p.PopupMenu.Click("Open Volume Control");
      w = Sys.Process("SNDVOL32").Window("Volume Control", "Master Volume");
      TrackBar = w.Window("msctls_trackbar32", "", 4);

      // Move the slider
      Log.Message(TrackBar.wPosition);
      TrackBar.Click(20,20);

      // Post the new slider position to the log
      Log.Message(Trackbar.wPosition);
    }

    Python

    def Main():
    
      # Obtain the trackbar object
      p = Sys.Process("Explorer").Window("Shell_TrayWnd").Window("TrayNotifyWnd").Window("SysPager").Window("ToolbarWindow32", "Notification Area")
      p.ClickItemR("Volume", False)
      p.PopupMenu.Click("Open Volume Control")
      w = Sys.Process("SNDVOL32").Window("Volume Control", "Master Volume")
      TrackBar = w.Window("msctls_trackbar32", "", 4)
    
      # Move the slider 
      Log.Message(TrackBar.wPosition)
      TrackBar.Click(20,20)
    
      # Post the new slider position to the log
      Log.Message(Trackbar.wPosition)

    VBScript

    Sub Main
    Dim p, w, TrackBar

      ' Obtain the trackbar object
      Set p = Sys.Process("Explorer").Window("Shell_TrayWnd").Window("TrayNotifyWnd").Window("SysPager").Window("ToolbarWindow32", "Notification Area")
      Call p.ClickItemR("Volume", False)
      Call p.PopupMenu.Click("Open Volume Control")
      Set w = Sys.Process("SNDVOL32").Window("Volume Control", "Master Volume")
      Set TrackBar = w.Window("msctls_trackbar32", "", 4)

      ' Move the slider
      Log.Message(TrackBar.wPosition)
      TrackBar.Click 20,20
      
      ' Post the new slider position to the log
      Log.Message(TrackBar.wPosition)
    End Sub

    DelphiScript

    procedure Main();
    var 
      p, w, TrackBar: OleVariant;
    begin

      // Obtain the trackbar object
      p := Sys.Process('Explorer').Window('Shell_TrayWnd').Window('TrayNotifyWnd').Window('SysPager').Window('ToolbarWindow32', 'Notification Area');
      p.ClickItemR('Volume', false);
      p.PopupMenu.Click('Open Volume Control');
      w := Sys.Process('SNDVOL32').Window('Volume Control', 'Master Volume');
      TrackBar := w.Window('msctls_trackbar32', '', 4);

      // Move the slider
      Log.Message(TrackBar.wPosition);
      TrackBar.Click(20,20);

      // Post the new slider position to the log
      Log.Message(TrackBar.wPosition);
    end;

    C++Script, C#Script

    function Main()
    {
    var p, w, TrackBar;

      // Obtain the trackbar object
      p = Sys["Process"]("Explorer")["Window"]("Shell_TrayWnd")["Window"]("TrayNotifyWnd")["Window"]("SysPager")["Window"]("ToolbarWindow32", "Notification Area");
      p["ClickItemR"]("Volume", false);
      p["PopupMenu"]["Click"]("Open Volume Control");
      w = Sys["Process"]("SNDVOL32")["Window"]("Volume Control", "Master Volume");
      TrackBar = w["Window"]("msctls_trackbar32", "", 4);

      // Move the slider
      Log["Message"](TrackBar["wPosition"]);
      TrackBar["Click"](20,20);

      // Post the new slider position to the log
      Log["Message"](TrackBar["wPosition"]);
    }

  • You can scroll the slider by simulating the rotation of the mouse wheel via the MouseWheel action. Here is an example:

    JavaScript, JScript

    function Main()
    {
    var p, w, TrackBar;

      // Obtain the trackbar object
      p = Sys.Process("Explorer").Window("Shell_TrayWnd").Window("TrayNotifyWnd").Window("SysPager").Window("ToolbarWindow32", "Notification Area");
      p.ClickItemR("Volume", false);
      p.PopupMenu.Click("Open Volume Control");
      w = Sys.Process("SNDVOL32").Window("Volume Control", "Master Volume");
      TrackBar = w.Window("msctls_trackbar32", "", 4);

      // Move the slider
      Log.Message(TrackBar.wPosition);
      TrackBar.MouseWheel(120);

      // Post the new slider position to the log
      Log.Message(TrackBar.wPosition);
    }

    Python

    def Main():
    
      # Obtain the trackbar object
      p = Sys.Process("Explorer").Window("Shell_TrayWnd").Window("TrayNotifyWnd").Window("SysPager").Window("ToolbarWindow32", "Notification Area")
      p.ClickItemR("Volume", False)
      p.PopupMenu.Click("Open Volume Control")
      w = Sys.Process("SNDVOL32").Window("Volume Control", "Master Volume")
      TrackBar = w.Window("msctls_trackbar32", "", 4)
    
      # Move the slider 
      Log.Message(TrackBar.wPosition)
      TrackBar.MouseWheel(120)
    
      # Post the new slider position to the log
      Log.Message(TrackBar.wPosition)

    VBScript

    Sub Main
    Dim p, w, TrackBar

      ' Obtain the trackbar object
      Set p = Sys.Process("Explorer").Window("Shell_TrayWnd").Window("TrayNotifyWnd").Window("SysPager").Window("ToolbarWindow32", "Notification Area")
      Call p.ClickItemR("Volume", False)
      Call p.PopupMenu.Click("Open Volume Control")
      Set w = Sys.Process("SNDVOL32").Window("Volume Control", "Master Volume")
      Set TrackBar = w.Window("msctls_trackbar32", "", 4)

      ' Move the slider
      Log.Message(TrackBar.wPosition)
      TrackBar.MouseWheel 120
      
      ' Post the new slider position to the log
      Log.Message(TrackBar.wPosition)
    End Sub

    DelphiScript

    procedure main();
    var 
      p, w, TrackBar: OleVariant;
    begin
      // Obtain the trackbar object
      p := Sys.Process('Explorer').Window('Shell_TrayWnd').Window('TrayNotifyWnd').Window('SysPager').Window('ToolbarWindow32', 'Notification Area');
      p.ClickItemR('Volume', false);
      p.PopupMenu.Click('Open Volume Control');
      w := Sys.Process('SNDVOL32').Window('Volume Control', 'Master Volume');
      TrackBar := w.Window('msctls_trackbar32', '', 4);

      // Move the slider
      Log.Message(TrackBar.wPosition);
      TrackBar.MouseWheel(120);

      // Post the new slider position to the log
      Log.Message(TrackBar.wPosition);
    end;

    C++Script, C#Script

    function Main()
    {
    var p, w, TrackBar;

      // Obtain the trackbar object
      p = Sys["Process"]("Explorer")["Window"]("Shell_TrayWnd")["Window"]("TrayNotifyWnd")["Window"]("SysPager")["Window"]("ToolbarWindow32", "Notification Area");
      p["ClickItemR"]("Volume", false);
      p["PopupMenu"]["Click"]("Open Volume Control");
      w = Sys["Process"]("SNDVOL32")["Window"]("Volume Control", "Master Volume");
      TrackBar = w["Window"]("msctls_trackbar32", "", 4);

      // Move the slider
      Log["Message"](TrackBar["wPosition"]);
      TrackBar["MouseWheel"](120);

      // Post the new slider position to the log
      Log["Message"](TrackBar["wPosition"]);
    }

See Also

Working With Trackbar Controls
wPosition Property (TrackBar and Slider Controls)
wTicPosition Property (TrackBar Controls)
Click Action
MouseWheel Action
Simulating Keystrokes

Highlight search results