TrackBar 类

表示一个标准的 Windows 跟踪条。

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
<ComVisibleAttribute(True)> _
<ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)> _
Public Class TrackBar
    Inherits Control
    Implements ISupportInitialize
用法
Dim instance As TrackBar
[ComVisibleAttribute(true)] 
[ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)] 
public class TrackBar : Control, ISupportInitialize
[ComVisibleAttribute(true)] 
[ClassInterfaceAttribute(ClassInterfaceType::AutoDispatch)] 
public ref class TrackBar : public Control, ISupportInitialize
/** @attribute ComVisibleAttribute(true) */ 
/** @attribute ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch) */ 
public class TrackBar extends Control implements ISupportInitialize
ComVisibleAttribute(true) 
ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch) 
public class TrackBar extends Control implements ISupportInitialize

备注

TrackBar 是类似于 ScrollBar 控件的可滚动控件。可以通过以下方式配置跟踪条的 Value 属性值滚动的范围:通过设置 Minimum 属性指定该范围的下限,设置 Maximum 属性指定该范围的上限。

LargeChange 属性定义在滚动框的任一侧单击鼠标时对 Value 属性进行增减的量。跟踪条可以水平显示或垂直显示。

可以使用此控件输入通过 Value 属性获取的数值型数据。可以在一个控件中显示此数值型数据,或者在代码中使用此数据。

示例

下面的代码示例显示了一个包含 TrackBar 控件和 TextBox 控件的窗体。该示例演示如何设置 MaximumTickFrequencyLargeChangeSmallChange 属性以及如何处理 Scroll 事件。Scroll 事件发生时,TextBox 内容被更新为 Value 属性值。

Imports System
Imports System.Drawing
Imports System.Windows.Forms

Public Class Form1
    Inherits System.Windows.Forms.Form

    Private WithEvents trackBar1 As System.Windows.Forms.TrackBar
    Private textBox1 As System.Windows.Forms.TextBox

    <System.STAThread()> _
    Public Shared Sub Main()
        System.Windows.Forms.Application.Run(New Form1)
    End Sub 'Main

    Public Sub New()
        Me.textBox1 = New System.Windows.Forms.TextBox
        Me.trackBar1 = New System.Windows.Forms.TrackBar

        ' TextBox for TrackBar.Value update.
        Me.textBox1.Location = New System.Drawing.Point(240, 16)
        Me.textBox1.Size = New System.Drawing.Size(48, 20)

        ' Set up how the form should be displayed and add the controls to the form.
        Me.ClientSize = New System.Drawing.Size(296, 62)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.textBox1, Me.trackBar1})
        Me.Text = "TrackBar Example"

        ' Set up the TrackBar.
        Me.trackBar1.Location = New System.Drawing.Point(8, 8)
        Me.trackBar1.Size = New System.Drawing.Size(224, 45)

        ' The Maximum property sets the value of the track bar when
        ' the slider is all the way to the right.
        trackBar1.Maximum = 30

        ' The TickFrequency property establishes how many positions
        ' are between each tick-mark.
        trackBar1.TickFrequency = 5

        ' The LargeChange property sets how many positions to move
        ' if the bar is clicked on either side of the slider.
        trackBar1.LargeChange = 3

        ' The SmallChange property sets how many positions to move
        ' if the keyboard arrows are used to move the slider.
        trackBar1.SmallChange = 2
    End Sub 'New

    Private Sub trackBar1_Scroll(ByVal sender As Object, _
                    ByVal e As System.EventArgs) Handles trackBar1.Scroll

        ' Display the trackbar value in the text box.
        textBox1.Text = trackBar1.Value
    End Sub 

End Class 'Form1
using System;
using System.Drawing;
using System.Windows.Forms;

public class Form1 : System.Windows.Forms.Form
{
    private System.Windows.Forms.TrackBar trackBar1;
    private System.Windows.Forms.TextBox textBox1;

    [STAThread]
    static void Main() 
    {
        Application.Run(new Form1());
    }

    public Form1()
    {
        this.textBox1 = new System.Windows.Forms.TextBox();
        this.trackBar1 = new System.Windows.Forms.TrackBar();

        // TextBox for TrackBar.Value update.
        this.textBox1.Location = new System.Drawing.Point(240, 16);
        this.textBox1.Size = new System.Drawing.Size(48, 20);

        // Set up how the form should be displayed and add the controls to the form.
        this.ClientSize = new System.Drawing.Size(296, 62);
        this.Controls.AddRange(new System.Windows.Forms.Control[] {this.textBox1,this.trackBar1});
        this.Text = "TrackBar Example";

        // Set up the TrackBar.
        this.trackBar1.Location = new System.Drawing.Point(8, 8);
        this.trackBar1.Size = new System.Drawing.Size(224, 45);
        this.trackBar1.Scroll += new System.EventHandler(this.trackBar1_Scroll);

        // The Maximum property sets the value of the track bar when
        // the slider is all the way to the right.
        trackBar1.Maximum = 30;
        
        // The TickFrequency property establishes how many positions
        // are between each tick-mark.
        trackBar1.TickFrequency = 5;

        // The LargeChange property sets how many positions to move
        // if the bar is clicked on either side of the slider.
        trackBar1.LargeChange = 3;

        // The SmallChange property sets how many positions to move
        // if the keyboard arrows are used to move the slider.
        trackBar1.SmallChange = 2;
    }


    private void trackBar1_Scroll(object sender, System.EventArgs e)
    {
        // Display the trackbar value in the text box.
        textBox1.Text = "" + trackBar1.Value;
    }
}
using namespace System;
using namespace System::Drawing;
using namespace System::Windows::Forms;
public ref class Form1: public System::Windows::Forms::Form
{
private:
   System::Windows::Forms::TrackBar^ trackBar1;
   System::Windows::Forms::TextBox^ textBox1;

public:
   Form1()
   {
      this->textBox1 = gcnew System::Windows::Forms::TextBox;
      this->trackBar1 = gcnew System::Windows::Forms::TrackBar;
      
      // TextBox for TrackBar::Value update.
      this->textBox1->Location = System::Drawing::Point( 240, 16 );
      this->textBox1->Size = System::Drawing::Size( 48, 20 );
      
      // Set up how the form should be displayed and add the controls to the form.
      this->ClientSize = System::Drawing::Size( 296, 62 );
      array<System::Windows::Forms::Control^>^formControls = {this->textBox1,this->trackBar1};
      this->Controls->AddRange( formControls );
      this->Text = "TrackBar Example";
      
      // Set up the TrackBar.
      this->trackBar1->Location = System::Drawing::Point( 8, 8 );
      this->trackBar1->Size = System::Drawing::Size( 224, 45 );
      this->trackBar1->Scroll += gcnew System::EventHandler( this, &Form1::trackBar1_Scroll );
      
      // The Maximum property sets the value of the track bar when
      // the slider is all the way to the right.
      trackBar1->Maximum = 30;
      
      // The TickFrequency property establishes how many positions
      // are between each tick-mark.
      trackBar1->TickFrequency = 5;
      
      // The LargeChange property sets how many positions to move
      // if the bar is clicked on either side of the slider.
      trackBar1->LargeChange = 3;
      
      // The SmallChange property sets how many positions to move
      // if the keyboard arrows are used to move the slider.
      trackBar1->SmallChange = 2;
   }


private:
   void trackBar1_Scroll( Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      
      // Display the trackbar value in the text box.
      textBox1->Text = String::Concat( "", trackBar1->Value );
   }

};


[STAThread]
int main()
{
   Application::Run( gcnew Form1 );
}
import System.*;
import System.Drawing.*;
import System.Windows.Forms.*;

public class Form1 extends System.Windows.Forms.Form
{
    private System.Windows.Forms.TrackBar trackBar1;
    private System.Windows.Forms.TextBox textBox1;

    /** @attribute STAThread()
     */
    public static void main(String[] args)
    {
        Application.Run(new Form1());
    } //main

    public Form1()
    {
        this.textBox1 = new System.Windows.Forms.TextBox();
        this.trackBar1 = new System.Windows.Forms.TrackBar();
        // TextBox for TrackBar.Value update.
        this.textBox1.set_Location(new System.Drawing.Point(240, 16));
        this.textBox1.set_Size(new System.Drawing.Size(48, 20));
        // Set up how the form should be displayed and add the controls to the
        // form.
        this.set_ClientSize(new System.Drawing.Size(296, 62));
        this.get_Controls().AddRange(new System.Windows.Forms.Control[] { 
            this.textBox1, this.trackBar1 });
        this.set_Text("TrackBar Example");
        // Set up the TrackBar.
        this.trackBar1.set_Location(new System.Drawing.Point(8, 8));
        this.trackBar1.set_Size(new System.Drawing.Size(224, 45));
        this.trackBar1.add_Scroll(new System.EventHandler(this.trackBar1_Scroll));
        // The Maximum property sets the value of the track bar when
        // the slider is all the way to the right.
        trackBar1.set_Maximum(30);
        // The TickFrequency property establishes how many positions
        // are between each tick-mark.
        trackBar1.set_TickFrequency(5);
        // The LargeChange property sets how many positions to move
        // if the bar is clicked on either side of the slider.
        trackBar1.set_LargeChange(3);
        // The SmallChange property sets how many positions to move
        // if the keyboard arrows are used to move the slider.
        trackBar1.set_SmallChange(2);
    } //Form1

    private void trackBar1_Scroll(Object sender, System.EventArgs e)
    {
        // Display the trackbar value in the text box.
        textBox1.set_Text("" + trackBar1.get_Value());
    } //trackBar1_Scroll
} //Form1

继承层次结构

System.Object
   System.MarshalByRefObject
     System.ComponentModel.Component
       System.Windows.Forms.Control
        System.Windows.Forms.TrackBar

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

TrackBar 成员
System.Windows.Forms 命名空间