0

Hello I'm trying to create a windows form that will have the user enter a 6-digit sales order into a textbox. Then, hit a "start" button and check to make sure that the user actually typed in 6 integers. Thank you!

Public Class Form1
Private Sub startButton_Click(sender As Object, e As EventArgs) Handles startButton.Click
    Dim salesOrder As integer 
    salesOrder = txtboxSalesOrder.Text
    If salesOrder 'Is a 6-digit integer 
    Then
    '.....Do Something


Private Sub txtboxSalesOrder_TextChanged(sender As Object, e As EventArgs) Handles txtboxSalesOrder.TextChanged
  End Sub
End Class
Bcon615
  • 5
  • 5

2 Answers2

0

You just need to convert the input from your textbox in an integer and checks the value obtained

Dim salesOrder As integer 
If Int32.TryParse(txtboxSalesOrder.Text, salesOrder) Then
   If salesOrder >= 100000 andalso salesOrder <= 999999 Then 
      ' valid number 
   else
      MessageBox.Show("Not a 6 digits number")
   End if
Else
   MessageBox.Show("Please type a number between 100000 and 999999")
End If

Notice that your code tries to automatically convert whatever has been typed in the textbox to an integer. This of course is a route that leads to errors. You shouldn't do it and you should set the Option Strict of your project to On to get an early error when you write this kind of potential wrong code.

Instead Int32.TryParse checks if you have a valid number and return false if not or set your variable with the result of the conversion.

Steve
  • 213,761
  • 22
  • 232
  • 286
  • That worked perfectly thank you! How would I also include checking to make sure that no letters are added? – Bcon615 Dec 14 '15 at 21:59
  • This is more complex. The TextChanged event should help a bit, but there is the copy/paste problem to avoid. I prefer a lazy approach to this kind of problems. Let the user typoe whatever he likes but check before taking an action on the input. – Steve Dec 14 '15 at 22:02
  • A few ways you could handle that. Do a check at validation like this 'If isnumeric(txtboxSalesOrder.Text)' Do a check as soon as keydown and use e.handled to prevent invalid characters. Usually done with a select case and specifying acceptible asc keykode value range. – JoshF Dec 14 '15 at 22:02
  • [There is this question and answer](http://stackoverflow.com/questions/463299/how-do-i-make-a-textbox-that-only-accepts-numbers) that digs in the subject. It is in C# but I hope that it is understandable – Steve Dec 14 '15 at 22:04
  • That's exactly what I was trying to describe Steve. Thanks for the link :) – JoshF Dec 14 '15 at 22:11
0

Steve's solution will work, obviously as you've tested it, but I would consider hooking into validating or validation event for when you leave the textbox to check the value.

Also consider using an errorprovider instead of a messagebox.

Private Sub txtboxSalesOrder_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtboxSalesOrder.Validating
    If IsNumeric(sender.Text) Then
        If <> sender.text.ToString.Length = 6 Then
            MessageBox.Show("Please type a number between 100000 and 999999")
        End If
    End If  
End Sub

That will verify you have correct data before you hit the Start button.

JoshF
  • 159
  • 4
  • 15