|
-
Aug 23rd, 2011, 05:25 PM
#1
Thread Starter
Frenzied Member
Handle Leave Event for Multiple TextBoxes
Is there another way to handle the leave event of multiple textboxes other then coding in each event handler? I may not be using correct terms
instead of doing this:
exisitng Code:
Private Sub leveladjstvaltxt_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles leveladjstvaltxt.Leave
If Me.leveladjstvaltxt.Text <> "" Then
Dim levels As Integer = CInt(Me.leveladjstvaltxt.Text)
Me.leveladjstvaltxt.Text = Format(levels, "#,###")
I have 30 TextBoxes all need same formatting , so I was trying to come up with something like this:
trying Code:
For Each ctrl As Control In Me.Controls
If TypeOf ctrl Is TextBox And ctrl.Text <> "" Then
Dim val As Double = CDbl(ctrl.Text)
CType(ctrl, TextBox).Text = Format(val, "#,###")
End If
I am stuck though and dont know where i would place that code if this is something that is even possible. Only thing I can currently think of is in the leave event which defeats the purpose, I wanted to avoid having to place the code in 30 differrent textoxes leave event
Thanks for any input
-
Aug 23rd, 2011, 07:02 PM
#2
Re: Handle Leave Event for Multiple TextBoxes
You can have an event handler method handle multiple events. If you do this, you will need to work out which textbox the event is from:
vbnet Code:
Private Sub TextBox_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.Leave, TextBox2.Leave, TextBox3.Leave, TextBox4.Leave Dim senderTextBox As TextBox = TryCast(sender, TextBox) If senderTextBox IsNot Nothing Then If senderTextBox.Text <> "" Then Dim levels As Integer = CInt(senderTextBox.Text) senderTextBox.Text = Format(levels, "#,###") End If End If End Sub
I would recommend better handling when the text in the textbox cannot be converted to an integer. I got something weird in my demo code - when you put text in the text box, you can move focus to another control, but you don't get the visual selection cues (highlight around the new textbox, cursor, etc) - you can still type and it affects the textbox, but it's not properly focused and you don't get a Leave event for that textbox.
-
Aug 23rd, 2011, 07:04 PM
#3
Thread Starter
Frenzied Member
Re: Handle Leave Event for Multiple TextBoxes
 Originally Posted by Evil_Giraffe
You can have an event handler method handle multiple events. If you do this, you will need to work out which textbox the event is from:
vbnet Code:
Private Sub TextBox_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.Leave,
TextBox2.Leave,
TextBox3.Leave,
TextBox4.Leave
Dim senderTextBox As TextBox = TryCast(sender, TextBox)
If senderTextBox IsNot Nothing Then
If senderTextBox.Text <> "" Then
Dim levels As Integer = CInt(senderTextBox.Text)
senderTextBox.Text = Format(levels, "#,###")
End If
End If
End Sub
I would recommend better handling when the text in the textbox cannot be converted to an integer. I got something weird in my demo code - when you put text in the text box, you can move focus to another control, but you don't get the visual selection cues (highlight around the new textbox, cursor, etc) - you can still type and it affects the textbox, but it's not properly focused and you don't get a Leave event for that textbox.
Thanks I was going to code the keypress so only numbers are allowed
-
Aug 23rd, 2011, 08:13 PM
#4
Re: Handle Leave Event for Multiple TextBoxes
I create a class called formutils that handles things simular to what you want to do. Heres some of the code,
Code:
Public Class clsFormUtils
Dim blnFirstClick As Boolean = True
Dim callingForm As Form
Public Sub SetEnterAsTab(ByVal frm As Form)
callingForm = frm
Dim ctl As Control
For Each ctl In frm.Controls
If ctl.GetType.IsAssignableFrom(GetType(TextBox)) Or ctl.GetType.IsAssignableFrom(GetType(ComboBox)) Then
AddHandler ctl.KeyDown, AddressOf HandleEnterAsTab
End If
Next
End Sub
Private Sub HandleEnterAsTab(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyEventArgs)
If e.KeyCode = Keys.Enter Then
Dim CurrentControl As Control = CType(sender, Control)
callingForm.SelectNextControl(CurrentControl, True, True, True, True)
e.SuppressKeyPress = True
End If
End Sub
Public Sub HightLightText(ByVal frm As Form)
Dim ctl As Control
For Each ctl In frm.Controls
If ctl.GetType.IsAssignableFrom(GetType(TextBox)) Then
AddHandler ctl.Click, AddressOf TextBox_Click
AddHandler ctl.Leave, AddressOf TextBox_Leave
End If
Next
End Sub
Private Sub TextBox_Click(ByVal sender As Object, ByVal e As System.EventArgs)
If blnFirstClick Then
Dim tb As TextBox = DirectCast(sender, TextBox)
tb.SelectAll()
End If
blnFirstClick = False
End Sub
Private Sub TextBox_Leave(ByVal sender As Object, ByVal e As System.EventArgs)
blnFirstClick = True
End Sub
End Class
Then in the form load event,
Code:
'Setup controls to use ENTER to move to next control like TAB
'Setup Textboxes to be highlighted when entered
Dim frmUtils As New clsFormUtils
frmUtils.SetEnterAsTab(Me)
frmUtils.HightLightText(Me)
But this this will effect all TextBoxes on the form.
If that won't work then just create a function that formats the textbox and call it, something like this,
Code:
Private Sub SHARE3TextBox_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles SHARE3TextBox.Validating
e.Cancel = ValidateNumeric(Me.SHARE3TextBox)
End Sub
-
Aug 24th, 2011, 01:21 AM
#5
Re: Handle Leave Event for Multiple TextBoxes
 Originally Posted by billboy
Thanks I was going to code the keypress so only numbers are allowed
the text will still be a string
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Aug 24th, 2011, 01:58 AM
#6
Re: Handle Leave Event for Multiple TextBoxes
You may like to follow the CodeBank link in my signature and check out my Numeric Text Box. If you use that control instead of a standard TextBox then you won't need any code. Validation and formatting are both built in.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|