UserControl的不可见子控件

时间:2019-01-15 03:54:51

标签: c# windows forms

我正在尝试创建一个容器控件,您可以通过单击切换按钮来“最小化”(变得非常小,不显示任何内容)和“最大化”(覆盖其全部内容)。我称它为ToggleBox。我已经做到了,使其表现得像一个容器,并且实现了切换功能,但是现在我面临一个难题。

您附加到ToggleBox的任何子控件都不会在运行时呈现。但是ToggleBox仍然表现得好像在那里(自动调整大小以覆盖所有不可见的内容)。我还尝试在其上绘制形状和线条,以查看其是否起作用,但我也无法使其起作用。图片:

This is how the ToggleBox looks like in my custom designer

这是我的自定义设计器中ToggleBox的外观

This is how the ToggleBox looks like in the form designer, with children controls attached (they attach properly)

这是ToggleBox在表单设计器中的外观,其中附加了子控件(它们已正确附加)

This is the ToggleBox during runtime. Notice that it resized itself to fit it's contents

这是运行时期间的ToggleBox。请注意,它会自动调整大小以适合其内容

代码:

    [Designer(typeof(ToggleBoxDesigner))]
public partial class ToggleBox : UserControl
{
    Panel panel;
    Pen pen;
    [Description("The text of the toggleBox")]
    public string Text
    {
        get { return groupBox.Text; }
        set { groupBox.Text = value; }
    }
    public bool On
    {
        get { return on; }
        set { if (value) { AutoSize = true; } else { AutoSize = false; Height = 25; } on = value; }
    }
    private bool on;

    public ToggleBox()
    {
        panel = new Panel();
        InitializeComponent();
        Text = "ToggleBox";
        Height = 100;
        button.Click += Toggle;
        Load += (object sender, EventArgs a) => { On = true; };

    }
    public void Toggle(object sender, EventArgs a)
    {
        On = !On;
    }

}

自定义设计师

[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
    public class ToggleBoxDesigner : ParentControlDesigner
    {

        public override void Initialize(System.ComponentModel.IComponent component)
        {
            base.Initialize(component);

            if (this.Control is ToggleBox)
            {
                this.EnableDesignMode(
                   ((ToggleBox)this.Control).DropZone, "DropZone");
            }
        }


    }

谢谢!

0 个答案:

没有答案