?
Solved

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

Posted on 2006-11-23
14
Medium Priority
?
7,804 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 63

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 63

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
 
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 63

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 1000 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 63

Accepted Solution

by:
Fernando Soto earned 1000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses
Course of the Month13 days, 6 hours left to enroll

777 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