Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a MaskedTextBox is empty from a user input?

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.

like image 820
Muhammed Salah Avatar asked Jul 20 '13 01:07

Muhammed Salah


3 Answers

You can simply use:

maskedTextBox1.MaskCompleted

Or

maskedTextBox1.MaskFull

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

like image 137
Shaharyar Avatar answered Oct 17 '22 04:10

Shaharyar


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
like image 21
user3219570 Avatar answered Oct 17 '22 05:10

user3219570


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());
like image 3
Jose Quiroa Avatar answered Oct 17 '22 06:10

Jose Quiroa