Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if Masked textbox is empty?

I have several textboxes and masked texboxes in a winform that I need to check if they are empty, null or nothing before proceeding.

The code I have for the most part is working as intended, if there is an empty texbox I get a message telling the user that the textbox is empty and it exits the sub, but for some reason that is not checking the masked textboxes.

Maybe I'm wrong and it is checking them, but since they have the mask they're not considered as empty or null.

Your help with checking if the 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
like image 977
David Avatar asked Jul 25 '13 17:07

David


3 Answers

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

Endif
like image 90
Tim Avatar answered Oct 20 '22 00:10

Tim


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

If TypeOf (myControl) Is TextBox Then

Since the MaskedTextBox control does not inherit from the TextBox class, you would need to check for that type separately, like this:

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

However, since they do both inherit from the TextBoxBase class, you could just check for that instead:

If TypeOf (myControl) Is TextBoxBase Then
like image 2
Steven Doggart Avatar answered Oct 20 '22 01:10

Steven Doggart


Try this:

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
like image 1
Eric J Avatar answered Oct 20 '22 02:10

Eric J