« | August 2025 | » | 日 | 一 | 二 | 三 | 四 | 五 | 六 | | | | | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | | | | | | | |
| 公告 |
暂无公告... |
Blog信息 |
blog名称: 日志总数:18 评论数量:10 留言数量:1 访问次数:137828 建立时间:2005年11月22日 |

| |
[编程]如何让TextBox只显示数字 电脑与网络
flymcx 发表于 2006/6/16 10:43:35 |
TextBox本身不支持对输入的数字控制,因为我们只有自己写。
我就这里写了一个。
public class NT_TextBox : TextBox { public enum NumericType { None, Int, Float } private NumericType _NumericOpration; public NumericType NumericOpration { get { return _NumericOpration; } set { _NumericOpration = value; } }
protected override void WndProc(ref Message m) { if(NumericType.None==NumericOpration) { base.WndProc(ref m); return; } int WM_CHAR = 0x0102; if(m.Msg == WM_CHAR) { char e=(char)m.WParam; if(DealInputChar(e)) base.WndProc(ref m); } else { base.WndProc (ref m); } }
private bool DealInputChar(char e) { int code=(int)e; int min=(char)'0'; int max=(char)'9'; if(code==(int)Keys.Back) return true; if(code==(int)Keys.Left) return true; if(code==(int)Keys.Right) return true; if(code==(int)'.') { if(Text.Trim().IndexOf(".")==-1) { if(NumericOpration==NumericType.Float) return true; else return false; } else return false; } if(code==(int)'-') { if(Text.Trim().IndexOf("-")==-1) { if(SelectionStart==0) return true; else return false; } else return false; } if(code<min||code>max) return false; return true; }
} |
|
|