ScrollBar.ValueChanged 事件

定义

当通过 Value 事件或以编程方式更改 Scroll 属性时发生。

C#
public event EventHandler ValueChanged;
C#
public event EventHandler? ValueChanged;

事件类型

示例

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

备注

有关如何在 Visual Studio 中运行此示例的说明,请参阅如何:使用 Visual Studio 编译和运行完整的Windows 窗体代码示例

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

注解

有关如何处理事件的详细信息,请参阅 处理和引发事件

适用于

产品 版本
.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

另请参阅