8

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
David
  • 163
  • 2
  • 4
  • 13

4 Answers4

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

Endif
Cleptus
  • 3,446
  • 4
  • 28
  • 34
Tim
  • 66
  • 1
  • 1
2

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
Steven Doggart
  • 43,358
  • 8
  • 68
  • 105
1

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
Eric J
  • 227
  • 5
  • 16
  • Is there a way to make sure that the check follows the tab order instead of checking random boxes? also is there a way to show in the messagebox a description instead of the .Name property? for example to show Shift instead of txtShift. Thanks – David Jul 25 '13 at 21:29
  • Sorry, I don't know...I think the index number for controls is the order in which they were added to the form if that helps, the loop would probably follow this order, but I'm not 100% on that – Eric J Jul 25 '13 at 21:29
  • I'm not sure about your 2nd question either. If the name of your control is txtShift, you'd need to figure out how to remove the "txt" obviously...maybe you can work with that 1st capital letter somehow. There may be a better way to accomplish it. Just a fleeting thought – Eric J Jul 25 '13 at 21:37
  • You may also want to look at using the tooltiptext property or the tag property of your controls. Perhaps instead of .Name, you could use .Tag or .ToolTipText. I've never tried anything like this, just my 2 cents. I see what you're wanting to accomplish though – Eric J Jul 25 '13 at 21:42
  • Look at this link. They're discussing how to loop through controls using tab order in vb.net. Should be of some help, but sounds rather tedious and not too reliable: http://www.vbforums.com/showthread.php?290986-For-Each-Loop-In-TabIndex-order – Eric J Jul 25 '13 at 21:49
  • Thanks Erick i used the .Tag instead of the .Name and it worked. I'll try to recreate the controls in the order i need them and see if that works. Thanks again. – David Jul 25 '13 at 22:12
  • Weird nobody told you to use MaskComplete and MaskFull (both are MaskedTextBox properties) to know if all blanks were complete being false if the mask is not fully complete. – Esselans Jul 26 '13 at 00:17
1

Untested but instead of checking against string.empty, you could check it against the MaskedTextBox's 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
keyboardP
  • 68,824
  • 13
  • 156
  • 205