限制用户在文本框中输入空格 [英] Restrict user from entering space in a textbox
问题描述
嗨!全部,
我有一个应用程序,我必须在文本框中输入一些字符,如何验证它,以使文本框在文本框的开头不接受空格.
Hi! all,
I have an application where i have to enter some characters in a textbox, how can i validate it so that the textbox doesn''t accept a space in the beginning of the textbox.
推荐答案
处理此文本框的KeyPress事件,并放置单个条件,例如.
Hi,
Handle the KeyPress event for this textBox and put a single condition like.
if (textBox1.Text.Length==0 && e.KeyChar==' ')
{
e.Handled = true;
}
else
{
e.Handled = false;
}
这里的textBox1是您的文本框.此代码将阻止用户在TextBox的开头输入空格.
现在,如果您想验证数据
然后,您需要检查TextBox的第一个字符是否为空格.
您可以找到这样的第一个字符
Here textBox1 is your textbox. This code will prevent user to enter space at begining of TextBox.
Now, If you want to validate the data
Then You need to check first character of TextBox whether it''s a space or not.
You can find first character like this
char FChar=textBox1.Text.Substring(0,1);
问候
AR
Regards
AR
if (textBox1.Text.Length==0 && e.KeyChar==' ')
{
e.Handled = true;
}
else
{
e.Handled = false;
}
处理此文本框的KeyPress事件,并放置单个条件,例如.
Hi,
Handle the KeyPress event for this textBox and put a single condition like.
if (textBox1.Text.Length==0 && e.KeyChar==' ')
{
e.Handled = true;
}
else
{
e.Handled = false;
}
这里的textBox1是您的文本框.此代码将阻止用户在TextBox的开头输入空格.
现在,如果您想验证数据
然后,您需要检查TextBox的第一个字符是否为空格.
您可以找到这样的第一个字符
Here textBox1 is your textbox. This code will prevent user to enter space at begining of TextBox.
Now, If you want to validate the data
Then You need to check first character of TextBox whether it''s a space or not.
You can find first character like this
char FChar=textBox1.Text.Substring(0,1);
这篇关于限制用户在文本框中输入空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!