0

Last month I asked how to get a leveling up system in VB.Net, which worked wonderfully. My problem now is that if a user tries to enter any string (including a mix of characters) it does nothing for the level box, instead of catching the user out. My current code is as follows:

ElseIf txtExperience.Text = "" Or txtExperience.Text = Letters Then
        MessageBox.Show("Input must be a whole number between 0 and 100000", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

I vaguely remember doing something similar to this when I was first learning VB.Net, however I no longer have access to that program and I can't seem to find anything which might catch the user when entering any letters into the box.

This is probably some nooby mistake, but thanks for any help you might be able to give :)

  • Use a `NumericUpDown` control instead. – Jens Jan 04 '17 at 10:41
  • Thank you for your help, however I would prefer to keep it as a textbox. Is there an easy way of keeping the textbox? – Riddler Fish Jan 04 '17 at 10:46
  • Have a look at [this](http://stackoverflow.com/questions/21894851/checking-for-numeric-value-entered-in-text-box-in-visual-basic) – Bugs Jan 04 '17 at 10:50
  • 1
    Use Int32.TryParse to see if the text can be converted to a number - if not, warn the user. – Hans Kesting Jan 04 '17 at 10:55
  • Your `txtExperience.Text = Letters` tests for an *exact* match between the typed text and the contents of a string variable `Letters`, it does not check whether there are *any* letters in that text. – Hans Kesting Jan 04 '17 at 10:57
  • Thank you @Jinx88909 for the help, however now when I insert string, instead of crashing, it just ignores it. Any ideas? – Riddler Fish Jan 04 '17 at 11:14
  • Please show the code you added that failed. Edit your question and **don't** add it as a comment. – Chris Dunaway Jan 04 '17 at 16:25

1 Answers1

0

Have you tried something like;

For Each c As Char in txtExperience.Text
  If Char.IsDigit(c) = False Then
   MsgBox("Only digits are allowed - Character " & c & " is not valid")
   Exit Sub
  End If
Next
David
  • 2,298
  • 6
  • 22
  • 56