TrackBar.ValueChanged 事件
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
当跟踪条的 Value 属性由于滚动框的移动或者由于代码中的操作而更改时发生。
public:
event EventHandler ^ ValueChanged;
public event EventHandler ValueChanged;
public event EventHandler? ValueChanged;
member this.ValueChanged : EventHandler
Public Custom Event ValueChanged As EventHandler
事件类型
示例
下面的代码示例演示如何使用 TickStyle、 Minimum和 Maximum 成员以及如何处理 ValueChanged 事件。 若要运行该示例,请将以下代码粘贴到包含TrackBar控件名称和TrackBar1
TextBox名为 的控件的TextBox1
窗体中。
InitializeTrackBar
从窗体的构造函数或Load事件处理方法调用 方法。
//Declare a new TrackBar object.
internal:
System::Windows::Forms::TrackBar^ TrackBar1;
// Initialize 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.
internal System.Windows.Forms.TrackBar TrackBar1;
// Initialize 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.
Friend WithEvents TrackBar1 As System.Windows.Forms.TrackBar
' Initialize 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
注解
当跟踪条中表示的值发生更改时,可以使用此事件更新其他控件。
有关处理事件的详细信息,请参阅 处理和引发事件。