ScrollBar.OnValueChanged(EventArgs) ScrollBar.OnValueChanged(EventArgs) ScrollBar.OnValueChanged(EventArgs) ScrollBar.OnValueChanged(EventArgs) Method

定义

引发 ValueChanged 事件。Raises the ValueChanged event.

protected:
 virtual void OnValueChanged(EventArgs ^ e);
protected virtual void OnValueChanged (EventArgs e);
abstract member OnValueChanged : EventArgs -> unit
override this.OnValueChanged : EventArgs -> unit
Protected Overridable Sub OnValueChanged (e As EventArgs)

参数

e
EventArgs EventArgs EventArgs EventArgs

包含事件数据的 EventArgsAn EventArgs that contains the event data.

示例

下面的代码示例使用派生类VScrollBarThe following code example uses the derived class VScrollBar. 创建ScrollValueChanged事件的事件处理程序。Event handlers for the Scroll and ValueChanged events are created. 此代码假定已在LabelButton体上创建了和,并且该按钮具有Click事件的事件处理程序。This code assumes that a Label and Button have been created on a form and that the button has an event handler for the Click event. 单击该按钮时,滚动条Value的属性将以代码进行调整。When the button is clicked, the Value property of the scroll bar is adjusted in code. 标签将显示Value属性的当前值和更改它的事件。The label will display the current value of the Value property and the event that changed it. 你会注意到,当按钮的Click事件更改滚动值时, ValueChanged只会引发事件。You will notice that when the scroll value is changed by the button's Click event, only the ValueChanged event is raised. 相反,当手动滚动滚动条时, Scroll事件将在ValueChanged事件之后立即引发。In contrast, when the scroll bar is scrolled manually, the Scroll event is raised immediately after the ValueChanged event.

备注

有关如何在 Visual Studio 中运行此示例的说明,请参阅如何:使用 Visual Studio编译和运行完整的 Windows 窗体代码示例。For instructions about how to run this example in Visual Studio, see How to: Compile and Run a Complete Windows Forms Code Example Using Visual Studio.

void AddMyScrollEventHandlers()
{
   // Create and initialize a VScrollBar.
   VScrollBar^ vScrollBar1 = gcnew VScrollBar;

   // Add event handlers for the OnScroll and OnValueChanged events.
   vScrollBar1->Scroll += gcnew ScrollEventHandler( this, &Form1::vScrollBar1_Scroll );
   vScrollBar1->ValueChanged += gcnew EventHandler( this, &Form1::vScrollBar1_ValueChanged );
}

// Create the ValueChanged event handler.
void vScrollBar1_ValueChanged( Object^ /*sender*/, EventArgs^ /*e*/ )
{
   // Display the new value in the label.
   label1->Text = String::Format( "vScrollBar Value:(OnValueChanged Event) {0}", vScrollBar1->Value );
}

// Create the Scroll event handler.
void vScrollBar1_Scroll( Object^ /*sender*/, ScrollEventArgs^ e )
{
   // Display the new value in the label.
   label1->Text = String::Format( "VScrollBar Value:(OnScroll Event) {0}", e->NewValue );
}

void button1_Click( Object^ /*sender*/, EventArgs^ /*e*/ )
{
   // Add 40 to the Value property if it will not exceed the Maximum value.
   if ( vScrollBar1->Value + 40 < vScrollBar1->Maximum )
   {
      vScrollBar1->Value = vScrollBar1->Value + 40;
   }
}
private void AddMyScrollEventHandlers()
 {
    // Create and initialize a VScrollBar.
    VScrollBar vScrollBar1 = new VScrollBar();
 
    // Add event handlers for the OnScroll and OnValueChanged events.
    vScrollBar1.Scroll += new ScrollEventHandler(
       this.vScrollBar1_Scroll);
    vScrollBar1.ValueChanged += new EventHandler(
       this.vScrollBar1_ValueChanged); 
 }
 
 // Create the ValueChanged event handler.
 private void vScrollBar1_ValueChanged(Object sender, 
                                       EventArgs e)
 {
     // Display the new value in the label.
     label1.Text = "vScrollBar Value:(OnValueChanged Event) " + vScrollBar1.Value.ToString();
 }
 
 // Create the Scroll event handler.
 private void vScrollBar1_Scroll(Object sender, 
                                 ScrollEventArgs e)
 {
     // Display the new value in the label.
     label1.Text = "VScrollBar Value:(OnScroll Event) " + e.NewValue.ToString();
 }
 
 private void button1_Click(Object sender, 
                           EventArgs e)
 {
    // Add 40 to the Value property if it will not exceed the Maximum value.
    if (vScrollBar1.Value + 40 < vScrollBar1.Maximum)
    {
        vScrollBar1.Value = vScrollBar1.Value + 40;
    }
 }
 
Private Sub AddMyScrollEventHandlers()
    ' Create and initialize a VScrollBar.
    Dim vScrollBar1 As New VScrollBar()
    
    ' Add event handlers for the OnScroll and OnValueChanged events.
    AddHandler vScrollBar1.Scroll, AddressOf Me.vScrollBar1_Scroll
    AddHandler vScrollBar1.ValueChanged, AddressOf Me.vScrollBar1_ValueChanged
End Sub    

' Create the ValueChanged event handler.
Private Sub vScrollBar1_ValueChanged(sender As Object, e As EventArgs)
    ' Display the new value in the label.
    label1.Text = "vScrollBar Value:(OnValueChanged Event) " & _
        vScrollBar1.Value.ToString()
End Sub    

' Create the Scroll event handler.
Private Sub vScrollBar1_Scroll(sender As Object, e As ScrollEventArgs)
    ' Display the new value in the label.
    label1.Text = "VScrollBar Value:(OnScroll Event) " & _
        e.NewValue.ToString()
End Sub    

Private Sub button1_Click(sender As Object, e As EventArgs)
    ' Add 40 to the Value property if it will not exceed the Maximum value.
    If vScrollBar1.Value + 40 < vScrollBar1.Maximum Then
        vScrollBar1.Value = vScrollBar1.Value + 40
    End If
End Sub

注解

引发事件时,将通过委托调用事件处理程序。Raising an event invokes the event handler through a delegate. 有关详细信息, 请参阅处理和引发事件For more information, see Handling and Raising Events.

OnValueChanged 方法还允许派生类对事件进行处理而不必附加委托。The OnValueChanged method also allows derived classes to handle the event without attaching a delegate. 这是在派生类中处理事件的首选技术。This is the preferred technique for handling the event in a derived class.

继承者说明

当在OnValueChanged(EventArgs)派生类中重写时,请确保调用基类的OnValueChanged(EventArgs)方法,以便注册的委托接收事件。When overriding OnValueChanged(EventArgs) in a derived class, be sure to call the base class' OnValueChanged(EventArgs) method so that registered delegates receive the event.

适用于

另请参阅