Custom Control Scrollbar options

Experienced posters,

I am attempting to sync the scrolling of various controls on a winform or
user control. Specifically a tree/grid on the left side and a custom gdi+
drawing on the left and a splitter in the middle. Each node on the left
should have a row in the dynamically drawn image on the right.

Can someone point me in the right direction?

Thanks
mross3Asked:
Who is Participating?
 
planoczCommented:
I don't know if this will help,but here is a sample that I had laying around......


'FORM 1

Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Drawing
Imports System.Windows.Forms
Namespace Microsoft.Samples
    Public Class ctlScrollBar
        Inherits System.Windows.Forms.Form
        ' Variables for adjusting the position of the pictureBox when scrolling
        Private dragging As Boolean = False
        Private oldX, oldY As Integer
        Private vScrollMultiplier As Single
        Private vAbsPos As Single
        Private hScrollMultiplier As Single
        Private hAbsPos As Single
        Public Sub New()

            MyBase.New()

            ScrollBarCtl = Me

            'This call is required by the Windows Forms Designer.
            InitializeComponent()

            comboBox1.SelectedIndex = 0
            comboBox2.SelectedIndex = 0

            VScrollBar1.LargeChange = Convert.ToInt16(comboBox1.SelectedItem.ToString())
            HScrollBar1.LargeChange = Convert.ToInt16(comboBox1.SelectedItem.ToString())
            VScrollBar1.SmallChange = Convert.ToInt16(comboBox2.SelectedItem.ToString())
            HScrollBar1.SmallChange = Convert.ToInt16(comboBox2.SelectedItem.ToString())

            SetVerticalScrollMultiplier()
            SetHorizontalScrollMultiplier()

            label11.Text = HScrollBar1.Value.ToString()
            label12.Text = VScrollBar1.Value.ToString()

            ' Change the cursor give the user feedback for dragging the
            ' picture around the picturebox
            pictureBox1.Cursor = Cursors.SizeAll

        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
        ' The vScrollMultiplier takes into account the height of the pictureBox
        ' when scrolling.
        ' It is essentially "pixels per tick", where a tick is the smallest
        ' increment of the scrollbar.
        Private Sub SetVerticalScrollMultiplier()
            Dim hsb As Single = CSng(VScrollBar1.Height - pictureBox1.Height)
            Dim ticks As Single = CSng(VScrollBar1.Maximum - VScrollBar1.Minimum)

            vScrollMultiplier = hsb / ticks
        End Sub
        ' The hScrollMultiplier takes into account the width of the
        ' pictureBox when scrolling.
        ' It is essentially "pixels per tick", where a tick is the smallest
        ' increment of the scrollbar.
        Private Sub SetHorizontalScrollMultiplier()
            Dim hsb As Single = CSng(HScrollBar1.Width - pictureBox1.Width)
            Dim ticks As Single = CSng(HScrollBar1.Maximum - HScrollBar1.Minimum)

            hScrollMultiplier = hsb / ticks
        End Sub
        ' Vertical scrollbar handler - scrollbar action handled by Windows Forms Scrollbar.
        ' Here we update the label displaying our value and the
        ' set the  pictureBox y coordinate.
        Private Sub VScrollBar1_Scroll(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles VScrollBar1.Scroll
            label12.Text = VScrollBar1.Value.ToString()
            vAbsPos = CSng(VScrollBar1.Value - VScrollBar1.Minimum)
            pictureBox1.Top = VScrollBar1.Bottom - CInt(vScrollMultiplier * vAbsPos) - pictureBox1.Height
        End Sub


        ' Horizontal scrollbar handler - scrollbar action handled by Windows Forms
        ' Scrollbar. Here we update the label displaying our value and the set
        ' the pictureBox y coordinate.
        Private Sub HScrollBar1_Scroll(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles HScrollBar1.Scroll
            label11.Text = HScrollBar1.Value.ToString()
            hAbsPos = CSng(HScrollBar1.Value - HScrollBar1.Minimum)
            pictureBox1.Left = HScrollBar1.Right - CInt(hScrollMultiplier * hAbsPos) - pictureBox1.Width
        End Sub


        ' Changing the "LargeChange" (the number of ticks per page) property of
        ' the ScrollBarCtl.
        Private Sub comboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles comboBox1.SelectedIndexChanged
            VScrollBar1.LargeChange = Convert.ToInt16(comboBox1.SelectedItem.ToString())
            HScrollBar1.LargeChange = Convert.ToInt16(comboBox1.SelectedItem.ToString())
            label11.Text = HScrollBar1.Value.ToString()
            label12.Text = VScrollBar1.Value.ToString()
        End Sub


        ' Changing the "SmallChange" (the number of ticks per line) property of
        ' the ScrollBarCtl.
        Private Sub comboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles comboBox2.SelectedIndexChanged
            VScrollBar1.SmallChange = Convert.ToInt16(comboBox2.SelectedItem.ToString())
            HScrollBar1.SmallChange = Convert.ToInt16(comboBox2.SelectedItem.ToString())
            label11.Text = HScrollBar1.Value.ToString()
            label12.Text = VScrollBar1.Value.ToString()
        End Sub


        ' Mouse event handler to initiate dragging the pictureBox around
        Private Sub pictureBox1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pictureBox1.MouseDown
            dragging = True

            ' (e.x, e.y) represent the coordinates of the cursor relative to the
            ' pictureBox's location. We need to save these on mouseDown in order
            ' to calculate the distance dragged.
            oldX = e.X
            oldY = e.Y
        End Sub

        ' Mouse event handler to effect dragging the pictureBox around.
        Private Sub pictureBox1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pictureBox1.MouseMove
            If dragging Then
                Dim minY As Integer = VScrollBar1.Minimum
                Dim maxY As Integer = VScrollBar1.Maximum
                Dim minX As Integer = HScrollBar1.Minimum
                Dim maxX As Integer = HScrollBar1.Maximum
                Dim value As Integer = CInt(VScrollBar1.Value - (e.Y - oldY) / vScrollMultiplier)
                If value < minY Then

                    ' The new y position of the scrollbar is
                    ' (old_y_Position - y_distance_that_the_cursor_has_moved).
                    ' We want to simulate "real" scrolling so we move the
                    ' ScrollBarCtl in the opposite direction of the pictureBox.
                    ' We need the multiplier to convert pixels to scrollbar ticks.
                    VScrollBar1.Value = minY
                Else
                    If value > maxY - VScrollBar1.LargeChange + 1 Then
                        VScrollBar1.Value = maxY - VScrollBar1.LargeChange + 1
                    Else
                        VScrollBar1.Value = value
                    End If

                End If
                ' Similarly for the x position...
                value = CInt(HScrollBar1.Value - (e.X - oldX) / hScrollMultiplier)
                If value < minX Then
                    HScrollBar1.Value = minX
                Else
                    If value > maxX - HScrollBar1.LargeChange + 1 Then
                        HScrollBar1.Value = maxX - HScrollBar1.LargeChange + 1
                    Else
                        HScrollBar1.Value = value
                    End If

                End If
                label11.Text = HScrollBar1.Value.ToString()
                label12.Text = VScrollBar1.Value.ToString()

                ' The pictureBox moves with the mouse. Thus the new y coordinate is
                ' (old_y_Position + y_distance_that_the_cursor_has_moved), and similarly
                ' for the x coordinate.
                value = pictureBox1.Top + (e.Y - oldY)
                If value < VScrollBar1.Top Then
                    value = VScrollBar1.Top
                Else
                    If value > VScrollBar1.Bottom - pictureBox1.Height Then
                        value = VScrollBar1.Bottom - pictureBox1.Height
                    End If
                End If
                pictureBox1.Top = value

                value = pictureBox1.Left + (e.X - oldX)
                If value < HScrollBar1.Left Then
                    value = HScrollBar1.Left
                Else
                    If value > HScrollBar1.Right - pictureBox1.Width Then
                        value = HScrollBar1.Right - pictureBox1.Width
                    End If
                End If
                pictureBox1.Left = value
            End If
        End Sub
        ' Mouse event handler to signal the end of the pictureBox dragging
        ' sequence.
        Private Sub pictureBox1_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pictureBox1.MouseUp
            dragging = False
        End Sub
#Region " Windows Form Designer generated code "

        'Required by the Windows Form Designer
        Private components As System.ComponentModel.Container
        Protected WithEvents VScrollBar1 As System.Windows.Forms.VScrollBar
        Protected WithEvents HScrollBar1 As System.Windows.Forms.HScrollBar
        Protected WithEvents groupBox1 As System.Windows.Forms.GroupBox
        Protected WithEvents label1 As System.Windows.Forms.Label
        Protected WithEvents label2 As System.Windows.Forms.Label
        Protected WithEvents comboBox2 As System.Windows.Forms.ComboBox
        Protected WithEvents pictureBox1 As System.Windows.Forms.PictureBox
        Protected WithEvents label3 As System.Windows.Forms.Label
        Protected WithEvents label4 As System.Windows.Forms.Label
        Protected WithEvents label5 As System.Windows.Forms.Label
        Protected WithEvents label7 As System.Windows.Forms.Label
        Protected WithEvents label6 As System.Windows.Forms.Label
        Protected WithEvents label12 As System.Windows.Forms.Label
        Protected WithEvents label11 As System.Windows.Forms.Label
        Protected WithEvents label8 As System.Windows.Forms.Label
        Protected WithEvents label9 As System.Windows.Forms.Label
        Protected WithEvents label10 As System.Windows.Forms.Label
        Protected WithEvents comboBox1 As System.Windows.Forms.ComboBox

        Private WithEvents ScrollBarCtl As System.Windows.Forms.Form

        '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 TextBox1 As System.Windows.Forms.TextBox
        Private Sub InitializeComponent()
            Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(ctlScrollBar))
            Me.label11 = New System.Windows.Forms.Label
            Me.label10 = New System.Windows.Forms.Label
            Me.VScrollBar1 = New System.Windows.Forms.VScrollBar
            Me.label12 = New System.Windows.Forms.Label
            Me.comboBox1 = New System.Windows.Forms.ComboBox
            Me.HScrollBar1 = New System.Windows.Forms.HScrollBar
            Me.groupBox1 = New System.Windows.Forms.GroupBox
            Me.label7 = New System.Windows.Forms.Label
            Me.label6 = New System.Windows.Forms.Label
            Me.comboBox2 = New System.Windows.Forms.ComboBox
            Me.pictureBox1 = New System.Windows.Forms.PictureBox
            Me.label4 = New System.Windows.Forms.Label
            Me.label5 = New System.Windows.Forms.Label
            Me.label8 = New System.Windows.Forms.Label
            Me.label9 = New System.Windows.Forms.Label
            Me.label2 = New System.Windows.Forms.Label
            Me.label3 = New System.Windows.Forms.Label
            Me.label1 = New System.Windows.Forms.Label
            Me.TextBox1 = New System.Windows.Forms.TextBox
            Me.groupBox1.SuspendLayout()
            Me.SuspendLayout()
            '
            'label11
            '
            Me.label11.Location = New System.Drawing.Point(408, 160)
            Me.label11.Name = "label11"
            Me.label11.Size = New System.Drawing.Size(72, 16)
            Me.label11.TabIndex = 4
            Me.label11.Text = "Label11"
            '
            'label10
            '
            Me.label10.Location = New System.Drawing.Point(216, 168)
            Me.label10.Name = "label10"
            Me.label10.Size = New System.Drawing.Size(24, 16)
            Me.label10.TabIndex = 13
            Me.label10.Text = "100"
            '
            'VScrollBar1
            '
            Me.VScrollBar1.Location = New System.Drawing.Point(200, 24)
            Me.VScrollBar1.Minimum = -100
            Me.VScrollBar1.Name = "VScrollBar1"
            Me.VScrollBar1.Size = New System.Drawing.Size(16, 152)
            Me.VScrollBar1.TabIndex = 2
            '
            'label12
            '
            Me.label12.Location = New System.Drawing.Point(408, 184)
            Me.label12.Name = "label12"
            Me.label12.Size = New System.Drawing.Size(72, 16)
            Me.label12.TabIndex = 7
            Me.label12.Text = "label12"
            '
            'comboBox1
            '
            Me.comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
            Me.comboBox1.Items.AddRange(New Object() {"20", "10", "5"})
            Me.comboBox1.Location = New System.Drawing.Point(136, 24)
            Me.comboBox1.Name = "comboBox1"
            Me.comboBox1.Size = New System.Drawing.Size(96, 21)
            Me.comboBox1.TabIndex = 0
            '
            'HScrollBar1
            '
            Me.HScrollBar1.Location = New System.Drawing.Point(16, 176)
            Me.HScrollBar1.Minimum = -100
            Me.HScrollBar1.Name = "HScrollBar1"
            Me.HScrollBar1.Size = New System.Drawing.Size(184, 16)
            Me.HScrollBar1.TabIndex = 3
            '
            'groupBox1
            '
            Me.groupBox1.Controls.Add(Me.label7)
            Me.groupBox1.Controls.Add(Me.label6)
            Me.groupBox1.Controls.Add(Me.comboBox2)
            Me.groupBox1.Controls.Add(Me.comboBox1)
            Me.groupBox1.Location = New System.Drawing.Point(264, 16)
            Me.groupBox1.Name = "groupBox1"
            Me.groupBox1.Size = New System.Drawing.Size(248, 264)
            Me.groupBox1.TabIndex = 5
            Me.groupBox1.TabStop = False
            Me.groupBox1.Text = "HScrollBar and VScrollBar"
            '
            'label7
            '
            Me.label7.Location = New System.Drawing.Point(16, 168)
            Me.label7.Name = "label7"
            Me.label7.Size = New System.Drawing.Size(92, 16)
            Me.label7.TabIndex = 2
            Me.label7.Text = "VScrollBar.value"
            '
            'label6
            '
            Me.label6.Location = New System.Drawing.Point(16, 144)
            Me.label6.Name = "label6"
            Me.label6.Size = New System.Drawing.Size(92, 32)
            Me.label6.TabIndex = 3
            Me.label6.Text = "HScrollBar.value"
            '
            'comboBox2
            '
            Me.comboBox2.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
            Me.comboBox2.Items.AddRange(New Object() {"5", "2", "1"})
            Me.comboBox2.Location = New System.Drawing.Point(136, 48)
            Me.comboBox2.Name = "comboBox2"
            Me.comboBox2.Size = New System.Drawing.Size(96, 21)
            Me.comboBox2.TabIndex = 1
            '
            'pictureBox1
            '
            Me.pictureBox1.Image = CType(resources.GetObject("pictureBox1.Image"), System.Drawing.Image)
            Me.pictureBox1.Location = New System.Drawing.Point(64, 32)
            Me.pictureBox1.Name = "pictureBox1"
            Me.pictureBox1.Size = New System.Drawing.Size(96, 96)
            Me.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage
            Me.pictureBox1.TabIndex = 6
            Me.pictureBox1.TabStop = False
            Me.pictureBox1.Text = "pictureBox1"
            '
            'label4
            '
            Me.label4.Location = New System.Drawing.Point(184, 192)
            Me.label4.Name = "label4"
            Me.label4.Size = New System.Drawing.Size(24, 16)
            Me.label4.TabIndex = 9
            Me.label4.Text = "100"
            '
            'label5
            '
            Me.label5.Location = New System.Drawing.Point(112, 200)
            Me.label5.Name = "label5"
            Me.label5.Size = New System.Drawing.Size(32, 16)
            Me.label5.TabIndex = 10
            Me.label5.Text = "0"
            '
            'label8
            '
            Me.label8.Location = New System.Drawing.Point(224, 24)
            Me.label8.Name = "label8"
            Me.label8.Size = New System.Drawing.Size(32, 16)
            Me.label8.TabIndex = 11
            Me.label8.Text = "-100"
            '
            'label9
            '
            Me.label9.Location = New System.Drawing.Point(224, 96)
            Me.label9.Name = "label9"
            Me.label9.Size = New System.Drawing.Size(16, 16)
            Me.label9.TabIndex = 12
            Me.label9.Text = "0"
            '
            'label2
            '
            Me.label2.Location = New System.Drawing.Point(280, 64)
            Me.label2.Name = "label2"
            Me.label2.Size = New System.Drawing.Size(92, 16)
            Me.label2.TabIndex = 1
            Me.label2.Text = "Small Change"
            '
            'label3
            '
            Me.label3.Location = New System.Drawing.Point(8, 200)
            Me.label3.Name = "label3"
            Me.label3.Size = New System.Drawing.Size(32, 16)
            Me.label3.TabIndex = 8
            Me.label3.Text = "-100"
            '
            'label1
            '
            Me.label1.Location = New System.Drawing.Point(280, 40)
            Me.label1.Name = "label1"
            Me.label1.Size = New System.Drawing.Size(92, 16)
            Me.label1.TabIndex = 0
            Me.label1.Text = "Large Change"
            '
            'TextBox1
            '
            Me.TextBox1.Location = New System.Drawing.Point(56, 136)
            Me.TextBox1.Name = "TextBox1"
            Me.TextBox1.Size = New System.Drawing.Size(104, 20)
            Me.TextBox1.TabIndex = 14
            Me.TextBox1.Text = "TextBox1"
            '
            'ctlScrollBar
            '
            Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
            Me.ClientSize = New System.Drawing.Size(520, 294)
            Me.Controls.Add(Me.TextBox1)
            Me.Controls.Add(Me.label12)
            Me.Controls.Add(Me.label11)
            Me.Controls.Add(Me.label10)
            Me.Controls.Add(Me.label9)
            Me.Controls.Add(Me.label8)
            Me.Controls.Add(Me.label5)
            Me.Controls.Add(Me.label4)
            Me.Controls.Add(Me.label3)
            Me.Controls.Add(Me.label2)
            Me.Controls.Add(Me.label1)
            Me.Controls.Add(Me.groupBox1)
            Me.Controls.Add(Me.HScrollBar1)
            Me.Controls.Add(Me.VScrollBar1)
            Me.Controls.Add(Me.pictureBox1)
            Me.Name = "ctlScrollBar"
            Me.Text = "VScrollBar"
            Me.groupBox1.ResumeLayout(False)
            Me.ResumeLayout(False)

        End Sub

#End Region

        ' The main entry point for the application.
        <STAThread()> Public Shared Sub Main()
            Application.Run(New ctlScrollBar)
        End Sub
        Private Sub TextBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseDown

        dragging = True

            ' (e.x, e.y) represent the coordinates of the cursor relative to the
            ' pictureBox's location. We need to save these on mouseDown in order
            ' to calculate the distance dragged.
            oldX = e.X
            oldY = e.Y
        End Sub
        Private Sub TextBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseMove
            dragging = True
            If dragging Then
                Dim minY As Integer = VScrollBar1.Minimum
                Dim maxY As Integer = VScrollBar1.Maximum
                Dim minX As Integer = HScrollBar1.Minimum
                Dim maxX As Integer = HScrollBar1.Maximum
                Dim value As Integer = CInt(VScrollBar1.Value - (e.Y - oldY) / vScrollMultiplier)
                If value < minY Then

                    ' The new y position of the scrollbar is
                    ' (old_y_Position - y_distance_that_the_cursor_has_moved).
                    ' We want to simulate "real" scrolling so we move the
                    ' ScrollBarCtl in the opposite direction of the pictureBox.
                    ' We need the multiplier to convert pixels to scrollbar ticks.
                    VScrollBar1.Value = minY
                Else
                    If value > maxY - VScrollBar1.LargeChange + 1 Then
                        VScrollBar1.Value = maxY - VScrollBar1.LargeChange + 1
                    Else
                        VScrollBar1.Value = value
                    End If

                End If
                ' Similarly for the x position...
                value = CInt(HScrollBar1.Value - (e.X - oldX) / hScrollMultiplier)
                If value < minX Then
                    HScrollBar1.Value = minX
                Else
                    If value > maxX - HScrollBar1.LargeChange + 1 Then
                        HScrollBar1.Value = maxX - HScrollBar1.LargeChange + 1
                    Else
                        HScrollBar1.Value = value
                    End If

                End If
                label11.Text = HScrollBar1.Value.ToString()
                label12.Text = VScrollBar1.Value.ToString()

                ' The pictureBox moves with the mouse. Thus the new y coordinate is
                ' (old_y_Position + y_distance_that_the_cursor_has_moved), and similarly
                ' for the x coordinate.
                value = TextBox1.Top + (e.Y - oldY)
                If value < VScrollBar1.Top Then
                    value = VScrollBar1.Top
                Else
                    If value > VScrollBar1.Bottom - TextBox1.Height Then
                        value = VScrollBar1.Bottom - TextBox1.Height
                    End If
                End If
                TextBox1.Top = value

                value = TextBox1.Left + (e.X - oldX)
                If value < HScrollBar1.Left Then
                    value = HScrollBar1.Left
                Else
                    If value > HScrollBar1.Right - TextBox1.Width Then
                        value = HScrollBar1.Right - TextBox1.Width
                    End If
                End If
                TextBox1.Left = value
            End If
        End Sub

    End Class
End Namespace
0
 
mross3Author Commented:
Thanks for repsonding.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.