Question

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.

Was it helpful?

Solution

You can simply use:

maskedTextBox1.MaskCompleted

Or

maskedTextBox1.MaskFull

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

OTHER TIPS

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

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());

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 == "  /  /")
        {
            ...;
        }

Did you try trim.

if(string.IsNullOrEmpty(maskedTextBox.Text.Trim())

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

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 "_".

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.

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)
{
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top