ScrollBar::OnValueChanged Method

Raises the ValueChanged event.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

virtual void OnValueChanged(
	EventArgs^ e


Type: System::EventArgs

An EventArgs that contains the event data.

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.

The following code 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's 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.


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;

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
© 2015 Microsoft