Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

ScrollBar.OnValueChanged Method

Raises the ValueChanged event.

[Visual Basic]
Protected Overridable Sub OnValueChanged( _
   ByVal e As EventArgs _
)
[C#]
protected virtual void OnValueChanged(
 EventArgs e
);
[C++]
protected: virtual void OnValueChanged(
 EventArgs* e
);
[JScript]
protected function OnValueChanged(
   e : EventArgs
);

Parameters

e
An EventArgs that contains the event data.

Remarks

Raising an event invokes the event handler through a delegate. For more information, see Raising an Event.

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.

Notes to Inheritors:  When overriding OnValueChanged in a derived class, be sure to call the base class's OnValueChanged method so that registered delegates receive the event.

Example

[Visual Basic, C#, C++] The following example uses the derived class, VScrollBar. Event handlers for the Scroll and ValueChanged events are created. 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. When the button is clicked, the Value property of the scroll bar is adjusted in code. The label will display the current value of the Value property and the event that changed it. You will notice that when the scroll value is changed by the button Click event, only the ValueChanged event is raised. In contrast, when the scroll bar is scrolled manually, the Scroll event is raised immediately after the ValueChanged event.

[Visual Basic] 
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


[C#] 
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;
    }
 }
 

[C++] 
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, 
       &Form1::vScrollBar1_Scroll);
    vScrollBar1->ValueChanged += new 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( S"vScrollBar Value:(OnValueChanged Event) {0}",
         __box(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( S"VScrollBar Value:(OnScroll Event) {0}",
         __box(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;
    }
 }
 

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

ScrollBar Class | ScrollBar Members | System.Windows.Forms Namespace | ValueChanged | EventArgs

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2015 Microsoft