Click here to Skip to main content
15,881,424 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Let me start off by saying this is a kinda dumb question.

Is there a way you can set the max (246) and min (0) value of a textbox?

I know I could do …
VB
If DataSizeText.Text < 0 Then
    DataSizeText.Text = 0
ElseIf DataSizeText.Text > 246 Then
    IndexText.Text = 246
    DataSizeText.Text = 246 
End if 

What other way could you go about setting max and min value? Like maybe use the properties tool bar ?
Posted
Updated 22-Mar-12 5:39am
v2
Comments
ProEnggSoft 22-Mar-12 11:40am    
Edit: pre tag for VB code and code tags added - PES
Shahin Khorshidnia 22-Mar-12 16:30pm    
Please tag your question. WinForm? ASP.Net? WPF? ...?

No. The Text Property is a string, not an integer, so "max" and "min" don't mean anything to it.

Instead, consider using a NumericUpDown instead.
 
Share this answer
 
You have not tagged your question either with WinForm or ASP.NET

If you want to use in Windows Forms application
Then NumericUpDown control can be used and Minimum and Maximum properties can be set to the values required. i.e. 0, 246 respectively.
An example is given here
http://msdn.microsoft.com/en-us/library/system.windows.forms.numericupdown.minimum.aspx[^]

If you want to use in ASP.NET application
Then TextBox control along with RangeValidator control can be used.
The RangeValidator is explained here
http://www.w3schools.com/aspnet/control_rangevalidator.asp[^]
It can be tested online at
http://www.w3schools.com/aspnet/showasp.asp?filename=demo_rangevalidator2[^]
and
http://www.w3schools.com/aspnet/showasp.asp?filename=demo_rangevalidator[^]
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 22-Mar-12 11:48am    
Nice, a 5.
--SA
ProEnggSoft 22-Mar-12 20:14pm    
Thank you very much.
Sergey Alexandrovich Kryukov 22-Mar-12 21:59pm    
Still, I suggest we should wait for OP's clarification of application type or UI library to be used, demand the clarification first. Answering in two or more variants is very irritating...
--SA
ProEnggSoft 22-Mar-12 22:38pm    
Thank you for suggestion.
Shahin Khorshidnia 22-Mar-12 16:31pm    
+5
I tried this simple code but how to set this for all textboxes in the form?
VB
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
 
Comments
yogesh vaidya 10-Jun-17 0:07am    
BUT HOW TO DETERMINER MINIMUM DIGIT IN TEXT BOX ?
I NEED ONLY 7 DIGIT NOT MORE THAN 7 OR NOT LESS THAN 7

Private Sub txtsbno_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtsbno.KeyPress

If txtsbno.Text.Length > 7 Then
If e.KeyChar > ControlChars.Back Or e.KeyChar < ControlChars.Back Then
e.Handled = True
MessageBox.Show("Not MORE THAN /LESS THAN 7 DIGIT ")
End If


End If
END SUB
BUT IT IS NOT ABLE TO REACT ON MINIMS DIGIT

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