ScrollBar.Scroll Událost

Definice

Vyvolá se při přesunutí posuvníku pomocí myši nebo akce klávesnice.

public:
 event System::Windows::Forms::ScrollEventHandler ^ Scroll;
public event System.Windows.Forms.ScrollEventHandler Scroll;
public event System.Windows.Forms.ScrollEventHandler? Scroll;
member this.Scroll : System.Windows.Forms.ScrollEventHandler 
Public Custom Event Scroll As ScrollEventHandler 

Event Type

Příklady

Následující příklad posune obrázek v poli obrázku. Pomocí Value posuvníku překresluje novou část obrázku pokaždé, když se uživatel posune. Tento příklad kódu je součástí většího příkladu ScrollBar pro přehled třídy.

Poznámka

Pokyny ke spuštění tohoto příkladu v sadě Visual Studio najdete v tématu Postupy: Kompilace a spuštění kompletního příkladu model Windows Forms Code pomocí sady Visual Studio.

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();
}
Private Sub HandleScroll(ByVal sender As [Object], ByVal e As ScrollEventArgs) _
  Handles vScrollBar1.Scroll, hScrollBar1.Scroll

    'Create a graphics object and draw a portion of the image in the PictureBox.
    Dim g As Graphics = pictureBox1.CreateGraphics()

    Dim xWidth As Integer = pictureBox1.Width
    Dim yHeight As Integer = pictureBox1.Height

    Dim x As Integer
    Dim y As Integer

    If (e.ScrollOrientation = ScrollOrientation.HorizontalScroll) Then

        x = e.NewValue
        y = vScrollBar1.Value

    Else 'e.ScrollOrientation == ScrollOrientation.VerticalScroll

        y = e.NewValue
        x = hScrollBar1.Value
    End If

    'First Rectangle: Where to draw the image.
    'Second Rectangle: The portion of the image to draw.

    g.DrawImage(pictureBox1.Image, _
      New Rectangle(0, 0, xWidth, yHeight), _
      New Rectangle(x, y, xWidth, yHeight), _
      GraphicsUnit.Pixel)

    pictureBox1.Update()
End Sub

Následující příklad kódu používá odvozenou třídu VScrollBar. Vytvoří se obslužné rutiny událostí Scroll pro události a ValueChanged . Tento kód předpokládá, že Label ve formuláři byly vytvořeny a Button a že tlačítko má obslužnou rutinu Click události. Po kliknutí na Value tlačítko se vlastnost posuvníku upraví v kódu. Popisek zobrazí aktuální hodnotu Value vlastnosti a událost, která ji změnila. Všimněte si, že při změně hodnoty posouvání událostí tlačítka Click je vyvolána pouze ValueChanged událost. Naproti tomu při ručním posouvání posuvníku Scroll je událost vyvolána ihned po události ValueChanged .

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

Poznámky

Další informace o zpracování událostí najdete v tématu Zpracování a vyvolávání událostí.

Platí pro

Viz také