Results 1 to 9 of 9

Thread: [RESOLVED] VS 2017 is there another way to do this?

  1. #1

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,204

    Resolved [RESOLVED] VS 2017 is there another way to do this?

    Code:
        Private Sub TxtPrice_Validated(sender As Object, e As EventArgs) Handles TxtPrice.Validated
    
            If Not IsNumeric(TxtPrice.Text) Then
                TxtPrice.Text = 0
                Exit Sub
            End If
        End Sub
    if the user enters something in the textbox like this
    20...
    ....
    and so on....
    tnx in advanced
    salsa

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: VS 2017 is there another way to do this?

    Well, the best answer is probably to not use a textbox for numeric entry. If you use the NumericUpDown control (NUD), the .Value property is a Decimal. The .Text property of the NUD is still a string, so it's best to avoid that. With the NUD, you can set the maximum, minimum, and the number of decimal places. So, if you want integers, you leave the number of decimal places at 0 (the default), and use CInt to convert the .Value property to an Integer. It can't be anything else, so you can avoid the costlier conversions like IsNumeric and TryParse.

    If you really want to validate, then you should use the Validating event rather than Validated. That gives you a chance to scold the user and leave them back at that control. Validated happens a little too late.

    Also, you would use one of the TryParse methods, such as Integer.TryParse, Double.TryParse, or Decimal.TryParse, depending on what type of value you wanted.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,204

    Re: VS 2017 is there another way to do this?

    hey shaggy
    the user can enter numbers and dights only.
    but he can enter something like this
    20..... or 29...6666
    which is not good
    can you show me an example please with try parse?

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: VS 2017 is there another way to do this?

    Code:
    Dim n As Integer
    If Integer.TryParse (TextBox1.Text, n) Then
      'It is an integer, and it is in n.
    Else
     'It is not an integer, so scold the user and send them back to correct their mistake
    End if
    However, using the NUD would still be easier, because you would set the minimum to 0, the maximum to whatever the largest number would be (the largest number that an integer can hold, if you want), and there would be no real validation. You'd just do this to get the integer:
    Code:
    Dim n As Integer = CInt(NumericUpDown1.Value)
    The size of the value in either case doesn't matter, as long as it is small enough to fit into an integer (roughly -2 billion to +2 billion). If the user tries to enter a value outside of that range, Integer.TryParses will return False. If the user tries to enter text, then Integer.TryParse will return False. Also, if the user doesn't enter anything at all into the textbox, Integer.TryParse will return False, because "" is not seen as 0, it's seen as not a number.
    My usual boring signature: Nothing

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: VS 2017 is there another way to do this?

    You shouldn't be handling Validated unless you're handling Validating. Validated is raised if and only if Validating passes. If you want to do the same thing every time without using Validating, i.e. without letting the control lose focus if the content isn't valid, then you should be handling Leave.

    As for using TryParse, what do you not understand about the examples you found when you made the effort to look for yourself?

  6. #6
    Hyperactive Member
    Join Date
    Jun 2018
    Posts
    434

    Re: VS 2017 is there another way to do this?

    Here is an example from the old days. It checks for special chars, max mins, gives msg, etc. Of course there are many ways and purposes depending on exactly...

    The example makes the controls just cut and paste to an empty form. Change the form name as required.

    Name:  a.gif
Views: 93
Size:  95.0 KB

    Code:
    Public Class Form1
        Private WithEvents InputTb As New TextBox With {.Parent = Me, .Location = New Point(50, 100), .Text = "",
                    .Font = New Font("tahoma", 14)}
        Private WithEvents GoBtn As New Button With {.Parent = Me, .Location = New Point(170, 100), .Text = "Go"}
        Private AnswerLbl As New Label With {.Parent = Me, .Location = New Point(70, 150), .AutoSize = True,
                    .Text = "Enter a Value," & vbLf & "then click Go.", .Font = New Font("tahoma", 14)}
    
        Private Sub GoBtn_Click(sender As Object, e As EventArgs) Handles GoBtn.Click
            Dim value As Double = 0
            Dim textString As String = InputTb.Text.Trim
            Dim result As Boolean = Double.TryParse(textString, value)
    
            AnswerLbl.ForeColor = Color.Red
            If result = True Or textString = "" Or textString = "-" Then
                If value > -1000000 And value < 1000000 Then
                    AnswerLbl.ForeColor = Color.Green
                    AnswerLbl.Text = value.ToString("f1") & " Feet"
                Else
                    AnswerLbl.ForeColor = Color.Red
                    AnswerLbl.Text = "Enter Values between" & vbLf & "-999,999 and 999,999"
                End If
            Else
                AnswerLbl.Text = "Invalid Entry: " & textString
            End If
    
        End Sub
    
        Private Sub Form5_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Text = "Input Number Example"
    
        End Sub
    End Class

  7. #7
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,046

    Re: VS 2017 is there another way to do this?

    you can change this to Integer
    Code:
     Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            TextBox1.Text = "1850,26"
            TextBox2.Text = "3"
    
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Dim NetPrice As Decimal
            Dim Divby As Decimal
    
            'in Textbox1 = 1850,26
            'in Textbox2 divide by = 3
    
            If Decimal.TryParse(Me.TextBox1.Text, NumberStyles.Number, _
                                CultureInfo.CurrentCulture, NetPrice) AndAlso _
              Decimal.TryParse(Me.TextBox2.Text, NumberStyles.Number, _
                               CultureInfo.CurrentCulture, Divby) Then
    
                Dim Div As Decimal = Math.Round(NetPrice / Divby, 2, MidpointRounding.AwayFromZero)
    
                Me.TextBox3.Text = Div.ToString("N2")
    
            Else
                MsgBox("not a valid number ")
    
            End If
        End Sub
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  8. #8
    Hyperactive Member
    Join Date
    Jun 2018
    Posts
    434

    Re: VS 2017 is there another way to do this?

    Quote Originally Posted by ChrisE View Post
    you can change this to Integer
    Code:
     Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            TextBox1.Text = "1850,26"
            TextBox2.Text = "3"
    
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Dim NetPrice As Decimal
            Dim Divby As Decimal
    
            'in Textbox1 = 1850,26
            'in Textbox2 divide by = 3
    
            If Decimal.TryParse(Me.TextBox1.Text, NumberStyles.Number, _
                                CultureInfo.CurrentCulture, NetPrice) AndAlso _
              Decimal.TryParse(Me.TextBox2.Text, NumberStyles.Number, _
                               CultureInfo.CurrentCulture, Divby) Then
    
                Dim Div As Decimal = Math.Round(NetPrice / Divby, 2, MidpointRounding.AwayFromZero)
    
                Me.TextBox3.Text = Div.ToString("N2")
    
            Else
                MsgBox("not a valid number ")
    
            End If
        End Sub

    Notice the "," comma in the string ChrisE Shows TextBox1.Text = "1850,26"

    That is something one needs to deal with sometimes languages use comma and dot differently.

    So Val and older functions may handle number formats wrong sometimes depending on windows langauge/number settings.

  9. #9

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,204

    Re: VS 2017 is there another way to do this?

    tnk you all very much

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width