form中有一个tabControl,有若干个tabPage,每个tabPage里有一些表单和按钮,如何实现按回车相当于触发当前tabPage里的按钮click事件。

解决方案 »

  1.   

    不用设置吧,单独设置对应Tab下关联回车键的Button就行了。
      

  2.   

    private void button1_KeyPress(object sender, KeyPressEventArgs e) 
            { 
                if(e.KeyChar==13) 
                { 
                    button1.Click +=new EventHandler(button1_Click(button1,null)); 
                } 
            }         private void button1_Click(object sender, EventArgs e) 
            { 
                MessageBox.Show("aaaaaaaaa"); 
            }
      

  3.   


    不是说“实现按回车相当于触发当前tabPage里的按钮click事件。”
    那样的话,焦点在不在按钮上有什么影响呢?
    实在不行,你就先把焦点聚焦在按钮上呗,
    .focus()
      

  4.   

    窗体捕获按键事件?
    判断tabControl当前选择页?
    调用改页面指定按钮的Click事件?
      

  5.   


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Windows.Forms;namespace WindowsFormsApplication7
    {
        static class Program
        {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                MessageFilter filter = new MessageFilter();
                Application.AddMessageFilter(filter);
                Form1 form = new Form1();
                form.Filtr = filter;
                Application.Run(new Form1());
            }
        }
        public class MessageFilter : IMessageFilter
        {
            public event EventHandler EnterPressed;
            #region IMessageFilter Members        public bool PreFilterMessage(ref Message m)
            {
                if (m.Msg == 0x0100)
                {
                    if ( m.WParam.ToInt32() ==  13 && EnterPressed != null)
                    {
                        EnterPressed(this,EventArgs.Empty);
                    }
                    
                }
                return false;
            }        #endregion
        }
    }
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;namespace WindowsFormsApplication7
    {
        public partial class Form1 : Form
        {
            private MessageFilter filtr;        public MessageFilter Filtr
            {
                get { return filtr; }
                set { filtr = value; }
            }
            public Form1()
            {
                InitializeComponent();
                filtr.EnterPressed += new EventHandler(OnEnterPrecessed);
            }        private void OnEnterPrecessed(object sender, EventArgs e)
            {
                //检查当前标签。并进行处理的代码。
            }
            
        }
    }
      

  6.   

    当前的焦点不知道在什么位置,不一定是在按钮上~你要的这个是可以实现的, 只是这样的一种要求同常规不大一致。一般来说,多个Tabpage时,确定按钮都是只有一个,在最下面,按回车时触发最下面的按钮,,而对于各个页来说是不需要回车确定的。。
      

  7.   

    或者把事件放到page_load里面,页面刷新时做个判断
      

  8.   

    这个功能对.NET WinForm来说太简单了...简单到你不敢相信,根本不用管什么焦点什么刷新...设置 Form.AcceptButton 属性即可...此属性可以指定当用户在应用程序中按 Enter 键时发生的默认操作。分配给此属性的按钮必须是位于当前窗体上或者其容器中的 IButtonControl。form1.AcceptButton = button1;//指定哪个按钮它就是当前窗体的默认按钮