2013-01-11 49 views
0

我想檢查我的窗體中的控件是否爲空...我有一個很長的代碼。我給出下面給出的代碼。在VB.NET中檢查空控件

Public totflag As Boolean 
Private Sub BTNSAVE_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BTNSAVE.Click 
    CheckMyControls() 
    If totflag = False Then 
     MessageBox.Show("Give Compleate Data!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning) 
     coloring() 
    ElseIf totflag = True Then 
     MessageBox.Show("Success", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information) 
    End If 
End Sub 
Public Sub coloring() 
    Dim txt, cmb, mtxt, rtxt As Control 
    For Each txt In EMPGBDATA.Controls 
     If TypeOf txt Is TextBox Then 
      If txt.Text = "" Then 
       txt.BackColor = Color.Red 
      End If 
     End If 
    Next 
    For Each cmb In EMPGBDATA.Controls 
     If TypeOf cmb Is ComboBox Then 
      If cmb.Text = "Select" Then 
       cmb.BackColor = Color.Red 
      End If 
     End If 
    Next 
    For Each mtxt In EMPGBDATA.Controls 
     If TypeOf mtxt Is MaskedTextBox Then 
      If mtxt.Text = "" AndAlso mtxt.Name <> "MTXTPFESI" Then 
       mtxt.BackColor = Color.Red 
      End If 
     End If 
    Next 
    For Each rtxt In EMPGBDATA.Controls 
     If TypeOf rtxt Is RichTextBox Then 
      If rtxt.Text = "" Then 
       rtxt.BackColor = Color.Red 
      End If 
     End If 
    Next 

End Sub 
Public Function CheckMyControls() As Boolean 
    Dim txt, cmb, mtxt, rtxt As Control 
    Dim flagtxt, flagcmb, flagmtxt, flagrtxt As Boolean 
    flagtxt = False 
    For Each txt In EMPGBDATA.Controls 
     If TypeOf txt Is TextBox Then 
      If txt.Text = "" Then 
       flagtxt = True 
      End If 
     End If 
    Next 
    flagcmb = False 
    For Each cmb In EMPGBDATA.Controls 
     If TypeOf cmb Is ComboBox Then 
      If cmb.Text = "Select" Then 
       flagcmb = True 
      End If 
     End If 
    Next 
    flagmtxt = False 
    For Each mtxt In EMPGBDATA.Controls 
     If TypeOf mtxt Is MaskedTextBox Then 
      If mtxt.Text = "" AndAlso mtxt.Name <> "MTXTPFESI" Then 
       flagmtxt = True 
      End If 
     End If 
    Next 
    flagrtxt = False 
    For Each rtxt In EMPGBDATA.Controls 
     If TypeOf rtxt Is RichTextBox Then 
      If rtxt.Text = "" Then 
       flagrtxt = True 
      End If 
     End If 
    Next 
    If flagtxt = True Or flagcmb = True Or flagmtxt = True Or flagrtxt = True Then 
     totflag = False 
    Else 
     totflag = True 
    End If 
    Return totflag 
End Function 

其實我不想檢查一個PFESI文本框。如果它是空的或沒有。這是一個蒙面的文本框。我的問題是,當我按下提交按鈕時,所有控件都有數據,它顯示的消息框「提供完整的數據!」。沒有數據的控件也顯示相同的消息。請嘗試檢查代碼並給我解決方案。

回答

0

您正試圖做得太複雜。 我不是在電腦前,所以也許代碼有小錯誤:

If CheckMyControls() = False Then 
     MessageBox.Show("Give Compleate Data!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning) 
     coloring() 
    Else 
     MessageBox.Show("Success", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information) 
    End If 

End Sub 

Public Sub coloring() 
    For Each cntrl In Me.Controls 
     Select Case cntrl.GetType 
      Case GetType(ComboBox) 
       If cntrl.Text = "Select" Then 
        cntrl.BackColor = Color.Red 
        Exit For 
       End If 
      Case GetType(MaskedTextBox) 
       If cntrl.Text = "" AndAlso cntrl.Name <> "MTXTPFESI" Then 
        cntrl.BackColor = Color.Red 
       End If 
      Case Else 
       If cntrl.Text = "" Then 
        cntrl.BackColor = Color.Red 
       End If 

     End Select 
    Next 

End Sub 

Public Function CheckMyControls() As Boolean 
    Dim bEmptyControlFlag As Boolean 
    For Each cntrl In Me.Controls 
     Select Case cntrl.GetType 
      Case GetType(ComboBox) 
       If cntrl.Text = "Select" Then 
        bEmptyControlFlag = True 
        Exit For 
       End If 
      Case Else 
       If cntrl.Text = "" Then 
        bEmptyControlFlag = True 
       End If 

     End Select 
    Next 

    Return Not bEmptyControlFlag 
End Function 
+0

我添加其他部分與我的代碼檢查每....它工作正常....感謝名單爲您重播.. .. – Thanzeem