注意: GotFocus事件和LostFocus事件在设计界面时没有,需要手动写 this.numericUpDown1.GotFocus += new System.EventHandler(this.numericUpDown1_GotFocus); this.numericUpDown1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.numericUpDown1_KeyDown); this.numericUpDown2.GotFocus += new System.EventHandler(this.numericUpDown1_GotFocus); this.numericUpDown2.KeyDown += new System.Windows.Forms.KeyEventHandler(this.numericUpDown1_KeyDown);...
this.numericUpDown1.LostFocus += new System.EventHandler(this.numericUpDown1_LostFocus); this.numericUpDown2.LostFocus += new System.EventHandler(this.numericUpDown1_LostFocus); ...
private void numericUpDown1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
SendKeys.Send("{TAB}");
}
}
如果要高亮显示输入位置,你可以把每个控件的GotFocus和LostFocus事件分别指定到下面两个事件方法
void numericUpDown1_GotFocus(object sender, System.EventArgs e)
{
(sender as Control).BackColor = System.Drawing.Color.Yellow;
(sender as Control).ForeColor = System.Drawing.Color.Red;
} void numericUpDown1_LostFocus(object sender, System.EventArgs e)
{
(sender as Control).BackColor = System.Drawing.Color.White;
(sender as Control).ForeColor = System.Drawing.Color.Black;
}
GotFocus事件和LostFocus事件在设计界面时没有,需要手动写
this.numericUpDown1.GotFocus += new System.EventHandler(this.numericUpDown1_GotFocus);
this.numericUpDown1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.numericUpDown1_KeyDown); this.numericUpDown2.GotFocus += new System.EventHandler(this.numericUpDown1_GotFocus);
this.numericUpDown2.KeyDown += new System.Windows.Forms.KeyEventHandler(this.numericUpDown1_KeyDown);...
this.numericUpDown2.LostFocus += new System.EventHandler(this.numericUpDown1_LostFocus);
...
{
if(e.KeyCode == Keys.Enter)
{
this.button1_Click(sender,e);
}
}
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
SendKeys.Send("{TAB}");
//高亮可以通过背景色
textBox2.BackColor = System.Drawing.Color.ForestGreen;
}
} private void textBox2_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
textBox2.BackColor = System.Drawing.Color.FloralWhite;
SendKeys.Send("{TAB}");
//高亮可以通过背景色
this.button1_Click(this.button1, null);
}
} private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("测试成功");
}
textBox1.SelectionStart = 0;
textBox1.SelectionLength = textBox1.Text.Length - 1;
但是NumericUpDown好像没有SelectionStart属性,
像上图,按下Enter键,下一个NumericUpDown会编程图片中地址栏那种,文本自动全部选中的状态,接下来输入就自动清空了,(书上说这就是高亮选中,),不知如何是实现,