使用TryParseExact与WindowsForms文本框 [英] Using TryParseExact with WindowsForms Textbox

查看:116
本文介绍了使用TryParseExact与WindowsForms文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在验证一个文本框,所以只能使用字母(AZ),可进入,配合使用TryParseExact的。

我有一个code到检查时间,虽然可能有人演示了如何这可以只用字母做的。

我的code是如下:

 私人无效textBox2_Validating(对象发件人,CancelEventArgs E)
{
    日期时间dateEntered;

    如果(DateTime.TryParseExact(textBox2.Text,HH:MM,System.Globalization.CultureInfo.CurrentCulture,System.Globalization.DateTimeStyles.None,出dateEntered))
    {

    }
    其他
    {
        的MessageBox.show(您需要输入有效的24小时时间);
    }
}
 

解决方案

这将检查字符串中的所有字符取值是一个字母:

 布尔结果= s.All(CH => char.IsLetter(CH));
 

另请参阅: Char.IsLetter法(MSDN)

如果你想只接受ASCII字母(即az和AZ):

 布尔结果= s.All(CH =>(CH> ='A'和;&安培; CH< ='Z')||(CH> = 'A'和;&安培; CH< ='Z'));
 

I am currently trying to validate a textbox, so only letters (a-Z) can be entered, with the use of TryParseExact.

I have a code to check a time, although could someone demonstrate how this could be done with letters only.

My code is as follows:

private void textBox2_Validating(object sender, CancelEventArgs e)
{
    DateTime dateEntered;

    if (DateTime.TryParseExact(textBox2.Text, "HH:mm", System.Globalization.CultureInfo.CurrentCulture, System.Globalization.DateTimeStyles.None, out dateEntered))
    {

    }
    else
    {
        MessageBox.Show("You need to enter valid 24 hour time");
    }
}

解决方案

This checks if all characters in a string s are a letter:

bool result = s.All(ch => char.IsLetter(ch));

See also: Char.IsLetter Method (MSDN)

If you want to accept only ASCII letters (i.e. a-z and A-Z):

bool result = s.All(ch => (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'));

这篇关于使用TryParseExact与WindowsForms文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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