如何免除文本框的字段

时间:2014-03-10 17:48:26

标签: vb.net

我正在使用vb.net开发一个项目,我的表单上有4个文本框和2个tabpages。 我编写了我的程序,所有4个文本框都必须填写才能进入tabpage2。

这是我的问题。 我需要textbox1为空,即使填写了textbox2,3和4,仍然可以转到tabpage2

这是我的代码

Private Sub TabControl1_Selecting(ByVal sender As Object, ByVal e As System.Windows.Forms.TabControlCancelEventArgs) Handles TabControl1.Selecting
   For Each TextBox As Control In Me.Controls
        If TypeOf TextBox Is TextBox Then
            If TextBox.Text.Equals(String.Empty) Then
                If e.TabPage Is TabPage2 Then
                    MsgBox("fill textbox")
                    e.Cancel = True
                End If
            End If
        End If
    Next

1 个答案:

答案 0 :(得分:0)

好吧,只需在条件检查中添加And TextBox.Name <> TextBox1.Name

Private Sub TabControl1_Selecting(ByVal sender As Object, ByVal e As System.Windows.Forms.TabControlCancelEventArgs) Handles TabControl1.Selecting
   For Each TextBox As Control In Me.Controls
        If TypeOf TextBox Is TextBox Then
            If TextBox.Text.Equals(String.Empty) And TextBox.Name <> TextBox1.Name Then
                If e.TabPage Is TabPage2 Then
                    MsgBox("fill textbox")
                    e.Cancel = True
                End If
            End If
        End If
    Next

顺便说一句,您可以通过直接声明TextBox As TextBox而不是As Control来缩短此代码:

    Private Sub TabControl1_Selecting(ByVal sender As Object, ByVal e As System.Windows.Forms.TabControlCancelEventArgs) Handles TabControl1.Selecting
   For Each TextBox As TextBox In Me.Controls.OfType(Of Textbox)()

            If TextBox.Text.Equals(String.Empty) And TextBox.Name <> TextBox1.Name Then
                If e.TabPage Is TabPage2 Then
                    MsgBox("fill textbox")
                    e.Cancel = True
                End If

        End If
    Next