9 Replies - 465 Views - Last Post: 17 October 2013 - 09:02 AM Rate Topic: -----

#1 YeaH_IKnoW13  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 17-October 13

If statements Msgbox error, "Input string was not in a correct for

Posted 17 October 2013 - 05:59 AM

Attached ImageHi, I was writing a code in vb to build a pyramid. I coded it up to 10 columns, and as I was inserting error checks. I am getting a message " Input string was not in a correct format."
Here is the code that it is pointing to: Image attached.

And it gives me error ONLY IF I am putting a blank OR writing letters in the inputBox. But I can put a number greater than
10 and it will let me click " OK " on the Msgbox and will not give me error. But for the code above the " < 10 " it gives me error if I click " OK " in the MSGbox.


Is This A Good Question/Topic? 0
  • +

Replies To: If statements Msgbox error, "Input string was not in a correct for

#2 astonecipher  Icon User is offline

  • D.I.C.
  • member icon

Reputation: 768
  • View blog
  • Posts: 3,358
  • Joined: 03-December 12

Re: If statements Msgbox error, "Input string was not in a correct for

Posted 17 October 2013 - 06:15 AM

That if statement needs to be in your validation code making sure it isNumeric, I would also recommend using .trim to remove forward and trailing spaces. Basically, you validate that the input is numeric and not an empty string, after the msgbox clears it try's to convert empty spaces or letters to double.

Hope that helps.
Was This Post Helpful? 0
  • +
  • -

#3 YeaH_IKnoW13  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 17-October 13

Re: If statements Msgbox error, "Input string was not in a correct for

Posted 17 October 2013 - 06:50 AM

View Postastonecipher, on 17 October 2013 - 06:15 AM, said:

That if statement needs to be in your validation code making sure it isNumeric, I would also recommend using .trim to remove forward and trailing spaces. Basically, you validate that the input is numeric and not an empty string, after the msgbox clears it try's to convert empty spaces or letters to double.

Hope that helps.

Could you please elaborate a little bit more. I am kind of bad at this.
Was This Post Helpful? 0
  • +
  • -

#4 HeBTeMy  Icon User is offline

  • D.I.C Regular

Reputation: 20
  • View blog
  • Posts: 299
  • Joined: 05-October 12

Re: If statements Msgbox error, "Input string was not in a correct for

Posted 17 October 2013 - 06:51 AM

I also suggest using the eventchanged for the textboxes instead of validating all in a button, it saves time.

Like this:

Private Sub TextBox2_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress
        If Not Char.IsDigit(e.KeyChar) And Not Char.IsControl(e.KeyChar) Then
            e.Handled = True
        End If
    End Sub



Edit: I also recommend using TryParse, it will catch any errors.

This post has been edited by HeBTeMy: 17 October 2013 - 06:59 AM

Was This Post Helpful? 0
  • +
  • -

#5 astonecipher  Icon User is offline

  • D.I.C.
  • member icon

Reputation: 768
  • View blog
  • Posts: 3,358
  • Joined: 03-December 12

Re: If statements Msgbox error, "Input string was not in a correct for

Posted 17 October 2013 - 06:57 AM

Change your first if statement to:


If isnumeric(txtinput.text) = false OR txtinput.text = "" then
    messagebox.show(msg)
Else
    convert.todouble
End if


Was This Post Helpful? 0
  • +
  • -

#6 YeaH_IKnoW13  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 17-October 13

Re: If statements Msgbox error, "Input string was not in a correct for

Posted 17 October 2013 - 07:05 AM

View Postastonecipher, on 17 October 2013 - 06:57 AM, said:

Change your first if statement to:


If isnumeric(txtinput.text) = false OR txtinput.text = "" then
    messagebox.show(msg)
Else
    convert.todouble
End if


It worked! Thanks!
Was This Post Helpful? 0
  • +
  • -

#7 HeBTeMy  Icon User is offline

  • D.I.C Regular

Reputation: 20
  • View blog
  • Posts: 299
  • Joined: 05-October 12

Re: If statements Msgbox error, "Input string was not in a correct for

Posted 17 October 2013 - 07:08 AM

I guess you didn't like my suggestion.
Well, glad you still worked it out.
Was This Post Helpful? 0
  • +
  • -

#8 YeaH_IKnoW13  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 17-October 13

Re: If statements Msgbox error, "Input string was not in a correct for

Posted 17 October 2013 - 07:23 AM

View PostHeBTeMy, on 17 October 2013 - 07:08 AM, said:

I guess you didn't like my suggestion.
Well, glad you still worked it out.

I am sorry for not replying. I tried your code, and it would have "ehandled" and other terms in the code underlined blue. I am guessing just maybe it is because I am using vb express 2012 for desktop. So maybe different commands? Sorry again.
Was This Post Helpful? 0
  • +
  • -

#9 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1062
  • View blog
  • Posts: 4,143
  • Joined: 02-July 08

Re: If statements Msgbox error, "Input string was not in a correct for

Posted 17 October 2013 - 07:38 AM

You should be using Double.TryParse for this.

Dim dbl As Double
If Double.TryParse(txtInput.Text, dbl) Then
   ' use the dbl variable - it has the converted value
Else 
   ' it did not convert
End If

Was This Post Helpful? 0
  • +
  • -

#10 HeBTeMy  Icon User is offline

  • D.I.C Regular

Reputation: 20
  • View blog
  • Posts: 299
  • Joined: 05-October 12

Re: If statements Msgbox error, "Input string was not in a correct for

Posted 17 October 2013 - 09:02 AM

View PostYeaH_IKnoW13, on 17 October 2013 - 07:23 AM, said:

View PostHeBTeMy, on 17 October 2013 - 07:08 AM, said:

I guess you didn't like my suggestion.
Well, glad you still worked it out.

I am sorry for not replying. I tried your code, and it would have "ehandled" and other terms in the code underlined blue. I am guessing just maybe it is because I am using vb express 2012 for desktop. So maybe different commands? Sorry again.


If you tell me what does it say exactly, i use it in vs 2010 and it doesn't show anything, and just allow digits, no spaces no anything, like you asked for at least one.

But as post above me said and what i've said earlier you should use Tryparse,
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1