Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6385583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:55:05+00:00 2026-05-25T02:55:05+00:00

TabControl .SelectTab() engages the tab page and the focus momentarily appears on the tab

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T02:55:05+00:00Added an answer on May 25, 2026 at 2:55 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using TabControl.SelectTab(...) shows the tab but it also gives the tab focus. I would
I use a TabControl Each tab has its default button. The first default button
I have a TabControl, and on the second tab is a button: <Button Name=deleteResourceBtn>
How can i remove the tabs from tabcontrol when i reference each tab to
In a winforms tabcontrol, is there a way to make a certain tab visible
Problem I have Telerik TabControl and each tab content is a partial view. Everything
I have a TabControl where each tab contains a DataGrid . When the user
I have a TabControl with multiple pages. On one page, I just have a
Facts: I have a TabControl with 2 Tabs, Each tab has 2 DateTimePicker. In
I am using a tabcontrol, i want to remove a particular tab from the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.