C#App中的蒙面文本框问题 [英] Masked Textbox Problem in C# App

查看:110
本文介绍了C#App中的蒙面文本框问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用Visual Studio 2012 / C#编写的Windows窗体应用程序时出现问题。我有一个蒙面文本框,格式化为邮政编码,上面有一个标签。如果用户忘记在单击ENTER按钮之前在已屏蔽的文本框中键入邮政编码,则会出现标签,告诉用户请输入邮政编码。问题是标签不应该出现。

这是我到目前为止编写的代码:

I'm having a problem with a Windows Forms app I'm writing in Visual Studio 2012/C#. I have a masked textbox formatted for ZIP codes and a label above it. If the user forgets to type a ZIP code into the masked textbox before clicking an ENTER button, the label appears, telling the user to "Please enter a ZIP code." The problem is that the label doesn't appear when it should.
Here's the code I've written so far:

if (txtZIPCode.Text == "")
{
    ErrorCount += 1;
    lblEnterZIPCode.Visible = true;
}
else
{
    ZIPCode = txtZIPCode.Text.ToString();
    lblEnterZIPCode.Visible = false;
}

推荐答案

如果txtZIPCode是控制掩码文本框

u可以使用以下内容:

if txtZIPCode is control masktextbox
u can use following :
if (!txtZIPCode.MaskFull)
{
ErrorCount += 1;
lblEnterZIPCode.Visible = true;
}



...


...


如果查看掩码和返回的Text属性,您会发现它几乎从不空洞。如果您使用了控件提供的标准邮政编码掩码,那么它包含一个 - 符号,而空控件的Text属性是
If you look at your mask, and the returned Text property, you will find it is almost never empty. If you used the standard "Zip code" mask provided wit the control, then it includes a "-" sign, and the Text property for an "empty" control is
"     -"

- 未通过测试。


尝试更改if:



try changing the if:

if (string.IsNullorEmpty(txtZIPCode.Text.Trim().Replace("-",""))) {
    ErrorCount += 1;
    lblEnterZIPCode.Visible = true;
} else {
    ZIPCode = txtZIPCode.Text.ToString();
    lblEnterZIPCode.Visible = false;
}


这篇关于C#App中的蒙面文本框问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆