Multible TB Event Access As One

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
"Please Remember I am old school VB 3.0 Learning 2010 VB"
Feel Free to Comment or call me Oldfart.
Ill still enjoy Coding!
This code still needs Tweaking,
So Copy and Paste at your own Luck!
First I created 6 arrays of Textboxs. Each for the 6 different columns of boxes.
Text1(14), Text2(14) and so on. Im converting from VB 3.0 to VB 2010. Text1-Text6 is through
out the program and saves me a bunch of problems.
"I copied over all code and worked through it" But Events are not in the Arrays.
VB 2010 does not have "Control Arrays" Soooo,
Next I created handles for the different areas I wanted to access "Focus, Click, Lostfocus".
I didnt understand this in the beginning!
All you are doing is adding an extra handle(Sub) to the already implemented Events(Subs).
Only this points to a sub I created. Wal La. A Handle along side the TBs Events.
Only I created it! I like this! Now I can point to any sub(Event) I want simply
by creating a handle for it. Nice!!! 3 Subs are below MakeHandles.
"Notice each TB Addhandler points to the same Sub" This way I can edit all input from one Sub.
============================================================
Private Sub MakeHandles() Call this at the form load level.
This sub is in the Form1 Class.
Bill is the set of BillNameTBs
Dim TextBoxs = (From T In Me.Controls.OfType(Of TextBox)() _
Where T.Name.StartsWith("Bill") Select T).ToList
If TextBoxs.Count > 0 Then
For Each tb In TextBoxs
TB sub(Event) of MouseClick / TBClick Sub Below
AddHandler tb.MouseClick, AddressOf TBClick
TB sub(Event) of GotFocus / TBFocused Sub Below
AddHandler tb.GotFocus, AddressOf TBFocused
TB sub(Event) of LostFocus / TBLostFocus Sub Below
AddHandler tb.LostFocus, AddressOf TBLostFocus
The "AddressOf" Points to my Sub!
Next
End If
TextBoxs.Clear()Clear TBs so not to carry over anything
AmtO is the AmtOwedTBs
TextBoxs = (From T In Me.Controls.OfType(Of TextBox)() _
Where T.Name.StartsWith("AmtO") Select T).ToList
If TextBoxs.Count > 0 Then
For Each tb In TextBoxs
AddHandler tb.MouseClick, AddressOf TBClick
AddHandler tb.GotFocus, AddressOf TBFocused
AddHandler tb.LostFocus, AddressOf TBLostFocus
Next
End If
TextBoxs.Clear()
Due is the DueDateTBs
TextBoxs = (From T In Me.Controls.OfType(Of TextBox)() _
Where T.Name.StartsWith("Due") Select T).ToList
If TextBoxs.Count > 0 Then
For Each tb In TextBoxs
AddHandler tb.MouseClick, AddressOf TBClick
AddHandler tb.GotFocus, AddressOf TBFocused
AddHandler tb.LostFocus, AddressOf TBLostFocus
Next
Me.Text = TextBoxs.Count
End If
TextBoxs.Clear()
AmtP is the AmtPaidTBs
TextBoxs = (From T In Me.Controls.OfType(Of TextBox)() _
Where T.Name.StartsWith("AmtP") Select T).ToList
If TextBoxs.Count > 0 Then
For Each tb In TextBoxs
AddHandler tb.MouseClick, AddressOf TBClick
AddHandler tb.GotFocus, AddressOf TBFocused
AddHandler tb.LostFocus, AddressOf TBLostFocus
Next
Me.Text = TextBoxs.Count
End If
TextBoxs.Clear()
Date is the DatePaidTBs
TextBoxs = (From T In Me.Controls.OfType(Of TextBox)() _
Where T.Name.StartsWith("Date") Select T).ToList
If TextBoxs.Count > 0 Then
For Each tb In TextBoxs
AddHandler tb.MouseClick, AddressOf TBClick
AddHandler tb.GotFocus, AddressOf TBFocused
AddHandler tb.LostFocus, AddressOf TBLostFocus
Next
Me.Text = TextBoxs.Count
End If
TextBoxs.Clear()
CKN is the CKNumberTBs
TextBoxs = (From T In Me.Controls.OfType(Of TextBox)() _
Where T.Name.StartsWith("CKN") Select T).ToList
If TextBoxs.Count > 0 Then
For Each tb In TextBoxs
AddHandler tb.MouseClick, AddressOf TBClick
AddHandler tb.GotFocus, AddressOf TBFocused
AddHandler tb.LostFocus, AddressOf TBLostFocus
Next
Me.Text = TextBoxs.Count
End If
TextBoxs.Clear()
End Sub
==============================================================
These are the subs I created and am pointing to with the handle.
Other subs are below. Notice there are no Handles behind it!!!
I iterate through the 6 columns of 15 TBs to find the one TB I want.
Then call the Sub sending the "sender, e" properties
Notice if it is MouseEventArgs or EventArgs.
Private Sub TBClick(ByVal sender As Object, ByVal e As MouseEventArgs)
Dim TextBoxClicked As TextBox = CType(sender, TextBox)
For I = 0 To 14
If TextBoxClicked.Name = Text1(I).Name Then
TextBox_Click(sender, e)
Exit Sub
ElseIf TextBoxClicked.Name = Text2(I).Name Then
TextBox_Click(sender, e)
Exit Sub
ElseIf TextBoxClicked.Name = Text3(I).Name Then
TextBox_Click(sender, e)
Exit Sub
ElseIf TextBoxClicked.Name = Text4(I).Name Then
TextBox_Click(sender, e)
Exit Sub
ElseIf TextBoxClicked.Name = Text5(I).Name Then
TextBox_Click(sender, e)
Exit Sub
ElseIf TextBoxClicked.Name = Text6(I).Name Then
TextBox_Click(sender, e)
Exit Sub
End If
Next
End Sub
================================================================
Private Sub TBFocused(ByVal sender As Object, ByVal e As EventArgs)
Dim TextBoxFocus As TextBox = CType(sender, TextBox)
For I = 0 To 14
If TextBoxFocus.Name = Text1(I).Name Then
TextBox_GotFocus(sender, e)
Exit Sub
ElseIf TextBoxFocus.Name = Text2(I).Name Then
TextBox_GotFocus(sender, e)
Exit Sub
ElseIf TextBoxFocus.Name = Text3(I).Name Then
TextBox_GotFocus(sender, e)
Exit Sub
ElseIf TextBoxFocus.Name = Text4(I).Name Then
TextBox_GotFocus(sender, e)
Exit Sub
ElseIf TextBoxFocus.Name = Text5(I).Name Then
TextBox_GotFocus(sender, e)
Exit Sub
ElseIf TextBoxFocus.Name = Text6(I).Name Then
TextBox_GotFocus(sender, e)
Exit Sub
End If
Next
End Sub
=================================================================
Private Sub TBLostFocus(ByVal sender As Object, ByVal e As EventArgs)
Dim TextBoxLostFocus As TextBox = CType(sender, TextBox)
For I = 0 To 14
If TextBoxLostFocus.Name = Text1(I).Name Then
TextBox_LostFocus(sender, e)
Exit Sub
ElseIf TextBoxLostFocus.Name = Text2(I).Name Then
TextBox_LostFocus(sender, e)
Exit Sub
ElseIf TextBoxLostFocus.Name = Text3(I).Name Then
TextBox_LostFocus(sender, e)
Exit Sub
ElseIf TextBoxLostFocus.Name = Text4(I).Name Then
TextBox_LostFocus(sender, e)
Exit Sub
ElseIf TextBoxLostFocus.Name = Text5(I).Name Then
TextBox_LostFocus(sender, e)
Exit Sub
ElseIf TextBoxLostFocus.Name = Text6(I).Name Then
TextBox_LostFocus(sender, e)
Exit Sub
End If
Next
End Sub
===================================================================================
Here is one of the subs I point to with the AddHandler. It works great but needs a little tweaking. Or maybe
a different look at. Feel free to comment. Please/Thank You
Private Sub TextBox_LostFocus(ByVal sender As Object, ByVal e As EventArgs)
Dim LostTextBox As TextBox = CType(sender, TextBox)
Dim mainspot As Integer = VScroll1.Value - 1
For I = 0 To 14
BillNameTBs
If LostTextBox.Name = Text1(I).Name Then
dirty = True
GetFields()
SetIcons()
Exit Sub
AmtOwedTBs / AmtPaidTBs
ElseIf LostTextBox.Name = Text2(I).Name Or LostTextBox.Name = Text4(I).Name Then
If Text2(I).Text = "" Then Text2(I).Text = "0"
If Text4(I).Text = "" Then Text4(I).Text = "0"
dirty = True
GetFields()
SetIcons()
Exit Sub
DateDueTBs
ElseIf LostTextBox.Name = Text3(I).Name Then
Dim UserDate As String = Text3(I).Text
Dim UserDate2 As Date
Dim LeapYeer As Boolean = False
Dim UserYear% = DatePart(DateInterval.Year, Now)

If IsDate(UserDate) Then Checks for Leap Year Also
dirty = True
UserDate2 = UserDate
Else
If UserDate = "2/29/" & WorkingYear Then
LeapYeer = True
End If
If UserDate = "" Then
Exit Sub
Else
Dim DgDef, Msg, Response, Title Declare variables.
If LeapYeer Then
Title = "Leap Year Check"
Msg = """" & UserDate & """" & " Does not exist " & _
NL & NL & " This is not a Leap Year" & NL
DgDef = MsgBoxStyle.Exclamation Describe dialog.
MsgBox(Msg, DgDef, Title) Get user response.
Exit Sub
End If
Title = " Date Error! > " & UserDate
Msg = "Either Bad Date Or Date Format Is Wrong"
"Either Bad Date or Date Format is wrong"
Msg = Msg & NL & NL & "Please Use A Date Format ""MM/DD/YYYY"""
DgDef = MsgBoxStyle.OkOnly Describe dialog.
Response = MsgBox(Msg, DgDef, Title) Get user response.
Text3(I).Text = ""
GetFields()
dirty = True
Text3(I).Focus()
Exit Sub
End If
Exit Sub
End If

Dim newyear% = WorkingYear%

If UserYear% <> WorkingYear Then
MSG = " The year " & UserYear% & " Is not the Working Year you are in! "
MSG = MSG & NL & NL & " Do you want to continue?"
Dim Style = MsgBoxStyle.YesNo Or MsgBoxStyle.DefaultButton2 Or
MsgBoxStyle.Critical
Dim response = MsgBox(MSG, Style, " Wrong Year Input?")
If response = MsgBoxResult.Yes Then
newyear% = DatePart(DateInterval.Year, UserDate2)
Else
newyear% = WorkingYear%
End If
Else
Do nothing
End If

Dim oldmonth% = DatePart(DateInterval.Month, UserDate2)
Dim oldday = DatePart(DateInterval.Day, UserDate2)
Dim newdate As Date = oldmonth & "/" & oldday & "/" & newyear
Text3(I).Text = newdate.ToShortDateString
dirty = True
GetFields()
SetIcons()
Exit Sub
DatePaidTBs
ElseIf LostTextBox.Name = Text5(I).Name Then
Dim UserDate As String = Text5(I).Text
Dim UserDate2 As Date
Dim LeapYeer As Boolean = False
If IsDate(UserDate) Then Checks for Leap Year Also
dirty = True
UserDate2 = UserDate
Else
If UserDate = "2/29/" & WorkingYear Then
LeapYeer = True
End If
If UserDate = "" Then
Exit Sub
Else
Dim DgDef, Msg, Response, Title Declare variables.
If LeapYeer Then
Title = "Leap Year Check"
Msg = """" & UserDate & """" & " Does not exist " & _
NL & NL & " This is not a Leap Year" & NL
DgDef = MsgBoxStyle.Exclamation Describe dialog.
MsgBox(Msg, DgDef, Title) Get user response.
Text5(I).Text = ""
Exit Sub
End If
Title = " Date Error! > " & UserDate
Msg = "Either Bad Date Or Date Format Is Wrong"
"Either Bad Date or Date Format is wrong"
Msg = Msg & NL & NL & "Please Use A Date Format ""MM/DD/YYYY"""
DgDef = MsgBoxStyle.OkOnly Describe dialog.
Response = MsgBox(Msg, DgDef, Title) Get user response.
Text5(I).Text = ""
GetFields()
dirty = True
Text5(I).Focus()
Exit Sub
End If
Exit Sub
End If
Dim UserYear% = DatePart(DateInterval.Year, UserDate2)
Dim newyear% = WorkingYear%
If UserYear% <> WorkingYear Then
MSG = " The year " & UserYear% & " Is not the Working Year you are in! "
MSG = MSG & NL & NL & " Do you want to continue?"
Dim Style = MsgBoxStyle.YesNo Or MsgBoxStyle.DefaultButton2 Or
MsgBoxStyle.Critical
Dim response = MsgBox(MSG, Style, " Wrong Year Input?")
If response = MsgBoxResult.Yes Then
newyear% = DatePart(DateInterval.Year, UserDate2)
Else
newyear% = WorkingYear%
End If
Else
Do nothing
End If
Using WorkingYear% in case user has different year running
Need to work with year loaded
Dim oldmonth% = DatePart(DateInterval.Month, UserDate2)
Dim oldday% = DatePart(DateInterval.Day, UserDate2)
Dim newdate As Date = oldmonth% & "/" & oldday% & "/" & newyear%
Text5(I).Text = newdate.ToShortDateString
dirty = True
GetFields() This sub Grabs the uper and lower TBs and Calls the Calculate sub.
SetIcons() This sub Sets the row color and icon to the left of the row.
Exit Sub
CheckNumberTBs
ElseIf LostTextBox.Name = Text6(I).Name Then
dirty = True
GetFields()
SetIcons()
Exit Sub
End If
Next
End Sub
===================================================================
Here is something different. "KeyDown" Event. I needed to do this 6 times due to each TB
Up,Down,Left,Right Key event is different.
After the Heading I put a KeyHandler(Sub) with (ColumnNumber, Sender, e) Sub Below.
I did this for each column so the keys U,D,L,R press will direct focus to the appropiate TB.
Private Sub ChkNum_KeyDown(ByVal sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles _
CKNum1.KeyDown, CKNum2.KeyDown, CKNum3.KeyDown, CKNum4.KeyDown, CKNum5.KeyDown _
, CKNum6.KeyDown, CKNum7.KeyDown, CKNum8.KeyDown, CKNum9.KeyDown, CKNum10.KeyDown _
, CKNum11.KeyDown, CKNum12.KeyDown, CKNum13.KeyDown, CKNum14.KeyDown, CKNum15.KeyDown
The number 6 represents the sixth column. If it were column 2 then it would be
the "Amount Owed" Column.
>>>>> KeyHandler(6, sender, e) Sub Below
Dim TextBoxKeyDown As TextBox = CType(sender, TextBox)
Select Case TextBoxKeyDown.Name
Case CKNum1.Name
Select Case e.KeyCode
Case Keys.Left
DatePaid1.Focus()
Case Keys.Right
BillNameTB1.Focus()
Case Keys.Up
CKNum15.Focus()
Case Keys.Down
CKNum2.Focus()
End Select
Case CKNum2.Name
Select Case e.KeyCode
Case Keys.Left
DatePaid2.Focus()
Case Keys.Right
BillNameTB2.Focus()
Case Keys.Up
CKNum1.Focus()
Case Keys.Down
CKNum3.Focus()
End Select
Case CKNum3.Name
This was shortened for length of txt message for you to view. It
goes on to CKNum15.Name!
All 6 Columns have there own sub for U,D,L,R key press
Im still tweaking so please understand.
End Select
End Sub
End Sub
=============================================================
Here is the Key Handler I created.
Private Sub KeyHandler(ByVal ColumnNumber As Integer, _
ByVal sender As Object, e As System.Windows.Forms.KeyEventArgs)
Dim TextBoxName As TextBox = CType(sender, TextBox)
Dim UserKey = e.KeyCode
Dim MSG As String = ""
Try
Select Case ColumnNumber
Case 1, 6 Bill Name Column of TBs and CheckNumber Column of ckn boxs.
Select Case e.KeyCode
Case Keys.Return
e.SuppressKeyPress = True
Case Else
End Select
Case 2, 4 Amount Owed/Amount Paid Columns of TBs
Select Case e.KeyCode
Case Keys.Return
e.SuppressKeyPress = True
If TextBoxName.Text > "" Then
If Strings.Left(TextBoxName.Text, 1) = "$" Then
Exit Sub
Else
TextBoxName.Text = "$" & TextBoxName.Text
TextBoxName.SelectionStart = 35
Exit Sub
End If
Else
TextBoxName.Text = "$0.00"
TextBoxName.SelectionStart = 5
Exit Sub
End If
Case Keys.Delete
Exit Sub
Case Keys.OemPeriod
If TextBoxName.TextLength = 0 Then
TextBoxName.Text = "0."
e.SuppressKeyPress = True
TextBoxName.SelectionStart = 2
Exit Sub
End If
Case Keys.Left, Keys.Right, Keys.Up, Keys.Down
If TextBoxName.Text > "" Then
If Strings.Left(TextBoxName.Text, 1) = "$" Then
Else
TextBoxName.Text = "$" & TextBoxName.Text
End If
Else
TextBoxName.Text = "$0.00"
End If
Exit Sub
End Select
This calls a sub to check if numeric or not. I added the "." to it
Need to check for more then 1 "." though!!!!!
So right now two/three "." are posible. Still Tweaking :)
Dim NonNumericFlag As Boolean = CheckUserKeyDown(sender, e)
If NonNumericFlag = True Then
MSG = "Character Needs To Be Of The Numeric Value! "
MSG = MSG & NL & NL & " In Currentcy Format, Exp. ""000.00"""
MSG = MSG & NL & NL & "___________________________________"
MSG = MSG & NL & NL & "Note:"
MSG = MSG & NL & " The Dollar Sign Will Be Added Automatically "
MSG = MSG & NL
MsgBox(MSG, MsgBoxStyle.Exclamation, "Non Numeric Character")
TextBoxName.Text = ""
End If
Case 3, 5 Date Columns
Still need to work here
End Select
Any and all comments are welcome. Thank You for looking
End Sub

View the full article
 
Back
Top