Leer en inglés

Compartir a través de


ScrollBar.Scroll Evento

Definición

Se produce cuando el cuadro de desplazamiento se ha movido por una acción del mouse o del teclado.

C#
public event System.Windows.Forms.ScrollEventHandler Scroll;
C#
public event System.Windows.Forms.ScrollEventHandler? Scroll;

Tipo de evento

Ejemplos

En el ejemplo siguiente se desplaza una imagen en un cuadro de imagen. Usa el Value de la barra de desplazamiento para volver a dibujar una nueva parte de la imagen cada vez que el usuario se desplaza. Este ejemplo de código forma parte de un ejemplo más grande proporcionado para la información general de la ScrollBar clase.

C#
private void HandleScroll(Object sender, ScrollEventArgs e)
{
    //Create a graphics object and draw a portion of the image in the PictureBox.
    Graphics g = pictureBox1.CreateGraphics();

    int xWidth = pictureBox1.Width;
    int yHeight = pictureBox1.Height;

    int x;
    int y;

    if (e.ScrollOrientation == ScrollOrientation.HorizontalScroll)
    {
        x = e.NewValue;
        y = vScrollBar1.Value;
    }
    else //e.ScrollOrientation == ScrollOrientation.VerticalScroll
    {
        y = e.NewValue;
        x = hScrollBar1.Value;
    }

    g.DrawImage(pictureBox1.Image,
      new Rectangle(0, 0, xWidth, yHeight),  //where to draw the image
      new Rectangle(x, y, xWidth, yHeight),  //the portion of the image to draw
      GraphicsUnit.Pixel);

    pictureBox1.Update();
}

En el ejemplo de código siguiente se usa la clase VScrollBarderivada . Se crean controladores de eventos para los Scroll eventos y ValueChanged . Este código supone que y LabelButton se han creado en un formulario y que el botón tiene un controlador de eventos para el Click evento. Cuando se hace clic en el botón, la Value propiedad de la barra de desplazamiento se ajusta en el código. La etiqueta mostrará el valor actual de la Value propiedad y el evento que lo cambió. Observará que, cuando el evento del botón cambia el valor de Click desplazamiento, solo se genera el ValueChanged evento . Por el contrario, cuando la barra de desplazamiento se desplaza manualmente, el Scroll evento se genera inmediatamente después del ValueChanged evento.

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;
    }
 }

Comentarios

Para obtener más información acerca de cómo controlar eventos, vea controlar y provocar eventos.

Se aplica a

Producto Versiones
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

Consulte también