VS 2005 C# 写PPC程序时,,窗口最上面是TextBox1,下面还有三个Button控件,,运行后光标在TextBox1中,,怎么让光标跳出TextBox,,选择控制其它Button控件呢?  具体操作和代码
注:此窗口是由别的窗口跳过来的,,TextBox1有文章

解决方案 »

  1.   

    this.ActiveControl = textbox1;
      

  2.   

    汗。。this.ActiveControl = button1;
      

  3.   

    ContainerControl.ActiveControl 属性
    获取或设置容器控件上的活动控件。 
      

  4.   

    和winform是不一样的.ppc里用键盘或者指点笔来切换键盘事件里设置,private void Form1_KeyDown(object sender, KeyEventArgs e)
            {
                if ((e.KeyCode == System.Windows.Forms.Keys.Up))
                {
                    // 向上导航
                    // 向上键
                }
                if ((e.KeyCode == System.Windows.Forms.Keys.Down))
                {
                    // 向下导航
                    // 向下键
                    this.button1.Focus(); //这句
                }
                if ((e.KeyCode == System.Windows.Forms.Keys.Left))
                {
                    // 向左键
                }
                if ((e.KeyCode == System.Windows.Forms.Keys.Right))
                {
                    // 向右键
                }
                if ((e.KeyCode == System.Windows.Forms.Keys.Enter))
                {
                    // Enter
                }        }
      

  5.   

    写了个Demo,按回车键循环切换控件,没来得及写注释,希望能看明白~~
            private void textBox1_KeyDown(object sender, KeyEventArgs e)
            {
                setFocus(sender, e);
            }        private void button1_KeyDown(object sender, KeyEventArgs e)
            {
                setFocus(sender, e);
            }        private void button2_KeyDown(object sender, KeyEventArgs e)
            {
                setFocus(sender, e);
            }        private void button3_KeyDown(object sender, KeyEventArgs e)
            {
                setFocus(sender, e);
            }
            public static Control FindFocusedControl(Control container)
            {
                foreach (Control childControl in container.Controls)
                {
                    if (childControl.Focused)
                    {
                        return childControl;
                    }
                }            foreach (Control childControl in container.Controls)
                {
                    Control maybeFocusedControl = FindFocusedControl(childControl);
                    if (maybeFocusedControl != null)
                    {
                        return maybeFocusedControl;
                    }
                }            return null;
            }        void setFocus(object sender, KeyEventArgs e)
            {            if ((e.KeyCode == System.Windows.Forms.Keys.Enter))
                {                this.SelectNextControl((Control)sender, true, true, true,true);            }
                this.textBox1.Text = FindFocusedControl(this).Name;
            }
      

  6.   

    奇怪,少粘贴了一段,楼上作废,重来
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace DeviceApplication5
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            private void textBox1_KeyDown(object sender, KeyEventArgs e)
            {
                setFocus(sender, e);
            }        private void button1_KeyDown(object sender, KeyEventArgs e)
            {
                setFocus(sender, e);
            }        private void button2_KeyDown(object sender, KeyEventArgs e)
            {
                setFocus(sender, e);
            }        private void button3_KeyDown(object sender, KeyEventArgs e)
            {
                setFocus(sender, e);
            }
            public static Control FindFocusedControl(Control container)
            {
                foreach (Control childControl in container.Controls)
                {
                    if (childControl.Focused)
                    {
                        return childControl;
                    }
                }            foreach (Control childControl in container.Controls)
                {
                    Control maybeFocusedControl = FindFocusedControl(childControl);
                    if (maybeFocusedControl != null)
                    {
                        return maybeFocusedControl;
                    }
                }            return null;
            }        void setFocus(object sender, KeyEventArgs e)
            {            if ((e.KeyCode == System.Windows.Forms.Keys.Enter))
                {                this.SelectNextControl((Control)sender, true, true, true,true);            }
                this.textBox1.Text = FindFocusedControl(this).Name;
            }
        }
    }
      

  7.   

    直接把这些代码粘贴在代码栏里,,运行后没有还是跟没有粘贴这些代码一样,,光标还是在TextBox1里,,按模拟器上的中间键和键盘的回车都没有效果
      

  8.   


    直接拷贝的话,你还要做几件事
    把textBox1的KeyDown的事件指向textBox1_KeyDown
    把button1的KeyDown事件指向button1_KeyDown
    把button2的KeyDown事件指向button2_KeyDown
    把button3的KeyDown事件指向button3_KeyDown
      

  9.   

    我将4个事件指向了,,运行后按回车,TextBox1显示了Button1,在按一次显示Button2,依次循环,,,,我想要的是  因为我们运行光标在textbox1里,,,按下回车,使光标跳出来,用上下方向键来选择控件,,按回车会实行控件所写的触发事件,,就像我们用鼠标来选择控件,,点击控件会实行控件的事件一样
      

  10.   

    之前你可能没开发过ppc,仔细看看,按了enter已经跳出来了
    为了让你看清楚当前焦点落在了哪个控件上
    所以写了一句 this.textBox1.Text = FindFocusedControl(this).Name;
    让textBox1显示当前焦点所在位置你想要的效果,小改动就能实现了
    textbox显示的
      

  11.   

    我之前只写过半个记事本的程序,,所以我的C#基础差得一塌糊涂,,所以你的代码我只看懂几句话的功能
    我那个TextBox1需要一直显示存在相关文章的,,
    所以this.textBox1.Text = FindFocusedControl(this).Name;对我来说不是实用
      

  12.   

    写这句this.textBox1.Text = FindFocusedControl(this).Name;只是为了让你看到当前那个按钮被激活,不写也不影响被激活的顺序稍等,改下给你
      

  13.   

    不好意思,中午有点事儿离开了按上下键切换焦点,按回车键显示当前焦点的messageusing System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace DeviceApplication5
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            private void textBox1_Press(object sender, KeyEventArgs e)
            {
                Control ctrl = (Control)sender;
                if ((e.KeyCode == System.Windows.Forms.Keys.Enter))
                {
                    this.SelectNextControl(ctrl, true, true, true, true);
                    MessageBox.Show("当前焦点的控件:" + FindFocusedControl(this).Name);
                }
            }        private void button1_Press(object sender, KeyEventArgs e)
            {
                Control ctrl = (Control)sender;
                if ((e.KeyCode == System.Windows.Forms.Keys.Down))
                {
                   this.SelectNextControl(ctrl, true, true, true, true);
                }
                if ((e.KeyCode == System.Windows.Forms.Keys.Up))
                {
                    this.SelectNextControl(ctrl, false, true, true, true);
                }
                //按回车要做的事儿
                if ((e.KeyCode == System.Windows.Forms.Keys.Enter))
                {
                    MessageBox.Show("当前焦点的控件:" + FindFocusedControl(this).Name);
                }
            }        private void button2_Press(object sender, KeyEventArgs e)
            {
                Control ctrl = (Control)sender;
                if ((e.KeyCode == System.Windows.Forms.Keys.Down))
                {
                    this.SelectNextControl(ctrl, true, true, true, true);
                }
                if ((e.KeyCode == System.Windows.Forms.Keys.Up))
                {
                    this.SelectNextControl(ctrl, false, true, true, true);
                }
                //按回车要做的事儿
                if ((e.KeyCode == System.Windows.Forms.Keys.Enter))
                {
                    MessageBox.Show("当前焦点的控件:" + FindFocusedControl(this).Name);
                }
            }        private void button3_Press(object sender, KeyEventArgs e)
            {
                Control ctrl = (Control)sender;
                if ((e.KeyCode == System.Windows.Forms.Keys.Down))
                {
                    this.SelectNextControl(ctrl, true, true, true, true);
                }
                if ((e.KeyCode == System.Windows.Forms.Keys.Up))
                {
                    this.SelectNextControl(ctrl, false, true, true, true);
                }
                //按回车要做的事儿
                if ((e.KeyCode == System.Windows.Forms.Keys.Enter))
                {
                    MessageBox.Show("当前焦点的控件:" + FindFocusedControl(this).Name);
                }
            }
            public static Control FindFocusedControl(Control container)
            {
                foreach (Control childControl in container.Controls)
                {
                    if (childControl.Focused)
                    {
                        return childControl;
                    }
                }
                return null;
            }
        }
    }
      

  14.   

    我把textBox1以及Button的KeyPress的事件指向textBox1_Press
    出现了错误 “System.Windows.Forms.KeyPressEventArgs”并不包含“KeyCode”的定义
    是不是事件指向错还是有什么错误呢?
      

  15.   


    textBox1指向textBox1_Press事件没错button1要指向button1_Press事件
    button2要指向button2_Press事件
    button3要指向button3_Press事件
      

  16.   

    textBox1事件里的KeyDown指向textBox1_Press事件button1事件里的KeyDown指向button1_Press事件
    button2事件里的KeyDown指向button2_Press事件
    button3事件里的KeyDown指向button3_Press事件
      

  17.   

    你有什么更好的方法可以实现模拟器上方向键选择控制控件以及按下中间键可以执行控件内容呢?
    当选到哪个Button控件时,那个控件的背景颜色会改变,