0

Being new to VB I am trying to wrap my thoughts around validating a user input (in textbox) range of 1 to 24. I know this is probably a simple expression but my mind is on a Java expression rather than Visual Basic.

Private Sub HoursAppUsed_TextChanged(sender As Object, e As TextChangedEventArgs) Handles HoursAppUsed.TextChanged
    'must check if string is numeric/integer or not'

    Dim hoursEntered As String = HoursAppUsed.Text
    'And hoursEntered > 0 Or hoursEntered < 25  ???? '

    If IsNumeric(hoursEntered) Then
        Dim decFromString1 As Decimal = Decimal.Parse(hoursEntered)
        hoursEntered = "Value: " + hoursEntered
        LabelFour.Content = hoursEntered
    Else
        LabelFour.Content = "Value is not Numeric!"
    End If
    'hoursEntered = "Hours Entered: " + hoursEntered'
    'LabelFour.Content = hoursEntered'

End Sub
MatSnow
  • 7,357
  • 3
  • 19
  • 31
Javacodeman113
  • 407
  • 1
  • 4
  • 11

1 Answers1

1

This could be easily achieved with a NumericUpDown-control by setting the Minimum and Maximum properties.

If you still want to use the TextBox instead, following should work:

Dim hoursEntered As String
Dim decFromString1 As Decimal

If Decimal.TryParse(hoursEntered, decFromString1) AndAlso
   decFromString1 >= 1 AndAlso
   decFromString1 <= 24 Then

    hoursEntered = "Value: " + hoursEntered
    LabelFour.Content = hoursEntered
Else
    LabelFour.Content = "Value is not Numeric!"
End If
MatSnow
  • 7,357
  • 3
  • 19
  • 31
  • This worked and it helped me view the order for the expression needed. Thank You! – Javacodeman113 Jan 31 '18 at 07:50
  • You're welcome. I've now realized that you're seemingly using WPF, not WinForms, because there's no `Content`-property on a WinForms-Label. Therefore maybe this question is of interest to you: https://stackoverflow.com/questions/841293/where-is-the-wpf-numeric-updown-control – MatSnow Jan 31 '18 at 07:54
  • 1
    Yes that also helps with my next solution as well. Thank you again! – Javacodeman113 Jan 31 '18 at 08:06
  • I could have used an ItemBox selection from 1 to 24 in range (converting it to decimal) also but the requirement was to allow the user to input a number of hours an object was used. – Javacodeman113 Jan 31 '18 at 08:19