ScrollBar.OnValueChanged(EventArgs) Método

Definición

Genera el evento 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)

Parámetros

e
EventArgs

Objeto EventArgs que contiene los datos del evento.An EventArgs that contains the event data.

Ejemplos

En el ejemplo de código siguiente se usa la clase derivada VScrollBar.The following code example uses the derived class VScrollBar. Se crean los controladores de eventos para los eventos Scroll y ValueChanged.Event handlers for the Scroll and ValueChanged events are created. En este código se supone que se ha creado un Label y Button en un formulario y que el botón tiene un controlador de eventos para el evento 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. Al hacer clic en el botón, la propiedad Value de la barra de desplazamiento se ajusta en el código.When the button is clicked, the Value property of the scroll bar is adjusted in code. La etiqueta mostrará el valor actual de la propiedad Value y el evento que lo cambió.The label will display the current value of the Value property and the event that changed it. Observará que cuando se cambia el valor de desplazamiento por el evento Click del botón, solo se genera el evento ValueChanged.You will notice that when the scroll value is changed by the button's Click event, only the ValueChanged event is raised. En cambio, cuando la barra de desplazamiento se desplaza manualmente, el evento Scroll se genera inmediatamente después del evento ValueChanged.In contrast, when the scroll bar is scrolled manually, the Scroll event is raised immediately after the ValueChanged event.

Nota

Para obtener instrucciones sobre cómo ejecutar este ejemplo en Visual Studio, vea [How para: Compile y ejecute un ejemplo de código completo Windows Forms con Visual Studio @ no__t-0.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

Comentarios

Cuando se genera un evento, se invoca el controlador de eventos a través de un delegado.Raising an event invokes the event handler through a delegate. Para obtener más información, consulte controlar y provocar eventos.For more information, see Handling and Raising Events.

El método OnValueChanged también permite que las clases derivadas controlen el evento sin adjuntar ningún delegado.The OnValueChanged method also allows derived classes to handle the event without attaching a delegate. Ésta es la técnica preferida para controlar el evento en una clase derivada.This is the preferred technique for handling the event in a derived class.

Notas a los desarrolladores de herederos

Al reemplazar OnValueChanged(EventArgs) en una clase derivada, asegúrese de llamar al método de la clase base ' OnValueChanged(EventArgs) para que los delegados registrados reciban el evento.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.

Se aplica a

Consulte también: