2013-07-25 196 views
6

我有一個winform中的幾個文本框和蒙面的texboxes,我需要檢查它們是否爲空,null或沒有任何處理之前。如何檢查屏蔽文本框是否爲空?

我的代碼大部分是按預期工作,如果有空的texbox我得到一條消息,告訴用戶文本框是空的,它退出子,但由於某種原因,不檢查蒙面文本框。

也許我錯了,它正在檢查他們,但由於他們有面具,他們不被視爲空或空。

你的幫助檢查是否蒙面texboxes是空的將不勝感激。

這是代碼:

Private Sub btnCargarInformacion_Click(sender As System.Object, e As System.EventArgs) Handles btnCargar.Click 
    For Each myControl As Control In Me.GroupBox1.Controls 
     If TypeOf (myControl) Is TextBox Then 
      If myControl.Text.Equals(String.Empty) Then 
       MessageBox.Show(String.Format("Please Fill the following Textboxes: {0}", String.Join(",", myControl.Name))) 
      End If 
      If myControl.Text.Equals(String.Empty) Then 
       Exit Sub 
      End If 
     End If 
    Next 
    Dim PartePersonalTableApt As New PersonalObraDataSetTableAdapters.PartePersonalTableAdapter 
    Dim PersonalObTableApt As New PersonalObraDataSetTableAdapters.PersonalObTableAdapter 
    PartePersonalTableApt.ClearBeforeFill = True 
    PartePersonalTableApt.FillByFecha(PersonalObraDataSet.PartePersonal, txtDate.Text, txtDepartamento.Text, txtTurno.Text) 
    PersonalObTableApt.ClearBeforeFill = True 
    PersonalObTableApt.Fillby(PersonalObraDataSet.PersonalOb) 
End Sub 

回答

3
if textbox.MaskCompleted=True Then 
    'they entered something 
else 
    ' they didnt enter anything 

Endif 
2

的問題是,你只能在這條線上尋找TextBox對象:

If TypeOf (myControl) Is TextBox Then 

由於MaskedTextBox控制不從TextBox類繼承,您需要單獨檢查該類型,如下所示:

If (TypeOf (myControl) Is TextBox) Or (TypeOf (myControl) Is MaskedTextBox) Then 

但是,因爲他們這樣做無論是從TextBoxBase類繼承,你可以只檢查是否存在,而不是:

If TypeOf (myControl) Is TextBoxBase Then 
+0

我改成了texboxbase,但它仍然忽略了maskedtexbox。 – David

1

試試這個:

If TypeOf myControl Is MaskedTextBox Then 
     If CType(myControl, MaskedTextBox).Text.Equals(String.Empty) Then 
      MessageBox.Show(String.Format("Please Fill the following Textboxes: {0}", String.Join(",", myControl.Name))) 
     End If 
     If CType(myControl, MaskedTextBox).Text.Equals(String.Empty) Then 
      Exit Sub 
     End If 
End If 
+0

有沒有辦法確保檢查遵循Tab鍵順序而不是檢查隨機框?還有一種方法可以顯示消息框中的描述,而不是.Name屬性?例如顯示Shift而不是txtShift。謝謝 – David

+0

對不起,我不知道......我認爲控件的索引號是它們被添加到表單的順序,如果有幫助的話,循環可能會遵循這個順序,但我不是100% –

+0

我不確定你的第二個問題。如果你的控件的名字是txtShift,你需要弄清楚如何清除「txt」......也許你可以用第一個大寫字母來處理。可能有更好的方法來實現它。只是短暫的想法 –

1

未經測試,但不是檢查對string.empty,你可以檢查它違背MaskedTextBox的Mask屬性。

If myControl.Text.Equals(myControl.Mask) Then 
    MessageBox.Show(String.Format("Please Fill the following Textboxes: {0}", String.Join(",", myControl.Name))) 
End If