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

发布于 2024-12-02 16:43:44 字数 12504 浏览 0 评论 0原文

TabControl .SelectTab() 接合选项卡页,焦点按预期暂时出现在选项卡上,但随后立即跳转到选项卡页上的按钮。我怎样才能让它焦点停留在选项卡上?

我尝试过

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

,但失败了,将焦点发送到看不见的地方。

一种解决方案是将所有按钮包含在一个组框中(!),但我宁愿避免这样的混乱。

显示此拼凑的默认 Windows 窗体应用程序代码位于此处 - 构建、运行并按 F5 查看失败,按 F6 查看拼凑。

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;
        }
    }

谢谢。

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.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;
        }
    }

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白龙吟 2024-12-09 16:43:44

不要将 TabPage 聚焦,它不知道如何指示焦点,也对键盘输入没有任何用处。改为聚焦 TabControl。

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文