我写了一个自定义控件MyButton,然后拖入Form窗体中,再拖入两个Button1,2.Tab键顺序是 Button1(0),Button(1),
MyButton(2),当我移动方向键时。Button1和Button2时。可以自由移动焦点。一旦移动到我的MyButton,方向键就失效了!请问大家一下,我要在MyButton下怎么写。才可以用方向键。3个按钮自由移动焦点位置!

解决方案 »

  1.   

    你的自定义控件是不是没有实现KeyDown事件
      

  2.   


    我不知道这里怎么写。请大家帮帮忙
     protected override void OnKeyDown(KeyEventArgs e)
            {            base.OnKeyDown(e);
                if (e.KeyCode == Keys.Up)
                {
                  
                   //获得上1个button的焦点
                }
                if (e.KeyCode == Keys.Down)
                {
                    //获得下1个button的焦点
                }        }
      

  3.   

    方法已经找到重写控件要加入这些内容
     protected override void OnLostFocus(EventArgs e)
            {
                Invalidate();
                base.OnLostFocus(e);
            }        protected override void OnClick(EventArgs e)
            {
                Invalidate();
                base.OnClick(e);
            }
            protected override void OnKeyUp(KeyEventArgs e)
            {
                Invalidate();
                base.OnKeyUp(e);
            }        protected override void OnPaint(PaintEventArgs e)
            {
                if (this._image != null)
                {
                    e.Graphics.DrawImage(this._image, 0, 0, e.ClipRectangle, GraphicsUnit.Pixel);
                }
                //base.OnPaint(e);
            }        public EventHandler MyKeyDown;
            public EventHandler MyKeyUp;        /// <summary>
            /// 方向键 
            /// </summary>
            /// <param name="e"></param>
            protected override void OnKeyDown(KeyEventArgs e)
            {
               Invalidate();
                if (e.KeyCode == Keys.Down || e.KeyCode == Keys.Right)//方向键 向下
                {
                    if (MyKeyDown != null)
                    {
                        MyKeyDown(this, null);
                    }
                   
                }
                if (e.KeyCode == Keys.Up || e.KeyCode == Keys.Left)//方向键 向上
                {
                    if (MyKeyUp != null)
                    {
                        MyKeyUp(this, null);
                    }
                }
                base.OnKeyDown(e);
            }            /// <summary>
            /// 回车
            /// </summary>
            /// <param name="e"></param>
            protected override void OnKeyPress(KeyPressEventArgs e)
            {            base.OnKeyPress(e);
                if (e.KeyChar == '\r')
                {
                    this.OnClick(e);
                    Invalidate();
                }
            }