【问题标题】:Show/Hide panel of parent control显示/隐藏父控件面板
【发布时间】:2012-07-19 21:12:53
【问题描述】:

我有一个继承自 UserControl 的基类,上面有一个面板。我创建了一个允许我显示/隐藏面板的属性。

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”是在类中声明而不是在designer.cs文件中?
  • 我这里只是为了表明它是Panel类型的,它实际上是在设计器中声明的。

标签: c# windows visual-studio-2010 forms designer


【解决方案1】:

我制作了一个快速测试应用程序,看看我是否可以复制您的问题。我唯一不同的是在设计器中添加面板并将其可见性设置为 false。它工作正常。看起来您正在手动创建 panTitle 面板。您在何处/何时将其添加到您的控件中,最好的办法是像我上面所说的那样添加面板。


编辑:

在仔细阅读您的问题时,您似乎不希望在查看 DerivedUserControl 的设计选项卡时显示面板。我发布的内容不会改变这一点,我不确定这种行为是否可以改变。但是,当您将其放在 Form 上时,它将不可见,并且这样做就像预期的那样。


这是一个简单的工作示例。

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();
        }
    }
}

【讨论】:

  • “在仔细阅读您的问题后,您似乎不希望在查看 DerivedUserControl 的设计选项卡时显示面板。”这正是我一直试图解决的问题。
【解决方案2】:

也许你需要调用基础构造函数

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
    }
}

同样来自MSDN

一个 DefaultValueAttribute 不会导致一个成员被自动 用属性的值初始化。您必须设置初始值 在你的代码中。

【讨论】:

  • 这没有改变。另外,如果我没记错的话,默认情况下编译器会插入 base() 调用。
【解决方案3】:

您是否尝试过将panTitle.Visible 默认设置为false?

ComponentModel的DefaultValue属性仅用于设计者判断propertyGrid中的属性是否应显示为粗体(脏),以及其值是否应生成到派生的InitializeComponent方法中 BaseControl 的类

【讨论】:

  • 这只会在运行时隐藏它。就目前而言,我看到的唯一可能性是在派生控件的设计器代码中将该属性设置为 false,即使它应该已经是 false ..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-19
  • 2011-03-27
  • 1970-01-01
  • 2023-03-03
相关资源
最近更新 更多