| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1801 人关注过本帖
标题:回车实现Tab键切换控件的问题?
只看楼主 加入收藏
35maoe
Rank: 1
等 级:新手上路
帖 子:341
专家分:0
注 册:2006-8-28
结帖率:100%
收藏
 问题点数:0 回复次数:3 
回车实现Tab键切换控件的问题?
我现在是通过每个控件的KeyPress事件来取得回车事件切换到下一控件,
这样就要在每个控件上都要写,每个控件都要写,
  private void txtCardid_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                this.cobIsjiankong.Focus();
            }
        }
上面就是在txtCardid控件的KeyPress事件上选择coblsjiankong控件,
请问如何让回车键通过简单方法来实现这一功能?
控件的Tabindex值现在是从0按顺序排列的
具体我不知怎么写,请大家帮忙!谢谢!!!
搜索更多相关主题的帖子: Tab 控件 回车 
2007-11-27 08:16
陌生人2007
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2007-11-21
收藏
得分:0 
将所有的控件keypress事件注册到同一个_keypress()中,之后在里面用foreach看看
2007-11-27 09:46
marer
Rank: 2
等 级:新手上路
威 望:3
帖 子:928
专家分:0
注 册:2005-7-18
收藏
得分:0 
public partial class Form1 : Form
    {
        private int i= 0;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.textBox1.Focus();
            
            //keyindex = this.textBox1.TabIndex;
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                i++;
                if (i > 3)
                    i = 0;
                foreach (System.Windows.Forms.Control c in this.Controls)
                {
                    if (c is TextBox)
                    {
                        if (c.TabIndex == i)
                            c.Focus();
                    }
                }
            }
        }

        
    }
窗体上有四个文本框,他们的TabIndex是从0到3顺序排列,每个文本框的KeyPress事件都是这个事件,即可实现

[[italic] 本帖最后由 marer 于 2007-11-27 13:16 编辑 [/italic]]

public class 人生历程 extends Thread{public void run(){while(true){努力,努力,再努力!!;Thread.sleep(0);}}}
2007-11-27 13:14
marer
Rank: 2
等 级:新手上路
威 望:3
帖 子:928
专家分:0
注 册:2005-7-18
收藏
得分:0 
如果窗体上不光是文本框还有其他控件可以这样写:(注意:好像Button不能实现)
public partial class Form1 : Form
    {
        private int i= 0;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.textBox1.Focus();
            
            //keyindex = this.textBox1.TabIndex;
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                i++;
                if (i > 4)
                    i = 0;
                foreach (System.Windows.Forms.Control c in this.Controls)
                {
                    //if (c is TextBox)
                    //{
                        if (c.TabIndex == i)
                            c.Focus();
                    //}
                }
            }
        }

        
    }

public class 人生历程 extends Thread{public void run(){while(true){努力,努力,再努力!!;Thread.sleep(0);}}}
2007-11-27 13:21
快速回复:回车实现Tab键切换控件的问题?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.043324 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved