6

I'm using a MaskedTextBox, with the following short date Mask: "00/00/0000".
My problem is that I wanna know when the control is empty:

if (string.IsNullOrEmpty(maskedTextBox1.Text))
{
    DataTable dt = function.ViewOrders(Functions.GetEid);
    dataGridView2.DataSource = dt;
}

It's not working, when maskedTextBox1 looks empty (and I'm sure it is), the if statement doesn't detect that it is null or Empty.

Gary Barrett
  • 1,764
  • 5
  • 21
  • 33
Muhammed Salah
  • 271
  • 3
  • 4
  • 9

10 Answers10

25

You can simply use:

maskedTextBox1.MaskCompleted

Or

maskedTextBox1.MaskFull

properties to check if user has entered the complete mask input or not.

Shaharyar
  • 12,254
  • 4
  • 46
  • 66
  • What if I want to check if the user entered anything, even if one character? – Si8 Jun 18 '14 at 17:57
  • I couldn't get your question. You can get the text of textbox by `maskedTextBox1.Text`, so you will get whatever user has written in it. – Shaharyar Jun 18 '14 at 19:34
  • 2
    this does not actually answer the question. Which is "how to know if it the control is EMPTY".The best way to do this is to remove the mask like the answer from @user3219570 – merce_00 Aug 26 '16 at 08:36
3

I know this is old but I would first remove the mask and then check the text like a normal textbox.

maskedTextBox.TextMaskFormat = MaskFormat.ExcludePromptAndLiterals;

//...Perform normal textbox validation
user3219570
  • 197
  • 1
  • 2
  • 10
3

I just faced this problem. I Needed the Masked Value, but also needed send empty string if the user didn't introduced any data in one single step.

I discovered the property MaskedTextProvider.ToDisplayString so I use the MaskedTextbox with:

maskedTextBox.TextMaskFormat = MaskFormat.ExcludePromptAndLiterals;

BUT I always read the text from:

maskedTextBox.MaskedTextProvider.ToDisplayString() 

This way, if the user has not introduced text in the control Text property will be empty:

maskedTextBox.Text == string.Empty

And when you detect the string is not empty you can use the full text including literals for example:

DoSomething((maskedTextBox.Text == string.Empty) ? maskedTextBox.Text: maskedTextBox.MaskedTextProvider.ToDisplayString());

or

DoSomething((maskedTextBox.Text == string.Empty) ? string.Empty: maskedTextBox.MaskedTextProvider.ToDisplayString());
2

If you set the property maskedTextBox.TextMaskFormat = MaskFormat.ExcludePromptAndLiterals then the TypeValidationCompleted event validation will not work. To test if the short date maskedtextbox is empty you could just use:

        if (maskedTextBox1.Text == "  /  /")
        {
            ...;
        }
StuKay
  • 307
  • 1
  • 7
1

Did you try trim.

if(string.IsNullOrEmpty(maskedTextBox.Text.Trim())
Nilesh
  • 2,583
  • 5
  • 21
  • 34
0

What logic are you trying to accomplish with the if statement? As it is right know, you are saying: If the textbox is empty, set source of datagridview2 + to ViewOrder data. I'm not sure what your trying to do but I think you want the info to load if you have a date. to fix this all you have to do is add ! in the if statement which would make the if statement mean, if there is text in textbox then run code.

if( !(string.IsNullOrEmpty(maskedTextBox2.Text)))
chris
  • 78
  • 1
  • 15
  • i wanna make if statement to do if the masked text box is empty do something else do another something and by the way the masked text box have a mask = "00/00/0000" (Short Date) – Muhammed Salah Jul 20 '13 at 01:28
  • @MuhammedSalah What do you mean by `Empty`? Do you mean by `Empty` that `textbox` should have only `mask` but the user input `value`? – Shaharyar Jul 20 '13 at 01:37
  • @Shaharyar Yup Exactly that 's what i need :) – Muhammed Salah Jul 20 '13 at 01:43
  • I dont have my computer infront of me to try it but look into MaskComplete and MaskFull properties for MaskedTextBox. MaskCompleted checks only required input elements. To determine whether all required and optional input elements have been filled in, use the MaskFull property instead. – chris Jul 20 '13 at 02:03
0

In case of Telerik masked textbox which does not have MaskCompleted or MaskFull, a tricky solution would be this:

the mask always contain a charachter like this: "_" we check masked text box by this:

if (textbox1.Text.Contains("_"))
        {
            MessageBox.Show("Please enter the correct numbers!","Error",MessageBoxButtons.OK,MessageBoxIcon.Stop);
            return;
        }

if the text box is full, then it does not contain "_".

0

I believe the MaskedTextBox, (MTB), using the mask “00/00/0000” is an incorrect string to use for testing its emptiness. This is because the MTB is not like a normal textbox, and the short date mask must be used to determine its string value. Let’s assume you have a MTB name mskDateOfBirth on your form. In order to test its emptiness, a statement like the following is needed

if (mskDateOfBirth.MaskedTextProvider.ToDisplayString() == "__/__/____")
{
    // Do something when true
}   
else
{
    // Do something when false          
}

I have tested this out using Visual Studio 2019 and it works fine. Hope this is helpful.

Philippe
  • 1,733
  • 1
  • 14
  • 28
  • For some reason the Stackflow word process, is condensing the undercores. Mask should look like "__/__/____" – dobriendob1 Jan 02 '21 at 22:25
  • The Answer field accepts markup; use the tag or the formatting buttons above the field to format code. – Dour High Arch Jan 02 '21 at 23:37
  • By the way, this can fail if the user has a non-standard system-level date format. For example, my machine is set to a `yyyy-MM-dd` format, which has a mask of `____-__-__`, which would fail the conditional check (even though it's empty). – Jeff B Sep 28 '21 at 18:10
-1

If the empty value is " / /", declare a constant for it:

const string EmptyDateInput = "  /  /";

And then later you can repeatedly use it to compare:

if (maskedTextBox1.Text == EmptyDateInput)
{
}
Luo Jiong Hui
  • 5,527
  • 2
  • 23
  • 18
-1

I test this concept and was success in in the following syntax

if( maskedtextbox_name.MaskkedTextProvider.ToDisplayString() == "__-__-____")
{
  // Your function;
}
Sane
  • 2,334
  • 2
  • 17
  • 20