Solved

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

Posted on 2006-11-23
14
6,983 Views
Last Modified: 2011-03-17
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
Comment
Question by:Kinger247
  • 4
  • 4
  • 2
  • +2
14 Comments
 
LVL 13

Expert Comment

by:newyuppie
ID: 18003232
you could check for the MaskedTextbox1.MaskCompleted boolean that indicates if the mask is complete or not...
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 18003280
Hi Kinger247;

Another way would be

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

Fernando
0
 
LVL 34

Expert Comment

by:Sancler
ID: 18003282
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
 
LVL 13

Expert Comment

by:newyuppie
ID: 18003372
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
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 18003373
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
 
LVL 10

Author Comment

by:Kinger247
ID: 18003390
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 34

Expert Comment

by:Sancler
ID: 18003508
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
 
LVL 34

Expert Comment

by:Sancler
ID: 18003529
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
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 18003571
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
 
LVL 34

Assisted Solution

by:Sancler
Sancler earned 250 total points
ID: 18003833
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
 
LVL 10

Author Comment

by:Kinger247
ID: 18003903
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
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 250 total points
ID: 18003927
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
 

Expert Comment

by:Ken_Lyon
ID: 35158483
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Custom auto number 6 51
VB.NET HttpWebRequest 12 34
Spacing between controls 4 17
Open a word document 23 15
This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now