各位来宾:
小弟在此先感谢了.
问题://只能输入小数跟小数点
private void OnKeyPress(object sender, KeyPressEventArgs e)
{
int kc = Convert.ToInt32(e.KeyChar);
if ((kc < 48 || kc > 57) && kc != 8)
e.Handled = true;
}在多个页面都有这样的需求,如果多个页面都写,这样应该是很不好的习惯的,我想把他抽象到公共类,但在公共类无法获取到e.KeyChar【按键值】,我在公共类应该怎么处理才能获取到e.KeyChar,或者说我怎么把e.KeyChar传过去?
foreach(Control control in this.Controls)
{
if(control is TextBox)
......
}
这个是遍历控件的,同样的也是我多个页面都需要,我抽象成公共类的话 怎么当前的容器传给公共类。
小弟在此先感谢了.
问题://只能输入小数跟小数点
private void OnKeyPress(object sender, KeyPressEventArgs e)
{
int kc = Convert.ToInt32(e.KeyChar);
if ((kc < 48 || kc > 57) && kc != 8)
e.Handled = true;
}在多个页面都有这样的需求,如果多个页面都写,这样应该是很不好的习惯的,我想把他抽象到公共类,但在公共类无法获取到e.KeyChar【按键值】,我在公共类应该怎么处理才能获取到e.KeyChar,或者说我怎么把e.KeyChar传过去?
foreach(Control control in this.Controls)
{
if(control is TextBox)
......
}
这个是遍历控件的,同样的也是我多个页面都需要,我抽象成公共类的话 怎么当前的容器传给公共类。
{
...
}事件里的 e.Handled = true; 还是要写的,textbox 和这个差不多
既然用foreach判断那为什么还要获取e.KeyChar?
就像完成时和进行搞混了一样
foreach判断那为什么还要获取e.KeyChar,这个你哪看来的
假设有以下方法HandleKeyPressForAllTextBox定义在公共里里 public void HandleKeyPressForAllTextBox(Control parentControl)
{
foreach (Control ctrl in parentControl.Controls)
{
TextBox tb = ctrl as TextBox;
if (tb != null)
{
tb.KeyPress += (s, e) =>
{
int kc = Convert.ToInt32(e.KeyChar);
if ((kc < 48 || kc > 57) && kc != 8)
e.Handled = true;
};
}
}
}
HandleKeyPressForAllTextBox(Control parentControl)
改成
HandleKeyPressForAllTextBox(Page parentControl)在Page_Load事件里 if (!Page.IsPostBack)
{
HandleKeyPressForAllTextBox(this);
}
{
protected override void OnKeyPress(KeyPressEventArgs e)
{
int kc = Convert.ToInt32(e.KeyChar);
if ((kc < 48 || kc > 57) && kc != 8)
e.Handled = true;
base.OnKeyPress(e);
}
}