我添加了两个textbox控件,对txtbox1的keypress事件做了一下处理,我希望texbox2在使用时也能触发就写了下面的代,但没有作用,大神指导~~
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
TextBox textboc = sender as TextBox;//
if (e.KeyChar < '0' || e.KeyChar > '9')
{
e.Handled = true;
}
if (e.KeyChar == 46)
{
if (textboc.Text.IndexOf(".") == -1)
{
if (textboc.SelectionStart > 0)
{
e.Handled = false;
}
}
}
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
TextBox textboc = sender as TextBox;//
if (e.KeyChar < '0' || e.KeyChar > '9')
{
e.Handled = true;
}
if (e.KeyChar == 46)
{
if (textboc.Text.IndexOf(".") == -1)
{
if (textboc.SelectionStart > 0)
{
e.Handled = false;
}
}
}
}
这句的意思就是当textBox2发生KeyPress 这个事件的时候,去找this.textBox1_KeyPress这个方法来处理,实际上所有的事件处理都是这种形式的,你所要做的就是让两个textBox在发生KeyPress事件的时候去找同一个事件处理程序,也就是this.textBox1_KeyPress,之后,在this.textBox1_KeyPress中才会有TextBox textboc = sender as TextBox用来区分是哪一个TextBox