C# 显示/隐藏父控件的面板
我有一个从UserControl继承的基类,上面有一个面板。我制作了一个属性,允许我显示/隐藏面板C# 显示/隐藏父控件的面板,c#,windows,visual-studio-2010,forms,designer,C#,Windows,Visual Studio 2010,Forms,Designer,我有一个从UserControl继承的基类,上面有一个面板。我制作了一个属性,允许我显示/隐藏面板 public partial class BaseControl : UserControl { // ... private Panel panTitle; // this is actually declared in the designer file.. public BaseControl() { InitializeComponent
public partial class BaseControl : UserControl
{
// ...
private Panel panTitle; // this is actually declared in the designer file..
public BaseControl()
{
InitializeComponent();
// hide the panel by default
IsTitlePanelVisible = false;
}
[DefaultValue(false)]
public bool IsTitlePanelVisible
{
get { return panTitle.Visible; }
set { panTitle.Visible = value; }
}
}
现在,如果我在设计器中打开从BaseControl继承的其他控件,面板将可见!
在属性窗口中将IsTitlePanelVisible属性更改为true并返回false后,它将消失
在BaseControl的设计器中,我还将面板本身的Visible属性设置为false,但它仍然显示出来
有没有人对如何在设计器中打开派生控件时不显示面板提出一些建议
编辑:为了让事情更清楚,增加了以下内容:
我已经有相当多的派生控件,我不想全部更改。
如果我打开一个派生控件并手动将该值设置为false,一切正常,但我不明白为什么它不工作,因为该值在基本控件的构造函数中设置为false。您是否尝试过将
panTitle.Visible
默认设置为false
ComponentModel
DefaultValue
属性仅用于设计器确定propertyGrid中的属性是否应显示为粗体(脏)以及是否应将其值生成到BaseControl
的派生类的InitializeComponent
方法。您是否尝试将panTitle.Visible
默认设置为false
ComponentModel
DefaultValue
属性仅用于设计器确定propertyGrid中的属性是否应显示为粗体(脏)以及是否应将其值生成到BaseControl的派生类的InitializeComponent
方法中。我制作了一个快速测试应用程序,看看是否可以复制您的问题。我唯一不同的做法是在设计器中添加面板,并将其可见性设置为false。这样做是正确的。看起来您正在手动创建panTitle面板。在何处/何时将其添加到控件中,最好的方法是像上面所述那样添加面板
编辑:
仔细阅读您的问题,您似乎不希望在查看DerivedUserControl的设计选项卡时显示面板。我所发布的不会改变这一点,我不确定这种行为是否可以改变。但是,当您将它放到表单上时,它将不可见,这样做就像预期的那样
下面是一个快速工作的示例
表格1
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; }
}
}
}
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();
}
}
}
BaseControl.Designer.cs初始化组件
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;
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; }
}
}
}
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();
}
}
}
我做了一个快速测试申请,看看是否可以复制你的问题。我唯一不同的做法是在设计器中添加面板,并将其可见性设置为false。这样做是正确的。看起来您正在手动创建panTitle面板。在何处/何时将其添加到控件中,最好的方法是像上面所述那样添加面板
编辑:
仔细阅读您的问题,您似乎不希望在查看DerivedUserControl的设计选项卡时显示面板。我所发布的不会改变这一点,我不确定这种行为是否可以改变。但是,当您将它放到表单上时,它将不可见,这样做就像预期的那样
下面是一个快速工作的示例
表格1
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; }
}
}
}
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();
}
}
}
BaseControl.Designer.cs初始化组件
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;
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; }
}
}
}
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();
}
}
}
也许您需要调用基本构造函数
class DerivedControl : BaseControl
{
public DerivedControl()
: base()
{
}
}
class BaseControl : UserControl
{
public BaseControl ()
{
InitializeComponent(); // makes the panel visible by default
IsTitlePanelVisible = false // makes the panel hidden explicity
}
}
也来自:
DefaultValueAttribute不会导致自动删除成员
使用属性的值初始化。必须设置初始值
在你的代码中
也许您需要调用基本构造函数
class DerivedControl : BaseControl
{
public DerivedControl()
: base()
{
}
}
class BaseControl : UserControl
{
public BaseControl ()
{
InitializeComponent(); // makes the panel visible by default
IsTitlePanelVisible = false // makes the panel hidden explicity
}
}
也来自:
DefaultValueAttribute不会导致自动删除成员
使用属性的值初始化。必须设置初始值
在你的代码中
为什么“panTitle”在类中声明而不是在designer.cs文件中?我在这里添加它只是为了表明它是Panel类型,它实际上是在designer中声明的。为什么“panTitle”在类中声明而不是在designer.cs文件中声明?我在这里添加它只是为了表明它是Panel类型,它实际上是在designer中声明的。“仔细阅读您的问题时,您似乎不希望在查看DerivedUserControl的“设计”选项卡时显示面板。“这正是我一直试图解决的问题。”仔细阅读您的问题时,当您查看DerivedUserControl的“设计”选项卡时,似乎不希望面板出现。“这正是我一直试图解决的问题。这没有改变任何内容。此外,如果我没有弄错的话,编译器默认插入base()调用。这没有改变任何内容。此外,base()调用如果我没有弄错的话,默认情况下编译器会插入调用。这只会使它在运行时隐藏。目前,我看到的唯一可能性是在派生控件的设计器代码中将属性设置为false,即使它应该已经为false。这只会使它在运行时隐藏。目前,我看到的唯一可能性是设置属性在派生控件的设计器代码中设置为false,即使它应该已经为false。。