VB.Net InputBox validation negative number -


i'm working on program user input number in inputbox. write codes validate input value numeric, couldn't came procedure validate negative number inputbox.

        dim userinput string = ""         dim userinputasnumber integer         dim sumoftotal integer          'to display inputbox         userinput = (inputbox("enter positive number", "input needed"))          'to capture userinput not numeric value         while (not isnumeric(userinput))             userinput = (inputbox("enter positive number", "input needed"))         end while          'if userinput number, convert value numeric variable         userinputasnumber = cint(userinput)          if (userinputasnumber <= 0)             userinput = (inputbox("enter positive number", "input needed"))         else             sumoftotal = cint(((userinputasnumber * (userinputasnumber + 1)) / 2))             'calculation         end if          messagebox.show("the sum " & cstr(sumoftotal))     end sub 

you need loop through code until valid positive integer. can simplify things using integer.tryparse validate string , convert integer (invalid strings convert zero).

private sub btnenternumbers_click(sender object, e eventargs) handles btnenternumbers.click     dim userinput string = ""     dim userinputasnumber integer     dim sumoftotal integer      while userinputasnumber <= 0         'to display inputbox         userinput = (inputbox("enter positive integer value", "input needed", "10"))          'convert integer, 0 if not valid integer         integer.tryparse(userinput, userinputasnumber)     loop      '    gauss formula calculate total of sequence of numbers userinput     sumoftotal = cint(userinputasnumber * (userinputasnumber + 1) / 2)      messagebox.show("the sum of numbers 1 through " & cstr(userinputasnumber) & " " & cstr(sumoftotal)) end sub 

Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -