Click here to Skip to main content
15,886,091 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a project with a somewhat custom interface but have yet to find a custom scroll bar control that looks and works well, I have found this code online which does work with and track bar which is good because there are plenty of nice looking customizable track bar controls out there. The problem is the code works in the opposite way so if I scroll up it scrolls down and the maximum is like the minimum. I think this is because the track bar can only be flipped vertical one way with the maximum at the bottom instead of the top. So can the code I have below be modifiyed to work or is there a nice looking custom scroll bar for vb.net?

VB
Private Sub Panel1_Paint(sender As Object, e As PaintEventArgs) Handles Panel1.Paint
    Dim pt As New Point(Me.Panel1.AutoScrollPosition.X, Me.Panel1.AutoScrollPosition.Y)
    Me.CustomScrollBar1.Minimum = 0
    Me.CustomScrollBar1.Maximum = Me.Panel1.DisplayRectangle.Height
    Me.CustomScrollBar1.Value = Math.Abs(Me.Panel1.AutoScrollPosition.Y)
    Me.CustomScrollBar1.Refresh()
    Me.CustomScrollBar1.Update()
End Sub

Private Sub CustomScrollBar1_Scroll(sender As Object, e As ScrollEventArgs) Handles CustomScrollBar1.Scroll
    Panel1.AutoScrollPosition = New Point(5, CustomScrollBar1.Value)
    CustomScrollBar1.Invalidate()
End Sub

Private Sub CustomScrollBar1_ValueChanged() Handles CustomScrollBar1.ValueChanged
    Panel1.AutoScrollPosition = New Point(5, CustomScrollBar1.Value)
    CustomScrollBar1.Invalidate()
End Sub


Thanks for any help
Posted

Here's what I came up with to get this to do what I wanted... However there are 2 sets of scrollbars... The one for the Panel at 17 pixels and the ones that I designed at 40 pixels.

While not perfect, this methodology seems to work!

---

VB
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.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 Panel1 As System.Windows.Forms.Panel
   Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
   Friend WithEvents Panel2 As System.Windows.Forms.Panel
   Friend WithEvents PictureBox2 As System.Windows.Forms.PictureBox
   Friend WithEvents HScrollBar1 As System.Windows.Forms.HScrollBar
   Friend WithEvents Panel3 As System.Windows.Forms.Panel
   Friend WithEvents VScrollBar1 As System.Windows.Forms.VScrollBar
   Friend WithEvents Splitter2 As System.Windows.Forms.Splitter
   Friend WithEvents Splitter1 As System.Windows.Forms.Splitter
    Private Sub InitializeComponent()
      Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1))
      Me.Panel1 = New System.Windows.Forms.Panel
      Me.Panel3 = New System.Windows.Forms.Panel
      Me.VScrollBar1 = New System.Windows.Forms.VScrollBar
      Me.Panel2 = New System.Windows.Forms.Panel
      Me.HScrollBar1 = New System.Windows.Forms.HScrollBar
      Me.PictureBox2 = New System.Windows.Forms.PictureBox
      Me.PictureBox1 = New System.Windows.Forms.PictureBox
      Me.Splitter2 = New System.Windows.Forms.Splitter
      Me.Splitter1 = New System.Windows.Forms.Splitter
      Me.Panel1.SuspendLayout()
      Me.Panel3.SuspendLayout()
      Me.Panel2.SuspendLayout()
      Me.SuspendLayout()
      '
      'Panel1
      '
      Me.Panel1.AutoScroll = True
      Me.Panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
      Me.Panel1.Controls.Add(Me.PictureBox1)
      Me.Panel1.Dock = System.Windows.Forms.DockStyle.Fill
      Me.Panel1.Location = New System.Drawing.Point(0, 0)
      Me.Panel1.Name = "Panel1"
      Me.Panel1.Size = New System.Drawing.Size(494, 408)
      Me.Panel1.TabIndex = 0
      '
      'Panel3
      '
      Me.Panel3.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
      Me.Panel3.Controls.Add(Me.Splitter1)
      Me.Panel3.Controls.Add(Me.VScrollBar1)
      Me.Panel3.Controls.Add(Me.PictureBox2)
      Me.Panel3.Dock = System.Windows.Forms.DockStyle.Right
      Me.Panel3.Location = New System.Drawing.Point(494, 0)
      Me.Panel3.Name = "Panel3"
      Me.Panel3.Size = New System.Drawing.Size(42, 450)
      Me.Panel3.TabIndex = 2
      '
      'VScrollBar1
      '
      Me.VScrollBar1.Dock = System.Windows.Forms.DockStyle.Fill
      Me.VScrollBar1.LargeChange = 50
      Me.VScrollBar1.Location = New System.Drawing.Point(0, 0)
      Me.VScrollBar1.Name = "VScrollBar1"
      Me.VScrollBar1.Size = New System.Drawing.Size(40, 408)
      Me.VScrollBar1.TabIndex = 1
      '
      'Panel2
      '
      Me.Panel2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
      Me.Panel2.Controls.Add(Me.HScrollBar1)
      Me.Panel2.Dock = System.Windows.Forms.DockStyle.Bottom
      Me.Panel2.Location = New System.Drawing.Point(0, 408)
      Me.Panel2.Name = "Panel2"
      Me.Panel2.Size = New System.Drawing.Size(494, 42)
      Me.Panel2.TabIndex = 1
      '
      'HScrollBar1
      '
      Me.HScrollBar1.Dock = System.Windows.Forms.DockStyle.Fill
      Me.HScrollBar1.LargeChange = 50
      Me.HScrollBar1.Location = New System.Drawing.Point(0, 0)
      Me.HScrollBar1.Name = "HScrollBar1"
      Me.HScrollBar1.Size = New System.Drawing.Size(492, 40)
      Me.HScrollBar1.TabIndex = 2
      '
      'PictureBox2
      '
      Me.PictureBox2.Dock = System.Windows.Forms.DockStyle.Bottom
      Me.PictureBox2.Image = CType(resources.GetObject("PictureBox2.Image"), System.Drawing.Image)
      Me.PictureBox2.Location = New System.Drawing.Point(0, 408)
      Me.PictureBox2.Name = "PictureBox2"
      Me.PictureBox2.Size = New System.Drawing.Size(40, 40)
      Me.PictureBox2.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize
      Me.PictureBox2.TabIndex = 0
      Me.PictureBox2.TabStop = False
      '
      'PictureBox1
      '
      Me.PictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
      Me.PictureBox1.Location = New System.Drawing.Point(380, 328)
      Me.PictureBox1.Name = "PictureBox1"
      Me.PictureBox1.Size = New System.Drawing.Size(368, 316)
      Me.PictureBox1.TabIndex = 0
      Me.PictureBox1.TabStop = False
      '
      'Splitter2
      '
      Me.Splitter2.Dock = System.Windows.Forms.DockStyle.Bottom
      Me.Splitter2.Enabled = False
      Me.Splitter2.Location = New System.Drawing.Point(0, 407)
      Me.Splitter2.Name = "Splitter2"
      Me.Splitter2.Size = New System.Drawing.Size(494, 1)
      Me.Splitter2.TabIndex = 3
      Me.Splitter2.TabStop = False
      '
      'Splitter1
      '
      Me.Splitter1.BackColor = System.Drawing.Color.Black
      Me.Splitter1.Dock = System.Windows.Forms.DockStyle.Bottom
      Me.Splitter1.Location = New System.Drawing.Point(0, 407)
      Me.Splitter1.Name = "Splitter1"
      Me.Splitter1.Size = New System.Drawing.Size(40, 1)
      Me.Splitter1.TabIndex = 2
      Me.Splitter1.TabStop = False
      Me.Splitter1.Visible = False
      '
      'Form1
      '
      Me.AutoScale = False
      Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
      Me.ClientSize = New System.Drawing.Size(536, 450)
      Me.Controls.Add(Me.Splitter2)
      Me.Controls.Add(Me.Panel1)
      Me.Controls.Add(Me.Panel2)
      Me.Controls.Add(Me.Panel3)
      Me.Name = "Form1"
      Me.Text = "Form1"
      Me.Panel1.ResumeLayout(False)
      Me.Panel3.ResumeLayout(False)
      Me.Panel2.ResumeLayout(False)
      Me.ResumeLayout(False)

   End Sub

#End Region

   Dim PictureRightSize As Integer
   Dim PictureBottomSize As Integer

   Dim Panel1Width As Integer
   Dim Panel1Height As Integer

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

      'Panel1.AutoScroll = True
      'MsgBox("Sadly the Normal Scrollbar WIDTH and HEIGHT don't seem to be easily resized at this point in time!")

      RecalculateFormScrollBarData()

   End Sub

   Private Sub RecalculateFormScrollBarData()

      ' +17 is for the Panel ScrollBars then Add 10 so that we have a little "wiggle" room!
      Dim ScrollSize As Integer = Panel2.Height + 17 + 10

      Panel1Width = Panel1.Width
      Panel1Height = Panel1.Height

      PictureRightSize = -Panel1.AutoScrollPosition.X + PictureBox1.Location.X + PictureBox1.Width + ScrollSize - Panel1Width ' + PictureBox1.Width + ScrollSize
      PictureBottomSize = -Panel1.AutoScrollPosition.Y + PictureBox1.Location.Y + PictureBox1.Height + ScrollSize - Panel1Height ' + PictureBox1.Height + ScrollSize

      HScrollBar1.Maximum = PictureRightSize
      VScrollBar1.Maximum = PictureBottomSize

      'MsgBox("Panel Bottom Right X, Y Data: " & PictureRightSize & ", " & PictureBottomSize & vbCrLf & vbCrLf & _
      '       "Current Panel X, Y Data: " & Panel1Width & ", " & Panel1Height)

   End Sub

   Private Sub Form1_Resize(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Resize

      RecalculateFormScrollBarData()

   End Sub

   Private Sub VScrollBar1_Scroll(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles VScrollBar1.Scroll

      'Panel1.AutoScroll = True
      Panel1.AutoScrollPosition = New Point(HScrollBar1.Value, VScrollBar1.Value)
      Panel1.Invalidate()

   End Sub


   Private Sub HScrollBar1_Scroll(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles HScrollBar1.Scroll

      'Panel1.AutoScroll = True
      Panel1.AutoScrollPosition = New Point(HScrollBar1.Value, VScrollBar1.Value)
      Panel1.Invalidate()

   End Sub

End Class

---

In the RecalculateFormScrollBarData routine, the -Panel1.AutoScrollPosition.X and -Panel1.AutoScrollPosition.Y in the calculations allow for the Panel1 Current Location Offset that we are currently viewing is taken into consideration.

So this is my best solution at the present time.

So this seems to temporarily solve the too small scrollbars for the present time!
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900