Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 42220

Trying to limit a textbox to a certain number range

$
0
0
Hello,

I am using Visual Basic 2010 Express. I am trying to limit a textbox to a certain range of numbers. I want someone to be able to type in a number from 0-20. I have a code snippet that somewhat works. If someone types 21 or higher, it changes it to 20, If someone types 1, and then puts a - in front of it, it will change it to 0. The problem comes when they delete the number in the box, or put a - with no number (to write -1). I get this error:

A first chance exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll
and
Conversion from string "" to type 'Double' is not valid.

My code is as follows:

Code:

Private Sub TxtMax1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxtMax1.TextChanged
        If CDbl(TxtMax1.Text) < 0 Then
            TxtMax1.Text = "0"
        End If
        If CDbl(TxtMax1.Text) > 20 Then
            TxtMax1.Text = "20"
        End If
    End Sub

I tried it on my own before without the CDbl() part, and I got the same result. I found the above online, and tried it, but I got the same result. I know there is something I am missing, I am just not sure what to add to make it work.

Thanks for any help you can give me.

Viewing all articles
Browse latest Browse all 42220

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>