Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 386
  • Last Modified:

Scrollbar ???

I have a form with different fields from my access table and I want to use a scrollbar to navigate the table.
How do I bind the dataset to the scrollbar?
Also, what are the events when clicking the scrollbar?

Thanks in advance,
  • 2
1 Solution
There are no data binding events for a scrollbar.

Your best bet is to load the data into a Dataset first so you can access it dynamically and move both forward and backward.  A datareader wont help you in this situation.

Once you have the dataset, you can setup the properties for your scrollbar.  Set your minimum value to zero which will correspond to record zero.  Set the max value to Dataset.Tables(0).Rows.Count - 1.  This will insure that a user can't scroll past the maximum number of records in the dataset.

Respond to the following event and access your dataset.  The NewValue parameter will correspond with the row index that you want to access.

    Private Sub VScrollBar1_Scroll(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles VScrollBar1.Scroll
        Dim Row as Datarow
        Row = MyDataset.Tables(0).Rows(e.NewValue)
        'Set your control values here
        Textbox1.Text = Row.Item("FirstName")
    End Sub

You could take some much more complex approaches if you truly wanted to create a DataBinder for a scrollbar, but I'm not sure if you really want to go through that trouble.
MylorAuthor Commented:
What do you mean where you wrote?:

   'Set your control values here
I don't think using scroll bar is a good approach in this case. You may use the database navigation technique as shown in the following links:

Ok, this should help.  This simple form binds 2 controls to a dataset that was generated with code.  The scrollbar navigates the records.

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()

        'This call is required by the Windows Form Designer.

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
            End If
        End If
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents txtName As System.Windows.Forms.TextBox
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents txtAddress As System.Windows.Forms.TextBox
    Friend WithEvents Scroll1 As System.Windows.Forms.HScrollBar
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.txtName = New System.Windows.Forms.TextBox
        Me.Label1 = New System.Windows.Forms.Label
        Me.Label2 = New System.Windows.Forms.Label
        Me.txtAddress = New System.Windows.Forms.TextBox
        Me.Scroll1 = New System.Windows.Forms.HScrollBar
        Me.txtName.Location = New System.Drawing.Point(128, 16)
        Me.txtName.Name = "txtName"
        Me.txtName.TabIndex = 0
        Me.txtName.Text = ""
        Me.Label1.Location = New System.Drawing.Point(16, 16)
        Me.Label1.Name = "Label1"
        Me.Label1.TabIndex = 1
        Me.Label1.Text = "Name"
        Me.Label2.Location = New System.Drawing.Point(16, 48)
        Me.Label2.Name = "Label2"
        Me.Label2.TabIndex = 3
        Me.Label2.Text = "Address"
        Me.txtAddress.Location = New System.Drawing.Point(128, 48)
        Me.txtAddress.Name = "txtAddress"
        Me.txtAddress.TabIndex = 2
        Me.txtAddress.Text = ""
        Me.Scroll1.Dock = System.Windows.Forms.DockStyle.Bottom
        Me.Scroll1.Location = New System.Drawing.Point(0, 93)
        Me.Scroll1.Name = "Scroll1"
        Me.Scroll1.Size = New System.Drawing.Size(352, 17)
        Me.Scroll1.TabIndex = 4
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(352, 110)
        Me.Name = "Form1"
        Me.Text = "Form1"

    End Sub

#End Region

    Private MyDataset As DataSet

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


        'Setup Scrollbar
        Scroll1.Minimum = 0
        Scroll1.Maximum = MyDataset.Tables(0).Rows.Count

        'Bind Controls to Dataset
        txtName.DataBindings.Add("Text", MyDataset, "MyTable.Name")
        txtAddress.DataBindings.Add("Text", MyDataset, "MyTable.Address")

    End Sub

    Private Sub GenerateDataset()

        MyDataset = New DataSet
        MyDataset.Tables.Add(New DataTable("MyTable"))
        MyDataset.Tables(0).Columns.Add(New DataColumn("Name"))
        MyDataset.Tables(0).Columns.Add(New DataColumn("Address"))

        Dim row As DataRow
        For i As Integer = 1 To 50
            row = MyDataset.Tables(0).NewRow()
            row.Item("Name") = "Name " & i
            row.Item("Address") = "Address " & i

    End Sub

    Private Sub Scroll1_Scroll(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles Scroll1.Scroll
        Me.BindingContext(MyDataset, "MyTable").Position = e.NewValue
    End Sub

End Class

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now