Figure 3 Custom Property in the Visual Basic Control ' Control properties. This is the interval at which the blinking label
' changes its color from on to off or back again, in seconds.
Private m_BlinkInterval As Integer = 1
Public Property BlinkInterval() As Integer
Get
Return m_BlinkInterval
End Get
' Property has changed. Remember its new value, and set the
' control's timer to use it.
Set(ByVal Value As Integer)
m_BlinkInterval = Value
myTimer.Interval = m_BlinkInterval * 1000
End Set
End Property
Figure 4 Using an Internal Timer Public Sub New()
MyBase.New()
' Hook up handler function for timer, and set timer variables
AddHandler myTimer.Elapsed, AddressOf OnTimerExpired
myTimer.AutoReset = True
myTimer.Interval = 1000
myTimer.Enabled = True
' Create brushes used for drawing label text
m_BlinkOnBrush = New System.Drawing.SolidBrush(Me.ForeColor)
m_BlinkOffBrush = New System.Drawing.SolidBrush(Me.BackColor)
End Sub
' Handler for our control's internal timer.
Private Sub OnTimerExpired(ByVal Source As Object,
ByVal e As System.Timers.ElapsedEventArgs)
' Toggle the flag the tells the painting code whether to use the
' BlinkOnColor or the BlinkOff color
If (m_UseBlinkOnColor = True) Then
m_UseBlinkOnColor = False
Else
m_UseBlinkOnColor = True
End If
' Invalidate the control to force a repaint.
Me.Invalidate()
' Fire the blink event to the control's container, in case it
' cares.
RaiseEvent BlinkStateChanged(m_UseBlinkOnColor)
End Sub
Figure 5 Paint Event Handler Private m_BlinkOnBrush As System.Drawing.SolidBrush
Private m_BlinkOffBrush As System.Drawing.SolidBrush
Private m_UseBlinkOnColor As Boolean = True
' This function gets called when the control needs painting
Protected Overrides Sub OnPaint(ByVal pe As _
System.Windows.Forms.PaintEventArgs)
Dim BrushToUse As System.Drawing.Brush
' Choose the brush to use for the text color. If the blink cycle is
' currently on, or if we're in design mode (in which case we never
' want to blink, select the first brush. Otherwise select the second.
If (m_UseBlinkOnColor = True Or Me.DesignMode = True) Then
BrushToUse = m_BlinkOnBrush
Else
BrushToUse = m_BlinkOffBrush
End If
' Draw the control's current Text property
pe.Graphics.DrawString (Me.Text, Me.Font, BrushToUse, 0, 0)
End Sub
Figure 8 Code of Inherited Control Public Class InheritTextBoxControl1VB
Inherits System.Windows.Forms.TextBox ' Check to see if string in text box appears to be a valid e-mail
' address. In this case, that means that it contains at least one @
' sign and at least one period.
Protected Overrides Sub OnTextChanged(ByVal e As System.EventArgs)
' Pass call to base class
MyBase.OnTextChanged(e)
' Perform our checking logic using inherited property Text, and
' set value of inherited property BackColor accordingly
If (Me.Text.IndexOf("@") <> -1 And Me.Text.IndexOf(".") <> -1)
Then
Me.BackColor = System.Drawing.Color.LightGreen
Else
Me.BackColor = System.Drawing.Color.LightPink
End If
End Sub
End Class
Figure 11 Sample User Control Public Class UserControlVB
Inherits System.Windows.Forms.UserControl ' User clicked OK.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles Button1.Click
Dim bFieldsValid As Boolean = True
' Check to make sure that required fields are filled in.
' Set error provider control to signal errors to the user if
' they're not.
If (TextBox1.Text.Length = 0) Then
ErrorProvider1.SetError(TextBox1, "A UserID is required")
bFieldsValid = False
Else
ErrorProvider1.SetError(TextBox1, "")
End If
If (TextBox2.Text.Length = 0) Then
ErrorProvider1.SetError(TextBox2, "A Password is required")
bFieldsValid = False
Else
ErrorProvider1.SetError(TextBox2, "")
End If
' Fire event to container if they are.
If (bFieldsValid = True) Then
RaiseEvent OkClicked(TextBox1.Text, TextBox2.Text)
End If
End Sub
' User clicked Cancel. Fire event to container.
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles Button2.Click
RaiseEvent CancelClicked()
End Sub
' Events that this control fires to its container when the user
' clicks the OK button or the Cancel button.
Public Event OkClicked(ByVal UserID As String, ByVal Password As _
String)
Public Event CancelClicked()
End Class
|