如何避免TabControl .SelectTab()导致焦点从tab跳到例如按钮?

时间:2011-09-01 21:00:40

标签: .net c#-4.0

TabControl .SelectTab()启用标签页,焦点会随时按预期显示在标签上,然后立即跳到标签页上的按钮。如何让它专注于留在标签上?

我试过

mytc.TabPages[mytc.SelectedIndex].Focus();

但是失败了,将焦点发送到隐藏的地方。

一种解决方案是将所有按钮括在一个组框中(!),但我宁愿避免使用这种按钮。

显示此kludge的默认Windows窗体应用程序代码在此处 - 构建,运行并按F5查看失败,并按F6查看kludged。

Form1.cs的

    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 WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            TabControl mytc;
            public Form1()
            {
                InitializeComponent();
                mytc = tabControl1;
            }

            private void tab1ToolStripMenuItem_Click(object sender, EventArgs e)
            {   
                mytc.SelectTab(0); 
                // mytc.TabPages[mytc.SelectedIndex].Focus(); // fails
            }

            private void tab2ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                mytc.SelectTab(1); 
                // mytc.TabPages[mytc.SelectedIndex].Focus(); // fails
            }

            private void button1_Click(object sender, EventArgs e)
            {
                debugl.Text = "Button1";

            }
            private void button2_Click(object sender, EventArgs e)
            {
                debugl.Text = "Button2";
            }

        }
    }

Form1.Designer.cs

    namespace WindowsFormsApplication1
    {
        partial class Form1
        {
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.IContainer components = null;

            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            #region Windows Form Designer generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        this.tabControl1 = new System.Windows.Forms.TabControl();
        this.tabPage1 = new System.Windows.Forms.TabPage();
        this.button3 = new System.Windows.Forms.Button();
        this.tabPage2 = new System.Windows.Forms.TabPage();
        this.button2 = new System.Windows.Forms.Button();
        this.menuStrip1 = new System.Windows.Forms.MenuStrip();
        this.operationToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
        this.tab1ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
        this.tab2ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
        this.debugl = new System.Windows.Forms.Label();
        this.button1 = new System.Windows.Forms.Button();
        this.button4 = new System.Windows.Forms.Button();
        this.groupBox1 = new System.Windows.Forms.GroupBox();
        this.tabControl1.SuspendLayout();
        this.tabPage1.SuspendLayout();
        this.tabPage2.SuspendLayout();
        this.menuStrip1.SuspendLayout();
        this.groupBox1.SuspendLayout();
        this.SuspendLayout();
        // 
        // tabControl1
        // 
        this.tabControl1.Controls.Add(this.tabPage1);
        this.tabControl1.Controls.Add(this.tabPage2);
        this.tabControl1.Location = new System.Drawing.Point(9, 23);
        this.tabControl1.Name = "tabControl1";
        this.tabControl1.SelectedIndex = 0;
        this.tabControl1.Size = new System.Drawing.Size(272, 136);
        this.tabControl1.TabIndex = 0;
        // 
        // tabPage1
        // 
        this.tabPage1.Controls.Add(this.button4);
        this.tabPage1.Controls.Add(this.button3);
        this.tabPage1.Location = new System.Drawing.Point(4, 22);
        this.tabPage1.Name = "tabPage1";
        this.tabPage1.Padding = new System.Windows.Forms.Padding(3);
        this.tabPage1.Size = new System.Drawing.Size(264, 110);
        this.tabPage1.TabIndex = 0;
        this.tabPage1.Text = "tabPage1";
        this.tabPage1.UseVisualStyleBackColor = true;
        // 
        // button3
        // 
        this.button3.Location = new System.Drawing.Point(35, 61);
        this.button3.Name = "button3";
        this.button3.Size = new System.Drawing.Size(75, 23);
        this.button3.TabIndex = 0;
        this.button3.Text = "butt&on1";
        this.button3.UseVisualStyleBackColor = true;
        this.button3.Click += new System.EventHandler(this.button1_Click);
        // 
        // tabPage2
        // 
        this.tabPage2.Controls.Add(this.groupBox1);
        this.tabPage2.Location = new System.Drawing.Point(4, 22);
        this.tabPage2.Name = "tabPage2";
        this.tabPage2.Padding = new System.Windows.Forms.Padding(3);
        this.tabPage2.Size = new System.Drawing.Size(264, 110);
        this.tabPage2.TabIndex = 1;
        this.tabPage2.Text = "tabPage2";
        this.tabPage2.UseVisualStyleBackColor = true;
        // 
        // button2
        // 
        this.button2.Location = new System.Drawing.Point(13, 27);
        this.button2.Name = "button2";
        this.button2.Size = new System.Drawing.Size(75, 23);
        this.button2.TabIndex = 1;
        this.button2.Text = "butto&n2";
        this.button2.UseVisualStyleBackColor = true;
        this.button2.Click += new System.EventHandler(this.button2_Click);
        // 
        // menuStrip1
        // 
        this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
        this.operationToolStripMenuItem});
        this.menuStrip1.Location = new System.Drawing.Point(0, 0);
        this.menuStrip1.Name = "menuStrip1";
        this.menuStrip1.Size = new System.Drawing.Size(292, 24);
        this.menuStrip1.TabIndex = 1;
        this.menuStrip1.Text = "menuStrip1";
        // 
        // operationToolStripMenuItem
        // 
        this.operationToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
        this.tab1ToolStripMenuItem,
        this.tab2ToolStripMenuItem});
        this.operationToolStripMenuItem.Name = "operationToolStripMenuItem";
        this.operationToolStripMenuItem.ShortcutKeys = System.Windows.Forms.Keys.F5;
        this.operationToolStripMenuItem.Size = new System.Drawing.Size(67, 20);
        this.operationToolStripMenuItem.Text = "Operation";
        // 
        // tab1ToolStripMenuItem
        // 
        this.tab1ToolStripMenuItem.Name = "tab1ToolStripMenuItem";
        this.tab1ToolStripMenuItem.ShortcutKeys = System.Windows.Forms.Keys.F5;
        this.tab1ToolStripMenuItem.Size = new System.Drawing.Size(126, 22);
        this.tab1ToolStripMenuItem.Text = "tab1";
        this.tab1ToolStripMenuItem.Click += new System.EventHandler(this.tab1ToolStripMenuItem_Click);
        // 
        // tab2ToolStripMenuItem
        // 
        this.tab2ToolStripMenuItem.Name = "tab2ToolStripMenuItem";
        this.tab2ToolStripMenuItem.ShortcutKeys = System.Windows.Forms.Keys.F6;
        this.tab2ToolStripMenuItem.Size = new System.Drawing.Size(126, 22);
        this.tab2ToolStripMenuItem.Text = "tab2";
        this.tab2ToolStripMenuItem.Click += new System.EventHandler(this.tab2ToolStripMenuItem_Click);
        // 
        // debugl
        // 
        this.debugl.AutoSize = true;
        this.debugl.Location = new System.Drawing.Point(40, 197);
        this.debugl.Name = "debugl";
        this.debugl.Size = new System.Drawing.Size(37, 13);
        this.debugl.TabIndex = 2;
        this.debugl.Text = "debug";
        // 
        // button1
        // 
        this.button1.Location = new System.Drawing.Point(33, 67);
        this.button1.Name = "button1";
        this.button1.Size = new System.Drawing.Size(75, 23);
        this.button1.TabIndex = 1;
        this.button1.Text = "butto&n2";
        this.button1.UseVisualStyleBackColor = true;
        this.button1.Click += new System.EventHandler(this.button2_Click);
        // 
        // button4
        // 
        this.button4.Location = new System.Drawing.Point(127, 61);
        this.button4.Name = "button4";
        this.button4.Size = new System.Drawing.Size(75, 23);
        this.button4.TabIndex = 0;
        this.button4.Text = "butt&on1";
        this.button4.UseVisualStyleBackColor = true;
        this.button4.Click += new System.EventHandler(this.button1_Click);
        // 
        // groupBox1
        // 
        this.groupBox1.Controls.Add(this.button1);
        this.groupBox1.Controls.Add(this.button2);
        this.groupBox1.Location = new System.Drawing.Point(30, 4);
        this.groupBox1.Name = "groupBox1";
        this.groupBox1.Size = new System.Drawing.Size(200, 100);
        this.groupBox1.TabIndex = 3;
        this.groupBox1.TabStop = false;
        this.groupBox1.Text = "groupBox1";
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(292, 273);
        this.Controls.Add(this.debugl);
        this.Controls.Add(this.tabControl1);
        this.Controls.Add(this.menuStrip1);
        this.MainMenuStrip = this.menuStrip1;
        this.Name = "Form1";
        this.Text = "Form1";
        this.tabControl1.ResumeLayout(false);
        this.tabPage1.ResumeLayout(false);
        this.tabPage2.ResumeLayout(false);
        this.menuStrip1.ResumeLayout(false);
        this.menuStrip1.PerformLayout();
        this.groupBox1.ResumeLayout(false);
        this.ResumeLayout(false);
        this.PerformLayout();

    }

    #endregion

            private System.Windows.Forms.TabControl tabControl1;
            private System.Windows.Forms.TabPage tabPage1;
            private System.Windows.Forms.TabPage tabPage2;
            private System.Windows.Forms.Button button2;
            private System.Windows.Forms.MenuStrip menuStrip1;
            private System.Windows.Forms.ToolStripMenuItem operationToolStripMenuItem;
            private System.Windows.Forms.ToolStripMenuItem tab1ToolStripMenuItem;
            private System.Windows.Forms.ToolStripMenuItem tab2ToolStripMenuItem;
            private System.Windows.Forms.Label debugl;
            private System.Windows.Forms.Button button3;
            private System.Windows.Forms.Button button4;
            private System.Windows.Forms.Button button1;
            private System.Windows.Forms.GroupBox groupBox1;
        }
    }

感谢。

1 个答案:

答案 0 :(得分:2)

不要关注TabPage,它不知道如何指示焦点,也不知道它对键盘输入有什么用处。改为关注TabControl。

        private void tab1ToolStripMenuItem_Click(object sender, EventArgs e)
        {   
            mytc.SelectTab(0); 
            mytc.Focus();
        }

当我尝试Win7时,它有点不稳定,它并不总是在选项卡上绘制虚线矩形。但是,按左/右光标键可以更改标签。