|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionRecently I needed a very simple meter embedded into a Using the CodeStart by creating a new class within your project that inherits the label class: Public Class LabelWithBar
Inherits Label
End Class
By inheriting from the Now add three properties: Public Property Value() As Integer
Get
Return mVal
End Get
Set(ByVal Value As Integer)
' Make sure that the value does not stray outside the valid range.
Select Case Value
Case Is < 0
mVal = 0
Case Is > 100
mVal = 100
Case Else
mVal = Value
End Select
' Invalidate the control to get a repaint.
Me.Invalidate()
End Set
End Property
Public Property BarColor() As Color
Get
Return mbarColor
End Get
Set(ByVal Value As Color)
mbarColor = Value
' Invalidate the control to get a repaint.
Me.Invalidate()
End Set
End Property
Public Property BarHeight() As Integer
Get
Return mbarHeight
End Get
Set(ByVal value As Integer)
Select Case value
Case Is > Me.Size.Height, Is < 0
mbarHeight = Me.Size.Height
Case Else
mbarHeight = value
End Select
' Invalidate the control to get a repaint.
Me.Invalidate()
End Set
End Property
The Now we can draw the bar by overriding the Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
Dim g As Graphics = e.Graphics
Dim percent As Decimal = mVal / 100
Dim brush As SolidBrush = New SolidBrush(BarColor)
Dim rect As Rectangle = e.ClipRectangle
Dim height As Integer = mbarHeight
rect.Width = rect.Width * percent
If height = 0 Then height = rect.Height
rect.Y = (rect.Height - height) / 2
rect.Height = height
' Draw bar
g.FillRectangle(brush, rect)
MyBase.OnPaint(e)
End Sub
This function first draws a bar with the specified color, height, and width. It then calls the That is really all you need to be on your way. You can now edit the controls To improve the custom control, it is desirable to tell Visual Studio what the default values are for each property. Do this by adding the default values to each class using the Now the final step is creating a version capable of being added to the <System.ComponentModel.DesignerCategory("code")> _
<ToolStripItemDesignerAvailability_
(ToolStripItemDesignerAvailability.StatusStrip)> _
Public Class ToolStripStatusLabelWithBar
Inherits ToolStripStatusLabel
End Class
The attributes preceding the class instruct Visual Studio that this class can be embedded in a The complete code can be downloaded from the link at the top of this article. Points of InterestWhile I have created a few custom controls in the past this was my first time delving deeply into the design time attributes. Overall this project would have taken less than a day if it wasn't for the following false starts. In my first attempt at creating the In my first attempt at creating the History
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||