0

I´m beginner and trying build a Windows Phone App with vb.net. I want to validate empty textbox but once start app, this blocked and it shows the following exception error.This calculate fine if the fields are filled but if some field is empty appears the error.

El código de usuario no controló System.InvalidCastException
  HResult=-2147467262
  Message=Input string was not in a correct format.
  Source=Conversionvbnet
  StackTrace:
       at Microsoft.VisualBasic.CompilerServices.Conversions.ToInteger(String Value)
       at Conversionvbnet.CourseGSWCA.calculatebtn_Click(Object sender, RoutedEventArgs e)
       at System.Windows.Controls.Primitives.ButtonBase.OnClick()
       at System.Windows.Controls.Button.OnClick()
       at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)
       at System.Windows.Controls.Control.OnMouseLeftButtonUp(Control ctrl, EventArgs e)
       at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName)
  InnerException: System.FormatException
       HResult=-2146233033
       Message=Input string was not in a correct format.
       Source=Conversionvbnet
       StackTrace:
            at Microsoft.VisualBasic.CompilerServices.Conversions.ParseDouble(String Value)
            at Microsoft.VisualBasic.CompilerServices.Conversions.ToInteger(String Value)
       InnerException: 

And it´s the code:

 Private Sub calculatebtn_Click(sender As Object, e As RoutedEventArgs) Handles calculatebtn.Click
    Dim windspeed As String = wsptxt.Text
    Dim windirection As String = wdtxt.Text
    Dim heading As String = headingtxt.Text
    Dim speed As String = tastxt.Text
    Dim valor As String = datosmsg.Text

     If String.IsNullOrEmpty(wsptxt.Text.ToString()) Then
        MessageBox.Show("Faltan datos")
    ElseIf String.IsNullOrEmpty(wdtxt.Text.ToString()) Then
        MessageBox.Show("Faltan datos")
    ElseIf String.IsNullOrEmpty(headingtxt.Text.ToString()) Then
        MessageBox.Show("Faltan datos")
    ElseIf String.IsNullOrEmpty(tastxt.Text.ToString()) Then
        MessageBox.Show("Faltan datos")
    End If

    datosmsg.Text = CStr(CInt(wsptxt.Text) + CInt(wdtxt.Text) + CInt(headingtxt.Text) + CInt(tastxt.Text))

I don´t know exactly what happen about this. I tried many ways and appear the same error.

Thanks in advance

Reg

S. Adam Nissley
  • 776
  • 1
  • 5
  • 13
  • Don't use CInt() when your code already decided that the text isn't valid. You need an Else clause to that chain of If/ElseIf statements. Move the last line of code into it. – Hans Passant Apr 26 '15 at 09:03

3 Answers3

0

There are several ways to convert Strings to Int32. The one you are implementing, CInt, throws System.InvalidCastException for invalid casting.


In your code, you are testing String.IsNullOrEmpty but you are allowing the code to continue running, and it will later fail to Cast even after you presented a MessageBox. One way to handle this might be to add Exit Sub after you show the message box, but that does not really handle all possible errors.
So you could put this section of code in a Try block, and then Catch System.InvalidCastException so that you could properly handle this error, or you could implement a different conversion method. Maybe something like:
    Dim windspeed As Int32
    Dim windirection As Int32
    Dim heading As Int32
    Dim speed As Int32

    If Int32.TryParse(wsptxt.Text, windspeed) AndAlso
        Int32.TryParse(wdtxt.Text, windirection) AndAlso
        Int32.TryParse(headingtxt.Text, windirection) AndAlso
        Int32.TryParse(tastxt.Text, windirection) Then
        datosmsg.Text = (windspeed + windirection + heading + speed).ToString
    Else
        MessageBox.Show("Faltan datos")
    End If
S. Adam Nissley
  • 776
  • 1
  • 5
  • 13
0

Thanks for all and it work. Also, I was looking for in mdsn help and see Return expression and it Works. It´s the sample:

Dim windspeed As String = wsptxt.Text
    Dim windirection As String = wdtxt.Text
    Dim heading As String = headingtxt.Text
    Dim speed As String = tastxt.Text
    Dim valor As String = datosmsg.Text

     If String.IsNullOrEmpty(wsptxt.Text.ToString()) Then
        MessageBox.Show("Faltan datos")
        Return
    ElseIf String.IsNullOrEmpty(wdtxt.Text.ToString()) Then
        MessageBox.Show("Faltan datos")
        Return
    ElseIf String.IsNullOrEmpty(headingtxt.Text.ToString()) Then
        MessageBox.Show("Faltan datos")
        Return
    ElseIf String.IsNullOrEmpty(tastxt.Text.ToString()) Then
        MessageBox.Show("Faltan datos")
        Return
    End If

    datosmsg.Text = CStr(CInt(wsptxt.Text) + CInt(wdtxt.Text) + CInt(headingtxt.Text) + CInt(tastxt.Text))

But I will use your code because it´s right!

Thanks for all!

Regards

0

try the following

foreach (Control ctrl in this.groupBox1.Controls)
{
    if (ctrl is TextBox) 
    {
        if (ctrl.Text != "") 
        {
          `enter code here`
        }
    }
}
Hadi
  • 36,233
  • 13
  • 65
  • 124