Public Class LabelScroller
Inherits System.Windows.Forms.UserControl
#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
'UserControl 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 lblMain As System.Windows.Forms.Label
Friend WithEvents vsBar As System.Windows.Forms.VScrollBar
Friend WithEvents pnlvsBar As System.Windows.Forms.Panel
Friend WithEvents pnllblMain As System.Windows.Forms.Panel
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.lblMain = New System.Windows.Forms.Label
Me.vsBar = New System.Windows.Forms.VScrollBar
Me.pnlvsBar = New System.Windows.Forms.Panel
Me.pnllblMain = New System.Windows.Forms.Panel
Me.pnlvsBar.SuspendLayout()
Me.pnllblMain.SuspendLayout()
Me.SuspendLayout()
'
'lblMain
'
Me.lblMain.Dock = System.Windows.Forms.DockStyle.Fill
Me.lblMain.Location = New System.Drawing.Point(0, 0)
Me.lblMain.Name = "lblMain"
Me.lblMain.Size = New System.Drawing.Size(200, 40)
Me.lblMain.TabIndex = 0
Me.lblMain.TextAlign = System.Drawing.ContentAlignment.TopCenter
'
'vsBar
'
Me.vsBar.Dock = System.Windows.Forms.DockStyle.Fill
Me.vsBar.Location = New System.Drawing.Point(0, 0)
Me.vsBar.Name = "vsBar"
Me.vsBar.Size = New System.Drawing.Size(16, 40)
Me.vsBar.TabIndex = 1
'
'pnlvsBar
'
Me.pnlvsBar.Controls.Add(Me.vsBar)
Me.pnlvsBar.Dock = System.Windows.Forms.DockStyle.Right
Me.pnlvsBar.Location = New System.Drawing.Point(200, 0)
Me.pnlvsBar.Name = "pnlvsBar"
Me.pnlvsBar.Size = New System.Drawing.Size(16, 40)
Me.pnlvsBar.TabIndex = 2
'
'pnllblMain
'
Me.pnllblMain.Controls.Add(Me.lblMain)
Me.pnllblMain.Dock = System.Windows.Forms.DockStyle.Fill
Me.pnllblMain.Location = New System.Drawing.Point(0, 0)
Me.pnllblMain.Name = "pnllblMain"
Me.pnllblMain.Size = New System.Drawing.Size(200, 40)
Me.pnllblMain.TabIndex = 3
'
'LabelScroller
'
Me.Controls.Add(Me.pnllblMain)
Me.Controls.Add(Me.pnlvsBar)
Me.Name = "LabelScroller"
Me.Size = New System.Drawing.Size(216, 40)
Me.pnlvsBar.ResumeLayout(False)
Me.pnllblMain.ResumeLayout(False)
Me.ResumeLayout(False)
End Sub
#End Region
'Holds lblMain.text property
Private _Message As String
Private _arr() As String
'numof lines needed to display message
Private _NumOfLines As Int32
'Control bounds
Private _MaxNumOfLines As Int32
Private _MaxNumOfChars As Int32
'ScrollBar Valus
Private _VSValue As Int32
'AutoScroll Attributes
Private _blnScrolling As Boolean
Private _ScrollInterval As Int32 = 1000
Private WithEvents _ScrollTimer As Timer
Public Overrides Property Text() As String
Get
Return _Message
End Get
Set(ByVal Value As String)
_Message = Value
Call Makeithappen()
End Set
End Property
Private Sub Makeithappen()
Call FindLabelBounds()
Call FindMessageTotalLines()
Call ProportionvsBar()
Call PopulatelblMaintext()
End Sub
Friend Property ScrollInterval() As Int32
Get
Return _ScrollInterval
End Get
Set(ByVal Value As Int32)
If _blnScrolling Then
Call ScrollStop()
_ScrollInterval = Value
Call ScrollStart()
Else
_ScrollInterval = Value
End If
End Set
End Property
Friend Sub ScrollStart()
_blnScrolling = True
_ScrollTimer = New Timer
_ScrollTimer.Interval = _ScrollInterval
_ScrollTimer.Start()
End Sub
Friend Sub ScrollStop()
_ScrollTimer.Stop()
_blnScrolling = False
End Sub
Private Sub _ScrollTimer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles _ScrollTimer.Tick
If _VSValue <> _NumOfLines Then
vsBar.Value += 1
Else
vsBar.Value = 0
End If
End Sub
Private Sub FindLabelBounds()
'Algorithm base(what I tested it against)
'If labelwidth is 200; the max characters in a line is 31. Ratio 0.155
'If labelheight is 40; the max lines is 3. Ratio 0.075
'(Dev Note: screw font)
_MaxNumOfChars = lblMain.Width * 0.155
_MaxNumOfLines = lblMain.Height * 0.075
End Sub
Private Sub FindMessageTotalLines()
ReDim _arr(0) 'clear it out
Dim arr() As String
Dim strHold As String
Dim intLineTotal As Int32 = 0
If Not _Message Is Nothing Then
arr = _Message.Split(" ")
For intCnt As Int32 = 0 To UBound(arr)
strHold += arr(intCnt) & " "
If (strHold.Length > _MaxNumOfChars) Or (intCnt = UBound(arr)) Then
intLineTotal += 1
ReDim Preserve _arr(intLineTotal)
_arr(intLineTotal - 1) = strHold
strHold = ""
End If
Next intCnt
_NumOfLines = intLineTotal
ReDim Preserve _arr(intLineTotal - 1)
End If
End Sub
Private Sub LabelScroller_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Resize
Call Makeithappen()
End Sub
Private Sub PopulatelblMaintext()
Me.lblMain.Text = ""
If Not _Message Is Nothing Then
For intcnt As Int32 = _VSValue To (_VSValue + _MaxNumOfLines) - 1
If Not intcnt > UBound(_arr) Then
lblMain.Text += _arr(intcnt) & vbNewLine
End If
Next
End If
End Sub
Friend ReadOnly Property MaxCharWidth() As Int32
Get
Return _MaxNumOfChars
End Get
End Property
Friend ReadOnly Property MaxCharHeight() As Int32
Get
Return _MaxNumOfLines
End Get
End Property
Private Sub vsBar_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles vsBar.ValueChanged
_VSValue = CType(sender, Windows.Forms.VScrollBar).Value
Call PopulatelblMaintext()
End Sub
Private Sub ProportionvsBar()
vsBar.SmallChange = 1
vsBar.LargeChange = _MaxNumOfLines - 1
If _MaxNumOfLines >= _NumOfLines Then
vsBar.Enabled = False
Else
vsBar.Enabled = True
vsBar.Maximum = _NumOfLines
vsBar.Minimum = 0
End If
End Sub
End Class