Apply formatting on multiple Text boxes within a Group box.
-
Hello, I have a request for help with how to format a text box with numbers with 3 digits with two decimal places. but will not advance until a decimal point is placed, this works with one box. but i would like to implement this on 14 text boxes in a group box. so i don't have to duplicate the code 14 times.
Private Sub RollTemp_BOTTextBox_KeyPress(sender As Object, e As KeyPressEventArgs) Handles RollTemp_BOTTextBox.KeyPress
Dim keyChar = e.KeyChar
If Char.IsControl(keyChar) Then
'Allow all control characters.
ElseIf Char.IsDigit(keyChar) OrElse keyChar = "."c Then
Dim text = Me.RollTemp_BOTTextBox.Text
Dim selectionStart = Me.RollTemp_BOTTextBox.SelectionStart
Dim selectionLength = Me.RollTemp_BOTTextBox.SelectionLength
text = text.Substring(0, selectionStart) & keyChar & text.Substring(selectionStart + selectionLength)
If Integer.TryParse(text, New Integer) AndAlso text.Length > 3 Then
'Reject an integer that is longer than 16 digits.
e.Handled = True
ElseIf Double.TryParse(text, New Double) AndAlso text.IndexOf("."c) < text.Length - 4 Then
'Reject a real number with two many decimal places.
e.Handled = True
End If
Else
'Reject all other characters.
e.Handled = True
End If
End SubThe Text Box will have like 123.67 in it. But if you enter 123 it doesn't let you put another number in and advance until you put the (.) in and then allows you to put another number in. Thank you
-
Hello, I have a request for help with how to format a text box with numbers with 3 digits with two decimal places. but will not advance until a decimal point is placed, this works with one box. but i would like to implement this on 14 text boxes in a group box. so i don't have to duplicate the code 14 times.
Private Sub RollTemp_BOTTextBox_KeyPress(sender As Object, e As KeyPressEventArgs) Handles RollTemp_BOTTextBox.KeyPress
Dim keyChar = e.KeyChar
If Char.IsControl(keyChar) Then
'Allow all control characters.
ElseIf Char.IsDigit(keyChar) OrElse keyChar = "."c Then
Dim text = Me.RollTemp_BOTTextBox.Text
Dim selectionStart = Me.RollTemp_BOTTextBox.SelectionStart
Dim selectionLength = Me.RollTemp_BOTTextBox.SelectionLength
text = text.Substring(0, selectionStart) & keyChar & text.Substring(selectionStart + selectionLength)
If Integer.TryParse(text, New Integer) AndAlso text.Length > 3 Then
'Reject an integer that is longer than 16 digits.
e.Handled = True
ElseIf Double.TryParse(text, New Double) AndAlso text.IndexOf("."c) < text.Length - 4 Then
'Reject a real number with two many decimal places.
e.Handled = True
End If
Else
'Reject all other characters.
e.Handled = True
End If
End SubThe Text Box will have like 123.67 in it. But if you enter 123 it doesn't let you put another number in and advance until you put the (.) in and then allows you to put another number in. Thank you
You can always reuse the same event handler for the different textboxes. What I would do is rename this event handler so it doesn't refer to the control by name and then point each of the relevant controls KeyPress events at this event handler. As the
sender
refers to the textbox that raises the event, you can refactor the method to avoid using Me.RollTemp_BOTTextBox by casting sender to a TextBox type. -
You can always reuse the same event handler for the different textboxes. What I would do is rename this event handler so it doesn't refer to the control by name and then point each of the relevant controls KeyPress events at this event handler. As the
sender
refers to the textbox that raises the event, you can refactor the method to avoid using Me.RollTemp_BOTTextBox by casting sender to a TextBox type.Would you give an example please, i am not following your explanation. Would be grateful.
-
Would you give an example please, i am not following your explanation. Would be grateful.
Start by creating a collection / list that contains your "14 text boxes". How you create your list "depends" on what you're doing now. That's enough of a task for the time being.
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food
-
Hello, I have a request for help with how to format a text box with numbers with 3 digits with two decimal places. but will not advance until a decimal point is placed, this works with one box. but i would like to implement this on 14 text boxes in a group box. so i don't have to duplicate the code 14 times.
Private Sub RollTemp_BOTTextBox_KeyPress(sender As Object, e As KeyPressEventArgs) Handles RollTemp_BOTTextBox.KeyPress
Dim keyChar = e.KeyChar
If Char.IsControl(keyChar) Then
'Allow all control characters.
ElseIf Char.IsDigit(keyChar) OrElse keyChar = "."c Then
Dim text = Me.RollTemp_BOTTextBox.Text
Dim selectionStart = Me.RollTemp_BOTTextBox.SelectionStart
Dim selectionLength = Me.RollTemp_BOTTextBox.SelectionLength
text = text.Substring(0, selectionStart) & keyChar & text.Substring(selectionStart + selectionLength)
If Integer.TryParse(text, New Integer) AndAlso text.Length > 3 Then
'Reject an integer that is longer than 16 digits.
e.Handled = True
ElseIf Double.TryParse(text, New Double) AndAlso text.IndexOf("."c) < text.Length - 4 Then
'Reject a real number with two many decimal places.
e.Handled = True
End If
Else
'Reject all other characters.
e.Handled = True
End If
End SubThe Text Box will have like 123.67 in it. But if you enter 123 it doesn't let you put another number in and advance until you put the (.) in and then allows you to put another number in. Thank you
As Pete suggested:
Private Sub NumericTextBox_KeyPress(sender As Object, e As KeyPressEventArgs) Handles RollTemp_BOTTextBox.KeyPress, OtherTextBox.KeyPress, YetAnotherTextBox.KeyPress
Dim keyChar = e.KeyChar
If Char.IsControl(keyChar) Then
'Allow all control characters.
ElseIf Char.IsDigit(keyChar) OrElse keyChar = "."c Then
Dim box As TextBox = DirectCast(sender, TextBox)
Dim text As String = box.Text
Dim selectionStart As Integer = box.SelectionStart
Dim selectionLength As Integer = box.SelectionLength
text = text.Substring(0, selectionStart) & keyChar & text.Substring(selectionStart + selectionLength)Dim valueInt32 As Integer Dim valueDouble As Double If Integer.TryParse(text, valueInt32) AndAlso valueInt32 > 999 Then ' Reject an integer that is longer than 3 digits. e.Handled = True ElseIf Double.TryParse(text, valueDouble) AndAlso text.IndexOf("."c) < text.Length - 4 Then ' Reject a real number with two many decimal places. e.Handled = True End If Else ' Reject all other characters. e.Handled = True End If
End Sub
How to: Connect Multiple Events to a Single Event Handler - Windows Forms .NET Framework | Microsoft Docs[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
As Pete suggested:
Private Sub NumericTextBox_KeyPress(sender As Object, e As KeyPressEventArgs) Handles RollTemp_BOTTextBox.KeyPress, OtherTextBox.KeyPress, YetAnotherTextBox.KeyPress
Dim keyChar = e.KeyChar
If Char.IsControl(keyChar) Then
'Allow all control characters.
ElseIf Char.IsDigit(keyChar) OrElse keyChar = "."c Then
Dim box As TextBox = DirectCast(sender, TextBox)
Dim text As String = box.Text
Dim selectionStart As Integer = box.SelectionStart
Dim selectionLength As Integer = box.SelectionLength
text = text.Substring(0, selectionStart) & keyChar & text.Substring(selectionStart + selectionLength)Dim valueInt32 As Integer Dim valueDouble As Double If Integer.TryParse(text, valueInt32) AndAlso valueInt32 > 999 Then ' Reject an integer that is longer than 3 digits. e.Handled = True ElseIf Double.TryParse(text, valueDouble) AndAlso text.IndexOf("."c) < text.Length - 4 Then ' Reject a real number with two many decimal places. e.Handled = True End If Else ' Reject all other characters. e.Handled = True End If
End Sub
How to: Connect Multiple Events to a Single Event Handler - Windows Forms .NET Framework | Microsoft Docs[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Thank you Pete, i see now what you mean. So easy when you have the expertise. :)
-
Thank you Pete, i see now what you mean. So easy when you have the expertise. :)
this could also be your Solution - integrate the functionality into your own customized Textbox :
Public Class NumericTextbox
Inherits TextBoxPrivate Sub NumericTextBox\_KeyPress(sender As Object, e As KeyPressEventArgs) Handles Me.KeyPress Dim keyChar = e.KeyChar If Char.IsControl(keyChar) Then 'Allow all control characters. ElseIf Char.IsDigit(keyChar) OrElse keyChar = "."c Then Dim box As TextBox = DirectCast(sender, TextBox) Dim text As String = box.Text Dim selectionStart As Integer = box.SelectionStart Dim selectionLength As Integer = box.SelectionLength text = text.Substring(0, selectionStart) & keyChar & text.Substring(selectionStart + selectionLength) Dim valueInt32 As Integer Dim valueDouble As Double If Integer.TryParse(text, valueInt32) AndAlso valueInt32 > 999 Then ' Reject an integer that is longer than 3 digits. e.Handled = True ElseIf Double.TryParse(text, valueDouble) AndAlso text.IndexOf("."c) < text.Length - 4 Then ' Reject a real number with two many decimal places. e.Handled = True End If Else ' Reject all other characters. e.Handled = True End If End Sub
End Class