如何在TextBox1.Text中禁用空格键? [英] How to Disable spacebar in TextBox1.Text?

查看:79
本文介绍了如何在TextBox1.Text中禁用空格键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我回来了。



首先,我要感谢你给我的正规表达,它很高兴地解决了我的问题。:笨蛋:



现在,在对数组进行排序之前,我从TextBox1.Text中获取值(例如,323,1,65,7,3,999,0,)。 />


我如何禁用此控件中的空格来限制用户提供不需要的空格?我只需要数字和逗号。:confused:



谢谢

Hi guys, am back.

First i need to thank you for the "regula expression" given to me ealier, it happily solved my problem.:thumbsup:

NOW, before sorting an array I get values from TextBox1.Text like(eg. 323,1,65,7,3,999,0,).

HOW can i disable the use of spaces in this control to restrict the user from giving unwanted spaces? I need only numbers and commas.:confused:

Thanks

推荐答案

我会看看使用文本框上的KeyPress事件,如果键是空格键,则设置e.Handled = true(e是事件处理程序的KeyPressEventArgs)。您的代码可能如下所示:
I would look at using the KeyPress event on the textbox and set e.Handled=true if the key was a spacebar (e is the KeyPressEventArgs for the event handler). Your code might look something like this:
private void TextBox1_KeyPress(object sender, KeyPressEventArgs e)
{
  e.Handled = e.KeyChar == ' ';
}

通过将Handled设置为true,此字符不会反映在文本框中。顺便说一句 - 请为你的变量命名,而不是VS给你的默认值。 TextBox1现在对你来说似乎不会太糟糕,但是你会记得它在6个月内的用途吗?

By setting Handled to true, this character won't be reflected in the textbox. BTW - please name your variables better than the default values that VS gives you. TextBox1 won't seem too bad to you now, but will you remember what it's for in 6 months time?


要添加上面的答案,KeyPress事件处理程序将不起作用,除非您将KeyPreview设置为true。 KeyPreview属性位于Windows窗体上(单击表单,查看右侧的属性框,找到KeyPreview,并将其设置为True)。



或者您可以手动将以下内容添加到表单构造函数中:

To add onto the answer above, the KeyPress event handler won't work unless you set KeyPreview to true. The KeyPreview property is on the Windows Form (click your form, look to the property box to the right, find KeyPreview, and set it to True).

Or you can manually add the following to the form constructor:
this.KeyPreview = true;


VB不使用双等号来测试相等性。它只使用一个等号。尝试:

VB does not use a double equal sign to test for equality. It only uses a single equal sign. Try:
e.Handled = (e.KeyChar = " ")



此外,它不使用分号来结束行。


Also, it doesn't use semicolons to end lines.


这篇关于如何在TextBox1.Text中禁用空格键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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