Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 8207
  • Last Modified:

How do you check if a maskedtextbox control is empty ? (PART II)

I have a mask of : 00/00/0000 90:00

But need to check if its empty, the .text property returns "__/__/____ __:__"

So I can't check to see if the text = the original mask.

I'm close to pulling my hair, and I don't have allot left ... help !

There must be some logic I'm missing ...

There has been some other answers : http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/Q_22070597.html
But none work as I need.

I've basically created a form with controls, some of these controls are required.
When the user trys to move forward off this screen, I do a check to make sure all required controls have values.
So I need to check the maskedtextbox for a value. And, the mask won't be the same each time.

0
Kinger247
Asked:
Kinger247
  • 4
  • 4
  • 2
  • +2
2 Solutions
 
newyuppieCommented:
you could check for the MaskedTextbox1.MaskCompleted boolean that indicates if the mask is complete or not...
0
 
Fernando SotoRetiredCommented:
Hi Kinger247;

Another way would be

        If Not MaskedTextBox1.MaskFull Then
            MessageBox.Show("Invalid")
        End If

Fernando
0
 
SanclerCommented:
Are you sure the .text property returns "__/__/____ __:__"?

For me, it doesn't.  The underscore is, effectively, a place-holder.  The other characters are literals.  So, for me, with the mask you specified, the .Text returned with an empty box is "  /  /       :".   Spaces are used to fill the place-holders until the last literal is reached.  Hence my suggestion in your other post.

Here's the debug output I've got with a maskedtextbox (mtb) with your mask.  I typed 2, then 3, then backspaced, then backspaced

  /  /       :
Empty
2 /  /       :
Not empty
23/  /       :
Not empty
2 /  /       :
Not empty
  /  /       :
Empty

This was the code

    Private Sub mtb_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles mtb.TextChanged
        Debug.WriteLine(mtb.Text)
        If mtb.Text = "  /  /       :" Then
            Debug.WriteLine("Empty")
        Else
            Debug.WriteLine("Not empty")
        End If
    End Sub

Roger
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
newyuppieCommented:
hi roger, i tested using a new project in vb2005 that what kinger is saying does indeed happen (at least on my machine too!) the text property returns a lot of underscores
0
 
Fernando SotoRetiredCommented:
Hi Kinger247;

Check multiple MaskedTextBox to see if they are filled in

        For Each Ctl As Control In Me.Controls
            If Ctl.GetType.Name = "MaskedTextBox" Then
                If Not CType(Ctl, MaskedTextBox).MaskFull Then
                    MessageBox.Show(Ctl.Name & " Is Invalid")
                End If
            End If
        Next

Fernando
0
 
Kinger247Author Commented:
Hang on 2 shakes I've got a bit of a crisis going on here and I need to do work on something else for an hour or 2.
Be back in a bit ..
0
 
SanclerCommented:
Let's see what gives

One form, one MaskedTextBox dragged onto it.  Nothing else done.  This code.  Same results as last time (when I set the mask in the Properties box) for me.  VB.NET 2005 Express.

Public Class Form1

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        MaskedTextBox1.Mask = "00/00/0000 90:00"
    End Sub

    Private Sub mtb_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles MaskedTextBox1.TextChanged
        Debug.WriteLine(MaskedTextBox1.Text)
        If MaskedTextBox1.Text = "  /  /       :" Then
            Debug.WriteLine("Empty")
        Else
            Debug.WriteLine("Not empty")
        End If
    End Sub

End Class

I know Kinger247's not about just now, but can someone else test that - newyuppie?  Fernando?

Roger
0
 
SanclerCommented:
OK, I think I've got it.

It's the TextMaskFormat property.  Mine is set to IncludeLiterals.  With IncludePromptAndLiterals, I get the underscores, too.

Roger
0
 
Fernando SotoRetiredCommented:
Hi Roger;

This is what I got when I ran your code. But I think that Kinger247 whants to know if all the fields of the mask were filled in.

  /  /       :
Empty
1 /  /       :
Not empty
11/  /       :
Not empty
11/2 /       :
Not empty
11/23/       :
Not empty
11/23/2      :
Not empty
11/23/20     :
Not empty
11/23/200    :
Not empty
11/23/2006   :
Not empty
11/23/2006 1 :
Not empty
11/23/2006 11:
Not empty
11/23/2006 11:0
Not empty
11/23/2006 11:00
Not empty

Fernando
0
 
SanclerCommented:
Thanks Fernando.  Your default settings for TextMaskFormat must be the same as mine.

We'll have to see if Kinger247 does want what you say but my impression was that he just wanted to test for "emptiness", and with different masks.  If so, the answer seems to me to lie TextMaskFormat being set to ExcludePromptAndLiterals and then testing, as usual, for an empty string.

Here's test code - form as before - to do that

Public Class Form1

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        MaskedTextBox1.Mask = "00/00/0000 90:00"
        MaskedTextBox1.TextMaskFormat = MaskFormat.ExcludePromptAndLiterals
    End Sub

    Private Sub mtb_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles MaskedTextBox1.TextChanged
        Debug.WriteLine(MaskedTextBox1.Text)
        If MaskedTextBox1.Text = "" Then
            Debug.WriteLine("Empty")
        Else
            Debug.WriteLine("Not empty")
        End If
    End Sub

End Class

Roger
0
 
Kinger247Author Commented:
Yes, sorry guys been a bit busy. I haven't checked any of the above codes yet, so don't know what will work. I won't get time now untill tomorrow. So I'd say pin this on the board till then ;)

Don't know if its clear what I'm trying to achieve so:

If you take it that I never know what the mask is, unless I view the .mask property.
I then need to determine if the textbox is empty. Some mask would make sure that the user filles all items in the mask other wise it will fail.
But other masks like #####, would enable the user not enter anything at all, but I want to make sure they do.

So, in short I need to make sure the user enters a value for any mask. But how can I check the value without the mask ?

0
 
Fernando SotoRetiredCommented:
Hi Roger;

Possible settings for the TextMaskFormat Property are as follows:

 ExcludePromptAndLiterals    Return only text input by the user.  
 IncludeLiterals                    Return text input by the user as well as any literal characters defined in the mask.  
 IncludePrompt                    Return text input by the user as well as any instances of the prompt character.  
 IncludePromptAndLiterals    Return text input by the user as well as any literal characters defined in the mask and any instances of the prompt character.  

Microsoft's default value is to IncludeLiterals.

Fernando
0
 
Ken_LyonCommented:
Despite being old and abandoned, this question was very helpful for me. Thanks for keeping these in the knowledge base!

I still want to use the "IncludeLiterals" option in the final output, so what I do is to temporarily change it to "ExcludePromptAndLiterals" to retrieve the text and then change it back:

// Store a reference to the initial value for TextMaskFormat.
var previousTextMaskFormat = maskedTextBox.TextMaskFormat;

// Change to MaskFormat.ExcludePromptAndLiterals.
maskedTextBox.TextMaskFormat = MaskFormat.ExcludePromptAndLiterals;

// Retrieve the text without prompts or literals. (This will help us see if the user's entered anything or not.)
var text = maskedTextBox.Text;

// Change TextMaskFormat back to the previous value.
maskedTextBox.TextMaskFormat = previousTextMaskFormat;

if (!String.IsNullOrEmpty(text) && !maskedTextBox.MaskCompleted)
{
    // Input mask was only partially filled.
    // TODO: Add code to handle incomplete text here...
}

Open in new window


This way I get the best of both worlds. I can tell if the user's text is empty, and I can also retrieve a nicely formatted result when it's correct.

NOTE: I use MaskCompleted, not MaskFilled. The former only needs the "required" parts of the mask to be populated, whereas the latter requires the "optional" parts too.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 4
  • 4
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now