开发者

How to avoid TabControl .SelectTab() causing focus to leap from tab to e.g. button?

开发者 https://www.devze.com 2023-04-01 17:42 出处:网络
TabControl .SelectTab() engages the tab page and the focus momentarily appears on the tab as expected but then immediately leaps to a button on the tab page. How can I get it focus to stay on the tab?

TabControl .SelectTab() engages the tab page and the focus momentarily appears on the tab as expected but then immediately leaps to a button on the tab page. How can I get it focus to stay on the tab?

I tried

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

but this fails, sending the focus somewhere invisible.

One solution is to enclose all buttons in a groupbox (!) but I would rather avoid such a kludge.

The default Windows Forms App code showing this kludge is here - build, run and press F5 to see the fail, and F6 for the 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.button开发者_Go百科2.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;
        }
    }

Thanks.


Don't focus the TabPage, it doesn't know how to indicate focus nor does it have any use for keyboard input. Focus the TabControl instead.

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

A bit flaky on Win7 when I tried it, it doesn't always draw the dotted rectangle on the tab. Pressing the left/right cursor keys does however change tabs.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号