0

我是初学者,正在尝试使用 vb.net 构建 Windows Phone 应用程序。我想验证空文本框,但是一旦启动应用程序,它就会被阻止并显示以下异常错误。如果字段已填充但如果某些字段为空则会出现错误。

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: 

这是代码:

 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))

我不知道这到底是怎么回事。我尝试了很多方法并出现了同样的错误。

提前致谢

注册

4

3 回答 3

0

有几种方法可以转换StringsInt32. 您正在实施的那个,CInt,抛出System.InvalidCastException无效的强制转换。


在您的代码中,您正在测试String.IsNullOrEmpty,但您允许代码继续运行,Cast即使您提交了MessageBox. 处理此问题的一种方法可能是Exit Sub在显示消息框后添加,但这并不能真正处理所有可能的错误。
所以你可以把这段代码放在一个Try块中,Catch System.InvalidCastException这样你就可以正确处理这个错误,或者你可以实现不同的转换方法。也许是这样的:

    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
于 2015-04-25T19:57:05.853 回答
0

谢谢大家,它工作。另外,我在 mdsn 帮助中寻找并查看 Return 表达式,它可以工作。这是样本:

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))

但我会使用你的代码,因为它是对的!

谢谢大家!

问候

于 2015-04-26T09:01:13.067 回答
0

尝试以下

foreach (Control ctrl in this.groupBox1.Controls)
{
    if (ctrl is TextBox) 
    {
        if (ctrl.Text != "") 
        {
          `enter code here`
        }
    }
}
于 2016-12-08T23:13:01.437 回答