My Control

 

Public Class MyUC

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()

originalColor1 = btnOne.BackColor

originalColor2 = btnTwo.BackColor

'Add any initialization after the InitializeComponent() call

 

End Sub

 

'UserControl1 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 btnOne As System.Windows.Forms.Button

Friend WithEvents btnTwo As System.Windows.Forms.Button

<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

Me.btnOne = New System.Windows.Forms.Button

Me.btnTwo = New System.Windows.Forms.Button

Me.SuspendLayout()

'

'btnOne

'

Me.btnOne.BackColor = System.Drawing.Color.Magenta

Me.btnOne.Location = New System.Drawing.Point(0, 0)

Me.btnOne.Name = "btnOne"

Me.btnOne.Size = New System.Drawing.Size(104, 24)

Me.btnOne.TabIndex = 0

Me.btnOne.Text = "One"

'

'btnTwo

'

Me.btnTwo.BackColor = System.Drawing.Color.Lime

Me.btnTwo.Location = New System.Drawing.Point(0, 24)

Me.btnTwo.Name = "btnTwo"

Me.btnTwo.Size = New System.Drawing.Size(104, 24)

Me.btnTwo.TabIndex = 1

Me.btnTwo.Text = "Two"

'

'MyUC

'

Me.Controls.Add(Me.btnTwo)

Me.Controls.Add(Me.btnOne)

Me.Name = "MyUC"

Me.Size = New System.Drawing.Size(104, 48)

Me.ResumeLayout(False)

 

End Sub

 

#End Region

 

Public Event OneClicked(ByVal value As String)

Public Event TwoClicked(ByVal value As String)

Public Event ColorClicked(ByVal value As Color)

Dim originalColor1 As Color

Dim originalColor2 As Color

 

Private Sub btnOne_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOne.Click

RaiseEvent OneClicked(btnOne.Text)

RaiseEvent ColorClicked(btnOne.BackColor)

End Sub

 

Private Sub btnTwo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTwo.Click

RaiseEvent TwoClicked(btnTwo.Text)

RaiseEvent ColorClicked(btnTwo.BackColor)

End Sub

 

Public ReadOnly Property Button1Color() As String

Get

Return btnOne.BackColor.ToString

End Get

End Property

 

Public ReadOnly Property Button2Color() As String

Get

Return btnTwo.BackColor.ToString

End Get

End Property

 

Public Sub ChangeButton1Color(ByVal colValue As Color)

btnOne.BackColor = colValue

End Sub

 

Public Sub ChangeButton2Color(ByVal colValue As Color)

btnTwo.BackColor = colValue

End Sub

 

Public Sub ResetButtons()

btnOne.BackColor = originalColor1

btnTwo.BackColor = originalColor2

End Sub

End Class

 

Test My Control

 

Public Class frmTestMyControl

Inherits System.Windows.Forms.Form

 

Private Sub MyUC1_ColorClicked(ByVal value As System.Drawing.Color) Handles MyUC1.ColorClicked

Me.BackColor = value

End Sub

 

Private Sub MyUC1_OneClicked(ByVal value As String) Handles MyUC1.OneClicked

MsgBox(value)

End Sub

 

Private Sub MyUC1_TwoClicked(ByVal value As String) Handles MyUC1.TwoClicked

MsgBox(value)

End Sub

 

Private Sub btnChange1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChange1.Click

MyUC1.ChangeButton1Color(Color.Azure)

End Sub

 

Private Sub btnChange2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChange2.Click

MyUC1.ChangeButton2Color(Color.Yellow)

End Sub

 

Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click

MyUC1.ResetButtons()

End Sub

 

Private Sub btnDisp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisp.Click

MsgBox("Button1 is " & MyUC1.Button1Color & " Button 2 is " & _

MyUC1.Button2Color)

End Sub

End Class

 

Control Key Pad

 

Option Explicit On

 

Public Class ctlKeyPad

Inherits System.Windows.Forms.UserControl

 

'--------------- EVENTS -------------------------------------------------------------------------

'

Public Event EnterClicked(ByVal vEntry As String)

Public Event Key(ByVal vLastEntry As String)

Public Event ValueChanged(ByVal vNewValue As String)

 

'--------------- PROPERTIES ---------------------------------------------------------------------

'

Private wrkBackcolor As Color

Private wrkDisplayHidden As Boolean

Private wrkErrorBeepOn As Boolean

Private wrkFont As Font

Private wrkLastEntry As String

Private wrkUseAmbient As Boolean

 

 

'--------------- INTERNAL VARIABLES -------------------------------------------------------------

'

Private DecimalEntered As Boolean

Private MinusEntered As Boolean

Private savBackColor As Object

Private ConstituentCtl As Control

'Private WithEvents KPFont As StdFont

 

'Public Overrides Property BackColor() As Color

' Get

' Return wrkBackcolor

' End Get

' Set(ByVal Value As Color)

' wrkBackcolor = Value

' MyBase.BackColor = wrkBackcolor

' End Set

 

'End Property

 

Public Property DisplayHidden() As Boolean

Get

Return wrkDisplayHidden

End Get

Set(ByVal Value As Boolean)

wrkDisplayHidden = Value

If wrkDisplayHidden Then

txtDisplay.Visible = False

Else

txtDisplay.Visible = True

End If

End Set

End Property

Public Property ErrorBeepON() As Boolean

Get

Return wrkErrorBeepOn

End Get

Set(ByVal Value As Boolean)

wrkErrorBeepOn = Value

End Set

End Property

Public ReadOnly Property LastEntry() As String

Get

Return wrkLastEntry

End Get

End Property

 

Public ReadOnly Property Value() As String

Get

Return txtDisplay.Text

End Get

End Property

 

Public Sub Clear()

txtDisplay.Text = ""

DecimalEntered = False

MinusEntered = False

wrkLastEntry = "PRGCLR"

 

End Sub

Public Sub SetValue(ByRef vValue As String)

txtDisplay.Text = vValue

If InStr(vValue, "-") = 0 Then

MinusEntered = False

Else

MinusEntered = True

End If

If InStr(vValue, ".") = 0 Then

DecimalEntered = False

Else

DecimalEntered = True

End If

wrkLastEntry = "PRGSET"

RaiseEvent ValueChanged(txtDisplay.Text)

End Sub

 

 

Private Sub btnClr_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClr.Click

txtDisplay.Text = ""

DecimalEntered = False

MinusEntered = False

wrkLastEntry = "CLR"

RaiseEvent Key(wrkLastEntry)

End Sub

 

Private Sub btnDecimal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDecimal.Click

If Not DecimalEntered Then

txtDisplay.Text = txtDisplay.Text & "."

wrkLastEntry = "."

DecimalEntered = True

RaiseEvent Key(wrkLastEntry)

Else

If ErrorBeepON Then

Beep()

End If

End If

End Sub

 

 

Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click

Dim DelChr As String

If Len(txtDisplay.Text) <> 0 Then

DelChr = Microsoft.VisualBasic.Right(txtDisplay.Text, 1)

txtDisplay.Text = Microsoft.VisualBasic.Left(txtDisplay.Text, (Len(txtDisplay.Text) - 1))

If DelChr = "." Then

DecimalEntered = False

End If

If DelChr = "-" Then

MinusEntered = False

End If

End If

wrkLastEntry = "DEL" & DelChr

RaiseEvent Key(wrkLastEntry)

End Sub

 

Private Sub btnEnter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnter.Click

RaiseEvent EnterClicked(txtDisplay.Text)

End Sub

 

Private Sub btnMinus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMinus.Click

If MinusEntered Then

txtDisplay.Text = Microsoft.VisualBasic.Right(txtDisplay.Text, (Len(txtDisplay.Text) - 1))

wrkLastEntry = "+"

MinusEntered = False

RaiseEvent Key(wrkLastEntry)

Else

txtDisplay.Text = "-" & txtDisplay.Text

wrkLastEntry = "-"

MinusEntered = True

RaiseEvent Key(wrkLastEntry)

End If

 

End Sub

 

Private Sub NmbrKey_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _

Handles Button0.Click, Button1.Click, Button2.Click, Button3.Click, Button4.Click, _

Button5.Click, Button6.Click, Button7.Click, Button8.Click, Button9.Click

Dim strNmbr As String

Select Case sender.name

Case "Button0"

strNmbr = "0"

Case "Button1"

strNmbr = "1"

Case "Button2"

strNmbr = "2"

Case "Button3"

strNmbr = "3"

Case "Button4"

strNmbr = "4"

Case "Button5"

strNmbr = "5"

Case "Button6"

strNmbr = "6"

Case "Button7"

strNmbr = "7"

Case "Button8"

strNmbr = "8"

Case "Button9"

strNmbr = "9"

End Select

txtDisplay.Text = txtDisplay.Text & strNmbr

wrkLastEntry = strNmbr

RaiseEvent Key(wrkLastEntry)

End Sub

End Class

 

Test key pad

 

Public Class Form1

Inherits System.Windows.Forms.Form

 

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

'CtlKeyPad1.UseAmbient = CtlKeyPad1.UseAmbient Xor True

End Sub

 

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

CtlKeyPad1.DisplayHidden = CtlKeyPad1.DisplayHidden Xor True

End Sub

 

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

CtlKeyPad1.Clear()

End Sub

 

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click

Dim ValueString As String

ValueString = InputBox("Enter a proper numeric string")

CtlKeyPad1.SetValue(ValueString)

End Sub

 

 

 

Private Sub CtlKeyPad1_Key(ByVal vLastEntry As String) Handles CtlKeyPad1.Key

Static LabelConstantsSaved As Boolean

Static Lbl1Const As String

Static Lbl2Const As String

 

If Not LabelConstantsSaved Then

Lbl1Const = Label1.Text & " "

Lbl2Const = Label2.Text & " "

LabelConstantsSaved = True

End If

 

Label1.Text = Lbl1Const & CtlKeyPad1.LastEntry

Label2.Text = Lbl2Const & CtlKeyPad1.Value

 

End Sub

 

Private Sub CtlKeyPad1_EnterClicked(ByVal vEntry As String) Handles CtlKeyPad1.EnterClicked

MsgBox("'" & vEntry & "' was entered")

End Sub

 

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

 

End Sub

 

Private Sub CtlKeyPad1_ValueChanged(ByVal vNewValue As String) Handles CtlKeyPad1.ValueChanged

MsgBox("SetValue method used")

End Sub

End Class