在winforms中的tabControl中隐藏选项卡标题

时间:2016-02-08 19:58:53

标签: c# winforms tabcontrol

我试图在tabControl中隐藏标签页头,就像this link中显示的一样,但我在设计师的代码中收到错误。一旦我改变了两行,我就明白了:

  

严重级代码说明项目文件行   消息设计人员无法处理未知名称' SelectedIndex'在第43行。方法中的代码' InitializeComponent'由设计者生成,不应手动修改。请删除所有更改,然后再次尝试打开设计器。 c:\ users \ krzysztof \ documents \ visual studio 2015 \ Projects \ DaneUzytkownika3 \ DaneUzytkownika3 \ TabController.Designer.cs 44

     

严重级代码说明项目文件行   错误CS1061' TabController'不包含' SelectedIndex'的定义没有扩展方法' SelectedIndex'接受类型' TabController'的第一个参数。可以找到(你错过了使用指令或汇编引用吗?)DaneUzytkownika3 c:\ users \ krzysztof \ documents \ visual studio 2015 \ Projects \ DaneUzytkownika3 \ DaneUzytkownika3 \ TabController.Designer.cs 43

表单设计人员代码中的第43行是:

this.tabControl1.SelectedIndex = 0;

有人可以告诉我,我该如何解决?

<小时/> TablessTabControl.cs

namespace hiding
{
    class TablessTabControl : Form1
    {
        protected override void WndProc(ref Message m)
        {
            // Hide tabs by trapping the TCM_ADJUSTRECT message
            if (m.Msg == 0x1328 && !DesignMode)
                m.Result = (IntPtr)1;
            else
                base.WndProc(ref m);
        }
    }
}

Form1.Designer.cs

namespace hiding
{
    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 TablessTabControl();
            //this.tabControl1 = new System.Windows.Forms.TabControl();
            this.tabPage1 = new System.Windows.Forms.TabPage();
            this.tabPage2 = new System.Windows.Forms.TabPage();
            this.tabControl1.SuspendLayout();
            this.SuspendLayout();
            // 
            // tabControl1
            // 
            this.tabControl1.Controls.Add(this.tabPage1);
            this.tabControl1.Controls.Add(this.tabPage2);
            this.tabControl1.Location = new System.Drawing.Point(31, 12);
            this.tabControl1.Name = "tabControl1";
            this.tabControl1.SelectedIndex = 0;//line with the error
            this.tabControl1.Size = new System.Drawing.Size(200, 100);
            this.tabControl1.TabIndex = 0;
            // 
            // tabPage1
            // 
            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(192, 74);
            this.tabPage1.TabIndex = 0;
            this.tabPage1.Text = "tabPage1";
            this.tabPage1.UseVisualStyleBackColor = true;
            // 
            // tabPage2
            // 
            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(192, 74);
            this.tabPage2.TabIndex = 1;
            this.tabPage2.Text = "tabPage2";
            this.tabPage2.UseVisualStyleBackColor = true;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(284, 261);
            this.Controls.Add(this.tabControl1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.tabControl1.ResumeLayout(false);
            this.ResumeLayout(false);

        }

        #endregion

        private TablessTabControl tabControl1;
        //private System.Windows.Forms.TabControl tabControl1;
        private System.Windows.Forms.TabPage tabPage1;
        private System.Windows.Forms.TabPage tabPage2;
    }
}

1 个答案:

答案 0 :(得分:4)

我创建了一个项目并实现了示例中给出的选项卡控件,如下所示:

class TablessTabControl : TabControl
{
    protected override void WndProc(ref Message m)
    {
        // Hide tabs by trapping the TCM_ADJUSTRECT message
        if (m.Msg == 0x1328 && !DesignMode)
            m.Result = (IntPtr)1;
        else
            base.WndProc(ref m);
    }
}

然后在重建项目时,我使用设计器将新的TablessTabControl添加到测试表单中。在设计器中,我可以使用可见的标题在选项卡之间切换。

在运行时,标题会按预期消失。我有两个标签;我可以使用以下代码在选项卡之间进行选择:

// Selects the first tab:
tablessTabControl1.SelectedIndex = 0;

// Selects the second tab:
tablessTabControl1.SelectedIndex = 1;

此外,在Form1.Designer.cs中,我的行48如下:

this.tablessTabControl1.SelectedIndex = 0;

对我来说没有任何困难。

您是否尝试关闭所有文档,清理解决方案,重建并重新打开设计器?