Click here to Skip to main content
16,016,501 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how do i set text box to display numbers only and the maximum value is 11
Posted
Comments
Pete O'Hanlon 17-Feb-14 9:48am    
ASP.NET? WPF? Silverlight? Windows Forms?
Sissy Ram 17-Feb-14 9:52am    
vb .net, windows forms
Sissy Ram 17-Feb-14 10:03am    
Help me please
Sissy Ram 17-Feb-14 10:10am    
I tried this simple code but how to set this for all textboxes in the form?
Private Sub textBox1_KeyPress(sender As Object, e As KeyPressEventArgs)
If e.KeyChar <> ControlChars.Back AndAlso e.KeyChar <> "-"C Then
'allow backspace for deleting and minus simbol
e.Handled = Not Char.IsNumber(e.KeyChar)
'allow numbers only
If Not e.Handled Then
Dim num As Integer = Integer.Parse(String.Format("{0}{1}", If(textBox1.Text = String.Empty, "", textBox1.Text), e.KeyChar.ToString()))
If num > 11 Then
e.Handled = True
End If
End If
End If
End Sub

Use one subroutine to handle the Keypress event for several textboxes.

VB.NET
Private Sub SeveralTextboxes_KeyPress(sender As Object, e As KeyPressEventArgs) _
    Handles textbox1.KeyPress,textbox2,KeyPress,textbox3.KeyPress, textbox4.KeyPress
    If e.KeyChar <> ControlChars.Back AndAlso e.KeyChar <> "-"C Then
        'allow backspace for deleting and minus simbol
        e.Handled = Not Char.IsNumber(e.KeyChar)
        'allow numbers only
        If Not e.Handled Then
            Dim num As Integer = _
                    Integer.Parse(String.Format("{0}{1}", _
                    If(DirectCast(sender,TextBox).Text = String.Empty, _
                    "", DirectCast(sender,TextBox).Text), e.KeyChar.ToString()))
            If  num > 11 Then
                e.Handled = True
            End If
        End If
    End If
End Sub
 
Share this answer
 
Comments
Sissy Ram 24-Feb-14 7:50am    
That works fine
Hi,

You can use the "Tag" property for text box and use the for each loop

like this i am used this for clear all the control on the for which the textbox tag property has "*"

VB
Public Shared Sub ClearEntryControls(ByVal pnl As Panel)
       Dim cnt As Control
       For Each cnt In pnl.Controls
           If (TypeOf cnt Is TextBox Or TypeOf cnt Is Label) And cnt.Tag = "*" Then
               cnt.Text = ""
           ElseIf TypeOf cnt Is NumericUpDown And cnt.Tag = "*" Then
               cnt.Text = 0
           End If
       Next
   End Sub



I hope this will help for you...
 
Share this answer
 
You can look at these 2 wonderful methods,
int.TryParse(...)[^] and TextBox.MaxLength[^].

-KR
 
Share this answer
 
I tried this simple code but how to set this for all textboxes in the form
VB.NET
Private Sub textBox1_KeyPress(sender As Object, e As KeyPressEventArgs)
    If e.KeyChar <> ControlChars.Back AndAlso e.KeyChar <> "-"C Then
        'allow backspace for deleting and minus simbol
        e.Handled = Not Char.IsNumber(e.KeyChar)
        'allow numbers only
        If Not e.Handled Then
            Dim num As Integer = Integer.Parse(String.Format("{0}{1}", If(textBox1.Text = String.Empty, "", textBox1.Text), e.KeyChar.ToString()))
            If  num > 11 Then
                e.Handled = True
            End If
        End If
    End If
End Sub
 
Share this answer
 
v2
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900