如何在继承的WinForm控件添加到Container(How do I add a Control

2019-07-20 13:38发布

我在强制执行相同的外观和感觉,我们所有的WinForms项目另一个项目基本形式。 我从音素表示继承,创建自己的模板,基本视角。 我的这个模板有像进度,一个定时器和一个TableLayoutPanel额外的控制。

现在我想从我的基本视点继承和使用Designer来添加我的具体程序的控制,但我不能删除同样的面板或其他控件到任何东西。 我已经尝试了一些建议,如确保基本形式的成分是公开的,但都无济于事 - 大多数TLP的属性仍显示为灰色。

可能有人或许能给我什么建议? 非常感谢!


partial class BaseView
{
    /// <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.components = new System.ComponentModel.Container();
        System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(BaseView));
        this.tableLayoutPanel_Form = new System.Windows.Forms.TableLayoutPanel();
        this.panel_Buttons = new System.Windows.Forms.Panel();
        this.panel_MainBody = new System.Windows.Forms.Panel();
        this.progressBar = new System.Windows.Forms.ProgressBar();
        this.timer_ProgressBar = new System.Windows.Forms.Timer(this.components);
        this.tableLayoutPanel_Form.SuspendLayout();
        this.SuspendLayout();
        // 
        // lblFormId
        // 
        this.lblFormId.AutoSize = false;
        this.lblFormId.Location = new System.Drawing.Point(390, 9);
        this.lblFormId.Size = new System.Drawing.Size(98, 13);
        this.lblFormId.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
        // 
        // btnClose
        // 
        this.btnClose.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
        this.btnClose.DialogResult = System.Windows.Forms.DialogResult.Cancel;
        this.btnClose.Location = new System.Drawing.Point(447, 5);
        this.btnClose.Size = new System.Drawing.Size(41, 21);
        this.btnClose.TabIndex = 4;
        // 
        // tableLayoutPanel_Form
        // 
        this.tableLayoutPanel_Form.ColumnCount = 1;
        this.tableLayoutPanel_Form.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
        this.tableLayoutPanel_Form.Controls.Add(this.panel_Buttons, 0, 2);
        this.tableLayoutPanel_Form.Controls.Add(this.panel_MainBody, 0, 1);
        this.tableLayoutPanel_Form.Dock = System.Windows.Forms.DockStyle.Fill;
        this.tableLayoutPanel_Form.Location = new System.Drawing.Point(0, 0);
        this.tableLayoutPanel_Form.Name = "tableLayoutPanel_Form";
        this.tableLayoutPanel_Form.RowCount = 3;
        this.tableLayoutPanel_Form.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 30F));
        this.tableLayoutPanel_Form.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
        this.tableLayoutPanel_Form.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 30F));
        this.tableLayoutPanel_Form.Size = new System.Drawing.Size(496, 322);
        this.tableLayoutPanel_Form.TabIndex = 26;
        // 
        // panel_Buttons
        // 
        this.panel_Buttons.Dock = System.Windows.Forms.DockStyle.Fill;
        this.panel_Buttons.Location = new System.Drawing.Point(3, 295);
        this.panel_Buttons.Name = "panel_Buttons";
        this.panel_Buttons.Size = new System.Drawing.Size(490, 24);
        this.panel_Buttons.TabIndex = 0;
        // 
        // panel_MainBody
        // 
        this.panel_MainBody.Dock = System.Windows.Forms.DockStyle.Fill;
        this.panel_MainBody.Location = new System.Drawing.Point(3, 33);
        this.panel_MainBody.Name = "panel_MainBody";
        this.panel_MainBody.Size = new System.Drawing.Size(490, 256);
        this.panel_MainBody.TabIndex = 1;
        // 
        // progressBar
        // 
        this.progressBar.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
        this.progressBar.Location = new System.Drawing.Point(383, 324);
        this.progressBar.Name = "progressBar";
        this.progressBar.Size = new System.Drawing.Size(112, 21);
        this.progressBar.TabIndex = 27;
        // 
        // timer_ProgressBar
        // 
        this.timer_ProgressBar.Interval = 700;
        this.timer_ProgressBar.Tick += new System.EventHandler(this.timer_Tick);
        // 
        // BaseView
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.CancelButton = this.btnClose;
        this.ClientSize = new System.Drawing.Size(496, 346);
        this.Controls.Add(this.progressBar);
        this.Controls.Add(this.tableLayoutPanel_Form);
        this.Cursor = System.Windows.Forms.Cursors.Default;
        this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
        this.MinimumSize = new System.Drawing.Size(512, 384);
        this.Name = "BaseView";
        this.Controls.SetChildIndex(this.btnClose, 0);
        this.Controls.SetChildIndex(this.tableLayoutPanel_Form, 0);
        this.Controls.SetChildIndex(this.lblFormId, 0);
        this.Controls.SetChildIndex(this.lblFormDescription, 0);
        this.Controls.SetChildIndex(this.progressBar, 0);
        this.tableLayoutPanel_Form.ResumeLayout(false);
        this.ResumeLayout(false);
        this.PerformLayout();

    }

    #endregion

    private System.Windows.Forms.ProgressBar progressBar;
    private System.Windows.Forms.Timer timer_ProgressBar;
    public System.Windows.Forms.TableLayoutPanel tableLayoutPanel_Form;
    public System.Windows.Forms.Panel panel_Buttons;
    public System.Windows.Forms.Panel panel_MainBody;
}

Answer 1:

这 MSDN文章给我的回答。

“并非所有的控件都支持从基本形式的视觉继承以下控件不支持在此演练中描述的场景:web浏览器,ToolStrip中,ToolStripPanel中,TableLayoutPanel中,FlowLayoutPanel的,DataGridView的这些控制在继承的形式始终是只读不管。修饰符使用(私有,保护,或公共)“。

我用了一个停靠面板和SplitContainer的,而不是一个TLP,现在我可以删除任何我想要的放进去。



文章来源: How do I add a Control to a Container in an inherited WinForm