Visual Basic .NET - ScrollBars Control
The ScrollBars control in VB.NET is used to add scroll bars to a form or a control. It provides the user with a way to view content that is not entirely visible on the screen. There are two types of scroll bars: horizontal and vertical. Both types can be added to a form or a control, depending on the need.
Common Properties:
- Anchor: Specifies how the control is anchored to the edges of its container.
- AutoSize: Determines whether the control resizes itself to fit its contents.
- BackColor: Sets the background color of the control.
- Enabled: Determines whether the control can respond to user interaction.
- Height: Specifies the height of the control.
- Maximum: Gets or sets the maximum value of the scroll bar.
- Minimum: Gets or sets the minimum value of the scroll bar.
- Name: Sets the name of the control.
- SmallChange: Gets or sets the amount of scroll bar value to change when the user clicks the arrow buttons.
- Value: Gets or sets the current value of the scroll bar.
- Width: Specifies the width of the control.
Common Methods:
- Focus(): Gives focus to the control.
- ResetText(): Clears the text associated with the control.
- Scroll(int value): Scrolls the content of the control by the specified amount.
- Select(): Selects the control.
Common Events:
- Scroll: Occurs when the user scrolls the scroll bar.
- ValueChanged: Occurs when the Value property changes.
Example:
- To add a vertical scroll bar to a form or a control, follow these steps:
- Drag and drop the ScrollBar control from the toolbox onto the form or control.
- Set the Maximum and Minimum properties to define the range of the scroll bar.
- Set the SmallChange property to define the amount by which the scroll bar value changes when the user clicks the arrow buttons.
- Handle the Scroll and ValueChanged events to respond to user interaction with the scroll bar.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' Add a vertical scroll bar to the TextBox control
Dim scrollBar As New VScrollBar()
scrollBar.Dock = DockStyle.Right
scrollBar.Minimum = 0
scrollBar.Maximum = TextBox1.Height
scrollBar.SmallChange = 10
AddHandler scrollBar.Scroll, AddressOf ScrollBar_Scroll
AddHandler scrollBar.ValueChanged, AddressOf ScrollBar_ValueChanged
Me.Controls.Add(scrollBar)
End Sub
Private Sub ScrollBar_Scroll(sender As Object, e As ScrollEventArgs)
' Scroll the TextBox control by the specified amount
TextBox1.ScrollToCaret()
End Sub
Private Sub ScrollBar_ValueChanged(sender As Object, e As EventArgs)
' Update the TextBox control's scroll position based on the scroll bar value
TextBox1.Top = -sender.Value
End Sub
In this example, a vertical scroll bar is added to a TextBox control by creating a new instance of the VScrollBar class and setting its properties. The Scroll and ValueChanged events are handled to update the TextBox control's scroll position based on the scroll bar value. When the user scrolls the scroll bar, the TextBox control is scrolled by the specified amount. When the scroll bar value changes, the TextBox control's Top property is updated to reflect the new scroll position.