textbox, string to interger & varible question

Johnson

Well-known member
Joined
Mar 6, 2009
Messages
158
Programming Experience
Beginner
This seems so simple but all google comes up with is 'numbers only for textbox'

Basically 1 textbox. On key press i want to check the user has not entered a number greater then 2500

VB.NET:
            Dim txtWidth = Val(txtWidth.Text)
            If txtWidth > 2500 Then
            'handle what ever code here
                txtWidth.Text = ""
                Exit Sub
            End If


but for some reason txtWidth is always 1 number less then my textbox

example,

1 = txtWidth 0

then i enter a 2 into the textbox

txtWidth = 1

then i enter a 3 into the textbox

txtwidth = 12

and so on. make sense?
 
make sense?

Not really, but if you used a NumericUpDown control, it would make your life so much easier, as you can set the Maximum to 2500.

I would also suggest turning Option Strict on.
 
To much coding has gone into it for the control to be changed.

here is the full code

VB.NET:
        Private Sub txtImageWidth_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtImageWidth.KeyPress
            'Check user entered a number only
            If Asc(e.KeyChar) <> 13 AndAlso Asc(e.KeyChar) <> 8 _
           AndAlso Not IsNumeric(e.KeyChar) Then
                Dim Message As String = "Numeric chr only"
                Dim Caption As String = "Invalid entry"

                Call BalloonHelp.Showx(txtImageWidth, Message, Caption, BalloonHelp.BalloonIcon.sError)
                e.Handled = True
                Exit Sub
            End If


            Dim n = Val(txtImageWidth.Text)
            If n > 2500 Then

                Dim Message As String = "We do not support images over 2500px"
                Dim Caption As String = "Invalid entry"
                Call BalloonHelp.Showx(txtImageWidth, Message, Caption, BalloonHelp.BalloonIcon.sError)
                txtImageWidth.Text = ""
                Exit Sub
            End If

        End Sub

while typing if the value becomes greater then 2500 it will msgbox an error
 
If that's all the code that relates to the textbox, change it. It will be quicker than having to write the code in case someone pastes in "9999", which wont be handled by your KeyPress event.
 
No that is not all the code. Thats the code for the spacific part. sorry but i'm not changing the textbox

1: numeric up down control looks horrible and would ruin my UI, which means i would have to change other controls
2: ignoring a problem and not facing it means i will never learn.

sorry :)
 
Managed to sort it. I decided on using a slight delay in a timer. since the key press was being passed before the value got to the textbox.

500delay works, might not be best
VB.NET:
        Private Sub tmrCheckcalc_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrCheckcalc.Tick
            Dim countTextboxchars = Val(txtImageWidth.TextLength)
            If CStr(countTextboxchars) = "4" Then
                Dim doThemath = Val(txtImageWidth.Text)
                If doThemath > 2500 Then
                    txtImageWidth.Text = ""
                    Dim Message As String = "We do not support pictures over 2500px"
                    Dim Caption As String = "Error"

                    Call BalloonHelp.Showx(txtImageWidth, Message, Caption, BalloonHelp.BalloonIcon.sError)
                    tmrCheckcalc.Enabled = False
                Else
                    'do nothing
                    tmrCheckcalc.Enabled = False
                End If
            End If
        End Sub
 
I am with ya, I don't like the NumericUpDown control much either. I use the textchanged and the keypress events to help protect my textboxes. If someone pastes "9999" into your textbox the textchanged event will catch it.
 
I'm confused as to why so many people dont like the NumericUpDown control :confused: Anyway, you can always get round the UI problems by building a custom textbox control with minimum and maximum properties, which changes colour when outside the acceptable values :)

Anyway, Johnson, did you look at the link in my post above? You should be using Integer.TryParse instead of Val, just in case any non-numeric characters ever get in there.
 
Back
Top