Click here to Skip to main content
12,946,816 members (48,502 online)
Rate this:
Please Sign up or sign in to 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 …
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 22-Mar-12 4:59am
Updated 22-Mar-12 5:39am
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? ...?
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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[^]

If you want to use in ASP.NET application
Then TextBox control along with RangeValidator control can be used.
The RangeValidator is explained here[^]
It can be tested online at[^]
SAKryukov 22-Mar-12 11:48am
Nice, a 5.
Shahin Khorshidnia 22-Mar-12 16:31pm
ProEnggSoft 22-Mar-12 20:12pm
Thank you very much.
ProEnggSoft 22-Mar-12 20:14pm
Thank you very much.
SAKryukov 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...
ProEnggSoft 22-Mar-12 22:38pm
Thank you for suggestion.
losmac 25-Mar-12 7:31am
Very good answer! My 5!
ProEnggSoft 25-Mar-12 7:38am
Thank you.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

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

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

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 5,059
CHill60 3,235
Maciej Los 2,538
Jochen Arndt 1,935
ppolymorphe 1,795

Advertise | Privacy | Mobile
Web02 | 2.8.170518.1 | Last Updated 17 Feb 2014
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100