我做了一個快速測試應用程序,看看我是否可以複製你的問題。我做的唯一不同的是在設計師中添加面板,並將其可見性設置爲false。它正確地做到了這一點。它看起來像你正在手動創建panTitle面板。何時/何時將它添加到您的控件中,您最好的選擇就是像上面所述添加面板。
編輯:
在閱讀你的問題有點接近,好像你不想要的面板,當你在看DerivedUserControl的設計選項卡showup。我發佈的內容不會改變,我不確定這種行爲是否可以改變。當你將它放在窗體上時它將不可見,並且以這種方式按預期行事。
這是一個快速工作的例子。
Form1中
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
{
DerivedUserControl dv = new DerivedUserControl();
public Form1()
{
InitializeComponent();
this.Controls.Add(dv);
}
private void button1_Click(object sender, EventArgs e)
{
if (dv.IsTitlePanelVisible)
dv.IsTitlePanelVisible = false;
else
dv.IsTitlePanelVisible = true;
}
}
}
基地用戶控件
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class BaseControl : UserControl
{
public BaseControl()
{
InitializeComponent();
}
[DefaultValue(false)]
public bool IsTitlePanelVisible
{
get { return panTitle.Visible; }
set { panTitle.Visible = value; }
}
}
}
BaseControl.Designer。CS的InitializeComponent
private void InitializeComponent()
{
this.panTitle = new System.Windows.Forms.Panel();
this.SuspendLayout();
//
// panTitle
//
this.panTitle.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(192)))), ((int)(((byte)(255)))));
this.panTitle.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.panTitle.Location = new System.Drawing.Point(0, 0);
this.panTitle.Name = "panTitle";
this.panTitle.Size = new System.Drawing.Size(150, 147);
this.panTitle.TabIndex = 0;
this.panTitle.Visible = false;
//
// BaseControl
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Controls.Add(this.panTitle);
this.Name = "BaseControl";
this.ResumeLayout(false);
}
派生的用戶控件
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class DerivedUserControl : BaseControl
{
public DerivedUserControl()
{
InitializeComponent();
}
}
}
爲什麼在該類中聲明「panTitle」,而不是在designer.cs文件中聲明? – 2012-07-20 15:19:37
我只在這裏添加它來表明它是Panel類型的,它實際上是在設計器中聲明的。 – loreggia 2012-08-22 14:19:12