Public Class svToolbar
Inherits System.Windows.Forms.Panel
#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 ToolTip1 As System.Windows.Forms.ToolTip
<system.diagnostics.debuggerstepthrough> Private Sub InitializeComponent()
Me.components = New System.ComponentModel.Container
Me.ToolTip1 = New System.Windows.Forms.ToolTip(Me.components)
End Sub
#End Region
#Region "Declarations"
#Region "Constants & Variables"
Private m_rbBackcolorSelected As Color = Color.FromName("ControlDark")
Private m_rbBackcolorUnselected As Color = Color.FromName("Control")
#End Region
#Region "Events"
Public Event ButtonClicked(ByVal but As Button)
Public Event RadioOptionChanged(ByVal rbut As RadioButton)
#End Region
#End Region
#Region "Public Methods"
Public Function ControlIndexByName(ByVal Value As String) As Integer
Dim bFound As Boolean = False
For i As Integer = 0 To Me.Controls.Count - 1
If Me.Controls(i).Name = Value Then
'use the VBClassic style
'rather than immediately returning
bFound = True
ControlIndexByName = i
Exit For
End If
Next
If Not bFound Then
ControlIndexByName = -1
End If
End Function
#End Region
#Region "Private Methods"
Private Sub DrawControls()
Dim ctl As Control
Dim previousControl As Control
For i As Integer = 0 To MyBase.Controls.Count - 1
ctl = MyBase.Controls(i)
'textboxes don't extend unless they're multiline
'and multilines don't look nice short, just force to align middle
ctl.Height = Me.ClientRectangle.Height - 6
ctl.Top = (Me.ClientRectangle.Height \ 2) - (ctl.Height \ 2)
If i = 0 Then
ctl.Left = 0
Else
previousControl = PreviousVisibleControl(i)
ctl.Left = previousControl.Left + previousControl.Width
End If
Next
End Sub
Protected Overrides Sub OnControlAdded(ByVal e As System.Windows.Forms.ControlEventArgs)
If e.Control.Visible Then
DrawControls()
End If
If e.Control.GetType Is GetType(Button) Then
AddHandler CType(e.Control, Button).Click, AddressOf evtButtonClick
ToolTip1.SetToolTip(e.Control, e.Control.Text)
e.Control.Text = ""
ElseIf e.Control.GetType Is GetType(RadioButton) Then
AddHandler CType(e.Control, RadioButton).CheckedChanged, AddressOf evtRadioOptionChanged
ToolTip1.SetToolTip(e.Control, e.Control.Text)
e.Control.Text = ""
'make the selected rb easy to see
If CType(e.Control, RadioButton).Checked Then
e.Control.BackColor = m_rbBackcolorSelected
Else
e.Control.BackColor = m_rbBackcolorUnselected
End If
End If
'all controls need to tell when to redraw the bar
AddHandler e.Control.VisibleChanged, AddressOf evtVisibleChanged
End Sub
Protected Overrides Sub OnControlRemoved(ByVal e As System.Windows.Forms.ControlEventArgs)
If e.Control.Visible Then
DrawControls()
End If
If e.Control.GetType Is GetType(Button) Then
RemoveHandler CType(e.Control, Button).Click, AddressOf evtButtonClick
ElseIf e.Control.GetType Is GetType(RadioButton) Then
RemoveHandler CType(e.Control, RadioButton).CheckedChanged, AddressOf evtRadioOptionChanged
End If
'all controls need to tell when to redraw the bar
RemoveHandler e.Control.VisibleChanged, AddressOf evtVisibleChanged
End Sub
Private Function PreviousVisibleControl(ByVal currentControlIndex As Integer) As Control
'go backwards till you find a visible control
For i As Integer = currentControlIndex - 1 To 0 Step -1
If MyBase.Controls(i).Visible Then
Return MyBase.Controls(i)
End If
Next
End Function
#End Region
#Region "Event Handlers"
Private Sub evtButtonClick(ByVal sender As Object, ByVal e As EventArgs)
RaiseEvent ButtonClicked(CType(sender, Button))
End Sub
Private Sub evtRadioOptionChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim tmpSender As RadioButton = DirectCast(sender, RadioButton)
If tmpSender.Checked Then
tmpSender.BackColor = m_rbBackcolorSelected
RaiseEvent RadioOptionChanged(tmpSender)
Else
tmpSender.BackColor = m_rbBackcolorUnselected
End If
End Sub
Private Sub evtVisibleChanged(ByVal sender As Object, ByVal e As EventArgs)
DrawControls()
End Sub
#End Region
End Class