How to check if Masked text box is empty?

I have several text boxes and masked texboxes in winform that I need to check if they are empty, zero or nothing before continuing.

The code that I use for the most part works as intended, if there is an empty text code, I get a message telling the user that the text field is empty and it leaves the subroutine, but for some reason this does not check the masking of the Textboxes.

Perhaps I am mistaken and checking them out, but since they have a mask, they are not considered empty or empty.

Your help in verifying if masked texboxes are empty would be much appreciated.

This is the code:

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 
+6
source share
4 answers
 if textbox.MaskCompleted=True Then 'they entered something else ' they didnt enter anything Endif 
+3
source

The problem is that you are only looking for TextBox objects on this line:

 If TypeOf (myControl) Is TextBox Then 

Since the MaskedTextBox control MaskedTextBox not inherited from the TextBox class, you will need to check this type separately, for example:

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

However, since they both inherit from the TextBoxBase class, you can simply check this instead:

 If TypeOf (myControl) Is TextBoxBase Then 
+2
source

Try the following:

 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 
+1
source

Unconfirmed, but instead of checking for string.empty you can check it for the MaskedTextBox Mask property.

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

Source: https://habr.com/ru/post/950292/


All Articles