Help I am getting an error RRS feed

  • Question

  • I am Getting an error on the second line it says: Input string was not in correct format

     Dim Score As Double
            Score = Double.Parse(TextBox_AvgScore.Text) - Input string was not in correct format
            Select Case (Score)
                Case 100
                    With Label_LetterGrade
                        .Text = "A+"
                        .ForeColor = Color.Blue
                    End With
                Case 93 To 100
                    With Label_LetterGrade
                        .Text = "A"
                        .ForeColor = Color.Blue
                    End With

            End Select
        End Sub
    End Class

    • Moved by Bob Ding Thursday, May 25, 2017 2:12 AM
    Thursday, May 11, 2017 4:04 AM

All replies

  • I would recommend asking this in the VB forum:


    At the outset it looks like the text entered in the textbox contains something else than numbers so the Double.Parse is not able to convert it to a double. For example, is I type 233d45, I would get this error as the textbox contains a letter d.

    Please Mark as Answered if this answers your question Or UnMark as Answered if it did not. Happy to Help :)

    Thursday, May 11, 2017 5:37 AM

  • Hi Citrus_ ,

    >>I  am Getting an error on the second line it says: Input string was not in correct format

    As vijaykamat suggested, you should make sure textbox Does not contain any non-numeric characters.

    You can set breakpoint debugging to see what the textbox  actual character is.

    In order to avoid a similar error, I suggest you can verify the textbox value firstly when you use it.

    The following method may help you.


     <TextBox x:Name="textBox" HorizontalAlignment="Left" Height="23" Margin="85,47,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120" PreviewTextInput="textBox_PreviewTextInput" DataObject.Pasting="textBox_Pasting"/>


      Private Sub textBox_PreviewTextInput(sender As Object, e As TextCompositionEventArgs)
            e.Handled = Not IsTextAllowed(e.Text)
        End Sub
        Private Shared Function IsTextAllowed(text As String) As Boolean
            Dim regex As New Regex("[^0-9.-]+")
            'regex that matches disallowed text
            Return Not regex.IsMatch(text)
        End Function
        Private Sub textBox_Pasting(sender As Object, e As DataObjectPastingEventArgs)
            If e.DataObject.GetDataPresent(GetType([String])) Then
                Dim text As [String] = DirectCast(e.DataObject.GetData(GetType([String])), [String])
                If Not IsTextAllowed(text) Then
                End If
            End If
        End Sub

    You can also extend the Functionality of the TextBox to tell us TextBox accepts integer, decimal, or any kind of values. You can refer to the implementation of the following article.

    WPF Maskable TextBox for Numeric Values:

    Best Regards,

    Yohann Lu

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, May 11, 2017 6:10 AM
  • It seems that you are not using WPF but WinForms.  You should ask this question in the VB forum.


    • Edited by sqlguy Thursday, May 11, 2017 2:04 PM
    Thursday, May 11, 2017 11:55 AM