Mylor
asked on
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,
How do I bind the dataset to the scrollbar?
Also, what are the events when clicking the scrollbar?
Thanks in advance,
ASKER
What do you mean where you wrote?:
'Set your control values here
'Set your control values here
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'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
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.ICon tainer
'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.TextB ox
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents txtAddress As System.Windows.Forms.TextB ox
Friend WithEvents Scroll1 As System.Windows.Forms.HScro llBar
<System.Diagnostics.Debugg erStepThro ugh()> Private Sub InitializeComponent()
Me.txtName = New System.Windows.Forms.TextB ox
Me.Label1 = New System.Windows.Forms.Label
Me.Label2 = New System.Windows.Forms.Label
Me.txtAddress = New System.Windows.Forms.TextB ox
Me.Scroll1 = New System.Windows.Forms.HScro llBar
Me.SuspendLayout()
'
'txtName
'
Me.txtName.Location = New System.Drawing.Point(128, 16)
Me.txtName.Name = "txtName"
Me.txtName.TabIndex = 0
Me.txtName.Text = ""
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(16, 16)
Me.Label1.Name = "Label1"
Me.Label1.TabIndex = 1
Me.Label1.Text = "Name"
'
'Label2
'
Me.Label2.Location = New System.Drawing.Point(16, 48)
Me.Label2.Name = "Label2"
Me.Label2.TabIndex = 3
Me.Label2.Text = "Address"
'
'txtAddress
'
Me.txtAddress.Location = New System.Drawing.Point(128, 48)
Me.txtAddress.Name = "txtAddress"
Me.txtAddress.TabIndex = 2
Me.txtAddress.Text = ""
'
'Scroll1
'
Me.Scroll1.Dock = System.Windows.Forms.DockS tyle.Botto m
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
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(352, 110)
Me.Controls.Add(Me.Scroll1 )
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.txtAddr ess)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.txtName )
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
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
GenerateDataset()
'Setup Scrollbar
Scroll1.Minimum = 0
Scroll1.Maximum = MyDataset.Tables(0).Rows.C ount
'Bind Controls to Dataset
txtName.DataBindings.Add(" Text", MyDataset, "MyTable.Name")
txtAddress.DataBindings.Ad d("Text", MyDataset, "MyTable.Address")
End Sub
Private Sub GenerateDataset()
MyDataset = New DataSet
MyDataset.Tables.Add(New DataTable("MyTable"))
MyDataset.Tables(0).Column s.Add(New DataColumn("Name"))
MyDataset.Tables(0).Column s.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
MyDataset.Tables(0).Rows.A dd(row)
Next
End Sub
Private Sub Scroll1_Scroll(ByVal sender As System.Object, ByVal e As System.Windows.Forms.Scrol lEventArgs ) Handles Scroll1.Scroll
Me.BindingContext(MyDatase t, "MyTable").Position = e.NewValue
End Sub
End Class
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'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
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.ICon
'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.TextB
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents txtAddress As System.Windows.Forms.TextB
Friend WithEvents Scroll1 As System.Windows.Forms.HScro
<System.Diagnostics.Debugg
Me.txtName = New System.Windows.Forms.TextB
Me.Label1 = New System.Windows.Forms.Label
Me.Label2 = New System.Windows.Forms.Label
Me.txtAddress = New System.Windows.Forms.TextB
Me.Scroll1 = New System.Windows.Forms.HScro
Me.SuspendLayout()
'
'txtName
'
Me.txtName.Location = New System.Drawing.Point(128, 16)
Me.txtName.Name = "txtName"
Me.txtName.TabIndex = 0
Me.txtName.Text = ""
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(16, 16)
Me.Label1.Name = "Label1"
Me.Label1.TabIndex = 1
Me.Label1.Text = "Name"
'
'Label2
'
Me.Label2.Location = New System.Drawing.Point(16, 48)
Me.Label2.Name = "Label2"
Me.Label2.TabIndex = 3
Me.Label2.Text = "Address"
'
'txtAddress
'
Me.txtAddress.Location = New System.Drawing.Point(128, 48)
Me.txtAddress.Name = "txtAddress"
Me.txtAddress.TabIndex = 2
Me.txtAddress.Text = ""
'
'Scroll1
'
Me.Scroll1.Dock = System.Windows.Forms.DockS
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
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(352, 110)
Me.Controls.Add(Me.Scroll1
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.txtAddr
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.txtName
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
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
GenerateDataset()
'Setup Scrollbar
Scroll1.Minimum = 0
Scroll1.Maximum = MyDataset.Tables(0).Rows.C
'Bind Controls to Dataset
txtName.DataBindings.Add("
txtAddress.DataBindings.Ad
End Sub
Private Sub GenerateDataset()
MyDataset = New DataSet
MyDataset.Tables.Add(New DataTable("MyTable"))
MyDataset.Tables(0).Column
MyDataset.Tables(0).Column
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
MyDataset.Tables(0).Rows.A
Next
End Sub
Private Sub Scroll1_Scroll(ByVal sender As System.Object, ByVal e As System.Windows.Forms.Scrol
Me.BindingContext(MyDatase
End Sub
End Class
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.Cou
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.Scrol
Dim Row as Datarow
Row = MyDataset.Tables(0).Rows(e
'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.