在VB.NET中验证空文本框

时间:2015-04-25 14:56:52

标签: vb.net textbox validating

我是初学者并尝试使用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))

我不知道到底发生了什么。我尝试了很多方法并出现了同样的错误。

提前致谢

注册

3 个答案:

答案 0 :(得分:0)

有多种方法可以将Strings转换为Int32。您正在实施的那个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

答案 1 :(得分:0)

感谢所有人,并且工作正常。此外,我正在寻找mdsn帮助,看到返回表达式,它的工作原理。这是样本:

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

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

谢谢大家!

此致

答案 2 :(得分:0)

尝试以下

foreach (Control ctrl in this.groupBox1.Controls)
{
    if (ctrl is TextBox) 
    {
        if (ctrl.Text != "") 
        {
          `enter code here`
        }
    }
}