ScrollBar.Scroll 事件

在通过鼠标或键盘操作移动滚动框后发生。

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
Public Event Scroll As ScrollEventHandler
用法
Dim instance As ScrollBar
Dim handler As ScrollEventHandler

AddHandler instance.Scroll, handler
public event ScrollEventHandler Scroll
public:
event ScrollEventHandler^ Scroll {
    void add (ScrollEventHandler^ value);
    void remove (ScrollEventHandler^ value);
}
/** @event */
public void add_Scroll (ScrollEventHandler value)

/** @event */
public void remove_Scroll (ScrollEventHandler value)
JScript 支持使用事件,但不支持进行新的声明。

备注

有关处理事件的更多信息,请参见 使用事件

示例

下面的代码示例使用派生类 VScrollBar。创建了用于 ScrollValueChanged 事件的事件处理程序。此代码假定已经在窗体上创建了一个 LabelButton,并且该按钮有一个用于 Click 事件的事件处理程序。当单击该按钮时,将在代码中调整滚动条的 Value 属性。该标签将显示 Value 属性的当前值以及更改该属性值的事件。您将会注意到,当按钮的 Click 事件更改滚动值时,只会引发 ValueChanged 事件。相反,当手动滚动滚动条时,在 ValueChanged 事件之后将立即引发 Scroll 事件。

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
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;
    }
 }
 
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.add_Scroll(
        new ScrollEventHandler(this.vScrollBar1_Scroll));
    vScrollBar1.add_ValueChanged(
        new EventHandler(this.vScrollBar1_ValueChanged));
} //AddMyScrollEventHandlers

// Create the ValueChanged event handler.
private void vScrollBar1_ValueChanged(Object sender, EventArgs e)
{
    // Display the new value in the label.
    label1.set_Text("vScrollBar Value:(OnValueChanged Event) " 
        + vScrollBar1.get_Value());
} //vScrollBar1_ValueChanged

// Create the Scroll event handler.
private void vScrollBar1_Scroll(Object sender, ScrollEventArgs e)
{
    // Display the new value in the label.
    label1.set_Text(
        "VScrollBar Value:(OnScroll Event) " + e.get_NewValue());
} //vScrollBar1_Scroll

private void button1_Click(Object sender, EventArgs e)
{
    // Add 40 to the Value property if it will not exceed the Maximum 
    // value.
    if (vScrollBar1.get_Value() + 40 < vScrollBar1.get_Maximum()) {
        vScrollBar1.set_Value(vScrollBar1.get_Value() + 40);
    }
} //button1_Click

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

ScrollBar 类
ScrollBar 成员
System.Windows.Forms 命名空间
OnScroll
ScrollEventHandler