我不想在c#中的文本框中允许所有空格 [英] I dont want to allow all space in textbox in c#

查看:216
本文介绍了我不想在c#中的文本框中允许所有空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hii

我不想在c#windows应用程序中允许文本框中的所有空格意味着如果我在文本框中提供所有空格,表单不应该提交



谢谢

问候

Mahesh

hii
I dont want to allow all space in textbox in c# windows applications means if i give all spaces in textbox the form should not submit

thank you
Regards
Mahesh

推荐答案

hi,

这里是您的小代码,在验证textBox的事件过程下编写以下代码。


here is the small code for you, write the following code under validating event procedure of the textBox.
private void mytextBox_Validating(object sender, CancelEventArgs e)
        {
            TextBox tb = sender as TextBox;
            if (string.IsNullOrWhiteSpace(tb.Text) == true)
            {
                MessageBox.Show("Your error message for users");
                e.Cancel = true;
                return;
            }
        }


处理TextBox.TextChanged事件。

其中:

Handle the TextBox.TextChanged Event.
In it:
private void myTextBox_TextChanged(object sender, EventArgs e)
    {
    TextBox tb = sender as TextBox;
    if (tb != null)
        {
        myOKButton.Enabled = !string.IsNullOrWhiteSpace(tb.Text);
        }
    }


对于 .Net Framework 3.0及更低版本,您可以使用 String.IsNullOrEmpty(string)函数返回 true 如果字符串参数是 Null 或者是空的。但是这个函数不检查解析的字符串是否只包含空格(WhiteSpace),所以你还必须调用string的 Trim()函数。你可以创建一个验证在汇款之前检查你的控件的功能...



For .Net Framework 3.0 and lower, you can use String.IsNullOrEmpty(string) function which returns true if the string argument is Null or is empty. But this function does not check if the string parsed contains only spaces(WhiteSpace), so you you also have to call the Trim() function of string.You can create a validate function to check your controls before remitting it...

public bool ValidateControls()
        {
            if (String.IsNullOrEmpty(myTextBox.Text))
            {
                //Tell the user that the text provided is unacceptable.
                MessageBox.Show("The content of the Textbox is not valid.");
                //Validation was unsuccessful.
                return false;
            }
            //Validation was successful.
            return true;
        }





但是,如果您使用的是 .Net Framework 3.5及更高版本,那么应该使用 String.IsNullOrWhiteSpace(string)函数,如果字符串参数为空,则返回 true Null 或仅包含空格。所以,您不必再次修剪字符串,上面的代码可以重写为





However, if you're using .Net Framework 3.5 and higher, you should use String.IsNullOrWhiteSpace(string) function which returns true if the string argument is empty, Null or contains only spaces. So, you would not have to Trim the string again, and the above code could be rewritten as

public bool ValidateControls()
        {
            if (String.IsNullOrWhiteSpace(myTextBox.Text))
            {
                //Tell the user that the text provided is unacceptable.
                MessageBox.Show("The content of the Textbox is not valid.");
                //Validation was unsuccessful.
                return false;
            }
            //Validation was successful.
            return true;
        }


这篇关于我不想在c#中的文本框中允许所有空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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