[英]Limit multiple textbox to number
I found the following solution to limit textbox to numbers.我找到了以下将文本框限制为数字的解决方案。 I have 20 textBoxes in my GUI is there a cleaner way than making 20 of these functions?我的 GUI 中有 20 个文本框,有没有比制作 20 个这些功能更简洁的方法?
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = !char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar);
}
My suggestion is to replace the standard TextBox Controls that need this feature, with a Custom Control that can also filter the User input when text is pasted in the Control or when the Control's Text
Property is set in the Designer, using the PropertyGrid.我的建议是用一个自定义控件替换需要此功能的标准文本框控件,该自定义控件还可以在控件中粘贴文本或在设计器中使用 PropertyGrid 设置控件的Text
属性时过滤用户输入。
If you only handle the KeyPress
event, you cannot prevent a bad paste.如果只处理KeyPress
事件,则无法防止错误粘贴。
I think it's also better to filter what is set in the Text
Property, to avoid a misunderstanding .我认为最好过滤Text
属性中设置的内容,以避免误解。
Test this simple Custom TextBox Control: it handles direct User input and text pasted at run-time.测试这个简单的自定义文本框控件:它处理直接用户输入和在运行时粘贴的文本。 Setting the UserPaste
Property to Disallow
, pastes are ignored, while setting it to NumbersOnly
(default) allows just numbers: if mixed chars are pasted in the Control, only the numbers are preserved.将UserPaste
属性设置为Disallow
,将忽略粘贴,而将其设置为NumbersOnly
(默认)仅允许数字:如果在控件中粘贴混合字符,则仅保留数字。
To also allow the input of comma and dot, change the Regex in [^0-9,.\b]
.要还允许输入逗号和点,请更改[^0-9,.\b]
中的正则表达式。
The Text property set in the Designer is also filtered. Designer 中设置的 Text 属性也会被过滤。
To replace the existing TextBox Controls with the this Custom Control, you can use the Find/Replace function (usually activated with CTRL+H ) in Visual Studio:要使用此自定义控件替换现有的 TextBox 控件,您可以在 Visual Studio 中使用查找/替换 function(通常使用CTRL+H激活):
using System.ComponentModel;
using System.Text.RegularExpressions;
using System.Windows.Forms;
[ToolboxItem(true)]
[DesignerCategory("Code")]
public class TextBoxNumbers : TextBox
{
private Regex regex = new Regex(@"[^0-9\b]", RegexOptions.Compiled);
public TextBoxNumbers() { }
public override string Text {
get => base.Text;
set { if (!base.Text.Equals(value)) base.Text = regex.Replace(value, ""); }
}
public enum PasteAction {
NumbersOnly,
Disallow
}
[DefaultValue(PasteAction.NumbersOnly)]
public PasteAction UserPaste { get; set; }
protected override void OnKeyPress(KeyPressEventArgs e)
{
if (regex.IsMatch(e.KeyChar.ToString())) {
e.Handled = true;
}
base.OnKeyPress(e);
}
protected override void WndProc(ref Message m)
{
switch (m.Msg) {
case NativeMethods.WM_PASTE:
switch (UserPaste) {
case PasteAction.Disallow:
return;
case PasteAction.NumbersOnly:
string text = Clipboard.GetText(TextDataFormat.UnicodeText);
text = regex.Replace(text, "");
NativeMethods.SendMessage(this.Handle, NativeMethods.EM_REPLACESEL, 1, text);
return;
}
break;
}
base.WndProc(ref m);
}
}
NativeMethods
class: NativeMethods
class:
using System.Runtime.InteropServices;
private class NativeMethods
{
internal const int WM_PASTE = 0x0302;
internal const int EM_REPLACESEL = 0xC2;
[DllImport("User32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
internal static extern int SendMessage(IntPtr hWnd, uint uMsg, int wParam, string lParam);
}
My simple and go-to solution would be to wirte the methode once, after that go to the desginer and select every of your texboxes that need it, click on the event-button in the propperties, scroll to the KeyPress Event and click on it once, now a dropdown-arrow should apear, select your methode there - done.我的简单而首选的解决方案是编写一次方法,然后将 go 写入设计器和 select 每个需要它的 texbox,单击属性中的事件按钮,滚动到 KeyPress 事件并单击它曾经,现在应该出现一个下拉箭头,select 你的方法在那里 - 完成。
private void textBox_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = !char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.