WinForm的TextBox限制只能输入数字并且屏蔽默认右键菜单

基于Window消息实现

class TextBoxExt:TextBox
{
private const int WM_RBUTTONDOWN = 0x0204;
private const int WM_CHAR = 0x0102;
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case WM_RBUTTONDOWN:
return;//屏蔽默认右键菜单
break;
case WM_CHAR:
int n = (int)m.WParam;
if (n==)//允许输入退格键
{
base.WndProc(ref m);
}
if (n>= && n<=)//允许输入0-9
{
base.WndProc(ref m); }
else
{
return;
}
break;
default:
base.WndProc(ref m);
break;
}
}
}
上一篇:js去掉浏览器右键点击默认事件(+vue项目开启右键行为)


下一篇:Leetcode: Ones and Zeroes