Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
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
Edited 22-Mar-12 5:39am
v2
Comments
ProEnggSoft at 22-Mar-12 11:40am
   
Edit: pre tag for VB code and code tags added - PES
Shahin Khorshidnia at 22-Mar-12 16:30pm
   
Please tag your question. WinForm? ASP.Net? WPF? ...?
Rate this: bad
good
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.
  Permalink  
Rate this: bad
good
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
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[^]
  Permalink  
v3
Comments
SAKryukov at 22-Mar-12 11:48am
   
Nice, a 5.
--SA
ProEnggSoft at 22-Mar-12 20:14pm
   
Thank you very much.
SAKryukov at 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 at 22-Mar-12 22:38pm
   
Thank you for suggestion.
Shahin Khorshidnia at 22-Mar-12 16:31pm
   
+5
ProEnggSoft at 22-Mar-12 20:12pm
   
Thank you very much.
losmac at 25-Mar-12 7:31am
   
Very good answer! My 5!
ProEnggSoft at 25-Mar-12 7:38am
   
Thank you.
Rate this: bad
good
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
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 354
1 Richard MacCutchan 310
2 OriginalGriff 275
3 Vinay Mistry 163
4 Jared Sanow 145
0 Sergey Alexandrovich Kryukov 5,939
1 OriginalGriff 4,885
2 CPallini 2,473
3 Abhinav S 1,507
4 Richard MacCutchan 1,437


Advertise | Privacy | Mobile
Web03 | 2.8.140814.1 | Last Updated 17 Feb 2014
Copyright © CodeProject, 1999-2014
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