-2

Working on a discount field. The field needs to only allow positive numbers between 0 and 100. Field is a text box. Using visual studio and visual basic.

braX
  • 11,506
  • 5
  • 20
  • 33
Jammocash
  • 3
  • 4
  • 1
    This could be done in a number of ways, several of which are in the top 10 Google results for "Visual Basic textbox positive numbers." Did you try any of those? – BJ Myers Dec 19 '17 at 18:46
  • To be honest I don't understand the IsNumeric part, and none of them show how to deny negative numbers. Private Sub txtDiscount_TextChanged(sender As Object, e As EventArgs) Handles txtDiscount.TextChanged If txtDiscount.Text < 0 Then MsgBox("Discount needs to be positive") End If End Sub That's what I've got so far. – Jammocash Dec 19 '17 at 19:25
  • 3
    Use a [**`NumericUpDown`**](https://msdn.microsoft.com/en-us/library/system.windows.forms.numericupdown(v=vs.110).aspx) instead. It is limited to numbers only and you can customize the range, display, decimal numbers, etc. – Visual Vincent Dec 19 '17 at 19:37

1 Answers1

1

You could use a NumericUpDown control along with its Minimum, Maximum, and DecimalPlaces properties.

However, if you don't like the scroll box and really want to use a TextBox, it can be done like this:

Private min As Decimal = 1
Private max As Decimal = 100

Private Sub txtDiscount_TextChanged(sender As Object, e As EventArgs) Handles txtDiscount.TextChanged
    Dim i As Decimal
    Static lastValidText = ""
    If (txtDiscount.Text = "") OrElse (Decimal.TryParse(txtDiscount.Text, i) AndAlso (i >= min And i <= max)) Then
        lastValidText = txtDiscount.Text
    Else
        txtDiscount.Text = lastValidText
        txtDiscount.SelectionStart = txtDiscount.TextLength
    End If
End Sub
djv
  • 15,168
  • 7
  • 48
  • 72
  • 1
    Dim min As Decimal = 0.00 Dim max As Decimal = 100.0 Dim i As Decimal Static lastValidText = "" If Decimal.TryParse(txtDiscount.Text, i) AndAlso (i >= min And i <= max) Then lastValidText = txtDiscount.Text Else txtDiscount.Text = lastValidText txtDiscount.SelectionStart = txtDiscount.TextLength End If End Sub That's what I got to. My only issue is that I can't erase all the characters, as in if I put in 100 it only erases back to 1. And sorry about the formatting. – Jammocash Dec 19 '17 at 20:53
  • Right! Didn't test all edge cases. Fixed in my answer. – djv Dec 19 '17 at 21:13
  • you should move this to keypressed event. you can evaluate the value before the key is inserted to the textbox. If the value is invalid then e.keychar="" or change the value of the textbox to the max value or min value depending if the user was trying -5 or 105 – Chillzy Dec 19 '17 at 21:17
  • @Chillzy then it wouldn't work with mouse clicks i.e. drag-drop or right-click paste. Though I'm not sure if it's a requirement (but I did consider it either way). – djv Dec 19 '17 at 21:23
  • a drag and drop has to be programmed to work so it should be an issue. I never tried a right click paste to see which events get trigger. Nicely done but I wasn't sure why go so far but great answer. – Chillzy Dec 19 '17 at 21:27
  • Pretty easy question, (though the quality is debatable) but it's holiday season so why not. – djv Dec 19 '17 at 21:28
  • Hey thanks for the help! – Jammocash Dec 19 '17 at 21:45
  • @djv : Actually there [are workarounds](https://stackoverflow.com/a/6222646) to remove the scroll box/spinbox of a `NumericUpDown`. Neat textbox-solution you made though! – Visual Vincent Dec 19 '17 at 22:51
  • @VisualVincent thanks. I've seen that solution, but it has a blank space where the scrollers were. I've made my own by subclassing TextBox and adding a numeric Value, Min, and Max properties and a Format (i.e. for engineering units and decimal places). Too big for this forum though :) – djv Dec 20 '17 at 03:45