我用的是C#,假设我有一个组合框,里面有几个NumericUpDown,还有一个Button,我想实现这种效果:每次输入完毕按下Enter键,可自动跳到下一个NumericUpDown输入(要求高亮选中输入位置),到最后一个NumericUpDown时,能够在输完后,按下Enter键,直接触发Button的Click事件。请帮助我解决,我只能实现自动跳转,但是不能高亮选中,最后只能跳到button上,但是不能触发事件,谢谢了! 

解决方案 »

  1.   

    触发事件很简单,直接btton1_click(this, new EventArgs())就可以了
      

  2.   

    你可以依次设置TagIndex,然后在Key_up或其它Key事件中用Sendkeys.Send( {"Tab"})来处理。或者每个控件分别写跳转。
      

  3.   

    先按顺序设置每个控件的TabOrder属性,然后将每个控件的Key_Down事件指定到下面这个事件方法,就可以实现按Enter跳到下一个控件
     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;
            }
      

  4.   

    注意:
    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);...
      

  5.   

     this.numericUpDown1.LostFocus += new System.EventHandler(this.numericUpDown1_LostFocus);
     this.numericUpDown2.LostFocus += new System.EventHandler(this.numericUpDown1_LostFocus);
    ...
      

  6.   

    private   void   textBox1_KeyDown(object   sender,   System.Windows.Forms.KeyEventArgs   e)   
      {   
      if(e.KeyCode   ==   Keys.Enter)   
      {   
      this.button1_Click(sender,e);   
      }   
      }
      

  7.   

    //添加textBox1  textBox2  button1。把textBox换成你的numericUpDown....
            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("测试成功");
            }
      

  8.   

    没太看明白,你试下这两句是不是你要的意思
    textBox1.SelectionStart = 0;
    textBox1.SelectionLength = textBox1.Text.Length - 1;
      

  9.   


    但是NumericUpDown好像没有SelectionStart属性,
    像上图,按下Enter键,下一个NumericUpDown会编程图片中地址栏那种,文本自动全部选中的状态,接下来输入就自动清空了,(书上说这就是高亮选中,),不知如何是实现,