C# 如何从用户输入中检查 MaskedTextBox 是否为空?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17757612/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check if a MaskedTextBox is empty from a user input?
提问by Muhammed Salah
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:
我正在使用MaskedTextBox
具有以下短日期的 , Mask
:“00/00/0000”。
我的问题是我想知道控件何时为空:
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
.
它不起作用,当maskedTextBox1
看起来为空时(我确定它是),该if
语句不会检测到它是null
或Empty
。
采纳答案by Shaharyar
You can simply use:
您可以简单地使用:
maskedTextBox1.MaskCompleted
Or
或者
maskedTextBox1.MaskFull
properties to check if user has entered the complete mask input or not.
属性来检查用户是否输入了完整的掩码输入。
回答by chris
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 语句实现什么逻辑?众所周知,您是在说:如果文本框为空,请将 datagridview2 + 的源设置为 ViewOrder 数据。我不确定您要做什么,但我认为如果您有约会,您希望加载信息。要解决此问题,您只需添加 !在使 if 语句的 if 语句中,如果文本框中有文本,则运行代码。
if( !(string.IsNullOrEmpty(maskedTextBox2.Text)))
回答by Nilesh
Did you try trim.
你试过修剪吗?
if(string.IsNullOrEmpty(maskedTextBox.Text.Trim())
回答by user3219570
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
回答by Jose Quiroa
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.ToDisplayStringso I use the MaskedTextbox with:
我发现了MaskedTextProvider.ToDisplayString属性, 因此我将 MaskedTextbox 用于:
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:
这样,如果用户没有在控件中引入文本,Text 属性就会为空:
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());
回答by Amir H Rahimi
In case of Telerik masked textbox which does not have MaskCompleted or MaskFull, a tricky solution would be this:
对于没有 MaskCompleted 或 MaskFull 的 Telerik 屏蔽文本框,一个棘手的解决方案是:
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 "_"
.
如果文本框已满,则它不包含"_"
.
回答by StuKay
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:
如果您设置属性 maskedTextBox.TextMaskFormat = MaskFormat.ExcludePromptAndLiterals,则 TypeValidationCompleted 事件验证将不起作用。要测试短日期 maskedtextbox 是否为空,您可以使用:
if (maskedTextBox1.Text == " / /")
{
...;
}