TrackBar.ValueChanged 事件

当跟踪条的 Value 属性由于滚动框的移动或者由于代码中的操作而更改时发生。

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

语法

声明
Public Event ValueChanged As EventHandler
用法
Dim instance As TrackBar
Dim handler As EventHandler

AddHandler instance.ValueChanged, handler
public event EventHandler ValueChanged
public:
event EventHandler^ ValueChanged {
    void add (EventHandler^ value);
    void remove (EventHandler^ value);
}
/** @event */
public void add_ValueChanged (EventHandler value)

/** @event */
public void remove_ValueChanged (EventHandler value)
JScript 支持使用事件,但不支持进行新的声明。

备注

当跟踪条中表示的值更改时,可以使用此事件更新其他控件。

有关处理事件的更多信息,请参见 使用事件

示例

下面的代码示例阐释了如何使用 TickStyleMinimumMaximum 成员以及如何处理 ValueChanged 事件。若要运行此示例,请将以下代码粘贴到一个窗体中,该窗体包含一个名为 TrackBar1TrackBar 控件和一个名为 TextBox1TextBox 控件。在窗体的构造函数或 Load 事件处理方法中调用 InitializeTrackBar 方法。

'Declare a new TrackBar object.
Friend WithEvents TrackBar1 As System.Windows.Forms.TrackBar

' Initalize the TrackBar and add it to the form.
Private Sub InitializeTrackBar()
    Me.TrackBar1 = New System.Windows.Forms.TrackBar

    ' Set the TickStyle property so there are ticks on both sides
    ' of the TrackBar.
    TrackBar1.TickStyle = TickStyle.Both

    ' Set the minimum and maximum number of ticks.
    TrackBar1.Minimum = 10
    TrackBar1.Maximum = 100

    ' Set the tick frequency to one tick every ten units.
    TrackBar1.TickFrequency = 10

    TrackBar1.Location = New System.Drawing.Point(75, 30)
    Me.Controls.Add(Me.TrackBar1)
End Sub


' Handle the TrackBar.ValueChanged event by calculating a value for
' TextBox1 based on the TrackBar value.  
Private Sub TrackBar1_ValueChanged(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles TrackBar1.ValueChanged
    TextBox1.Text = System.Math.Round(TrackBar1.Value / 10)
End Sub
//Declare a new TrackBar object.
internal System.Windows.Forms.TrackBar TrackBar1;

// Initalize the TrackBar and add it to the form.
private void InitializeTrackBar()
{
    this.TrackBar1 = new System.Windows.Forms.TrackBar();
    TrackBar1.Location = new System.Drawing.Point(75, 30);

    // Set the TickStyle property so there are ticks on both sides
    // of the TrackBar.
    TrackBar1.TickStyle = TickStyle.Both;

    // Set the minimum and maximum number of ticks.
    TrackBar1.Minimum = 10;
    TrackBar1.Maximum = 100;

    // Set the tick frequency to one tick every ten units.
    TrackBar1.TickFrequency = 10;

    // Associate the event-handling method with the 
    // ValueChanged event.
    TrackBar1.ValueChanged += 
        new System.EventHandler(TrackBar1_ValueChanged);
    this.Controls.Add(this.TrackBar1);
}


// Handle the TrackBar.ValueChanged event by calculating a value for
// TextBox1 based on the TrackBar value.  
private void TrackBar1_ValueChanged(object sender, System.EventArgs e)
{
    TextBox1.Text = (System.Math.Round(TrackBar1.Value/10.0)).ToString();
}
   //Declare a new TrackBar object.
internal:
   System::Windows::Forms::TrackBar^ TrackBar1;

   // Initalize the TrackBar and add it to the form.
private:
   void InitializeTrackBar()
   {
      this->TrackBar1 = gcnew System::Windows::Forms::TrackBar;
      TrackBar1->Location = System::Drawing::Point( 75, 30 );
      
      // Set the TickStyle property so there are ticks on both sides
      // of the TrackBar.
      TrackBar1->TickStyle = TickStyle::Both;
      
      // Set the minimum and maximum number of ticks.
      TrackBar1->Minimum = 10;
      TrackBar1->Maximum = 100;
      
      // Set the tick frequency to one tick every ten units.
      TrackBar1->TickFrequency = 10;
      
      // Associate the event-handling method with the 
      // ValueChanged event.
      TrackBar1->ValueChanged += gcnew System::EventHandler( this, &Form1::TrackBar1_ValueChanged );
      this->Controls->Add( this->TrackBar1 );
   }

   // Handle the TrackBar.ValueChanged event by calculating a value for
   // TextBox1 based on the TrackBar value.  
   void TrackBar1_ValueChanged( Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      TextBox1->Text = (System::Math::Round( TrackBar1->Value / 10.0 )).ToString();
   }
//Declare a new TrackBar object.
System.Windows.Forms.TrackBar trackBar1;

// Initalize the TrackBar and add it to the form.
private void InitializeTrackBar()
{
    this.trackBar1 = new System.Windows.Forms.TrackBar();
    trackBar1.set_Location(new System.Drawing.Point(75, 30));
    // Set the TickStyle property so there are ticks on both sides
    // of the TrackBar.
    trackBar1.set_TickStyle(TickStyle.Both);
    // Set the minimum and maximum number of ticks.
    trackBar1.set_Minimum(10);
    trackBar1.set_Maximum(100);
    // Set the tick frequency to one tick every ten units.
    trackBar1.set_TickFrequency(10);
    // Associate the event-handling method with the 
    // ValueChanged event.
    trackBar1.add_ValueChanged(new System.EventHandler(
        trackBar1_ValueChanged));
    this.get_Controls().Add(this.trackBar1);
} //InitializeTrackBar

// Handle the TrackBar.ValueChanged event by calculating a value for
// textBox1 based on the TrackBar value.  
private void trackBar1_ValueChanged(Object sender, System.EventArgs e)
{
    textBox1.set_Text(((System.Double)System.Math.Round(
        trackBar1.get_Value() / 10.0)).ToString());
} //trackBar1_ValueChanged

平台

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 类
TrackBar 成员
System.Windows.Forms 命名空间