繁体   English   中英

将控件拖放到自定义用户控件上将变为隐藏状态

[英]Dragging and dropping controls onto a custom user control become hidden

我创建了一个自定义UserControl,我支持在设计时拖放控件。 我的控件正确地放入我的用户控件中,但是一旦放到用户控件上就会隐藏它们。 为了使添加的控件变得可见,我必须选择它并单击设计时IDE按钮“Bring to Front”以查看添加的控件。 当我重建解决方案时,控件再次隐藏。

我用以下代码重现了这个问题。 在IDE中我创建了一个简单的用户控件“MyControl”,我在其中添加了一个停靠在“Fill”的Panel控件。 然后应将此用户控件“MyControl”放到Windows面板上。 然后,将另一个控件(例如Label或Buton控件)拖放到用户控件上,它将变为隐藏状态。

以下是用户控件的代码。 如何在设计时将放入用户控件的控件自动置于最前面?

MyControl.Designer.cs:

namespace Test
{
    partial class MyControl
    {
        private System.ComponentModel.IContainer components = null;

        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            this.panel1 = new System.Windows.Forms.Panel();
            this.SuspendLayout();
            // 
            // panel1
            // 
            this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.panel1.Location = new System.Drawing.Point(0, 0);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(150, 150);
            this.panel1.TabIndex = 0;
            // 
            // MyControl
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Controls.Add(this.panel1);
            this.Name = "MyControl";
            this.ResumeLayout(false);

        }

        private System.Windows.Forms.Panel panel1;
    }
}

MyControl.cs:

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;
using System.Windows.Forms.Design;

namespace Axiom.Controls
{
    [Designer(typeof(ParentControlDesigner))]
    public partial class MyControl : UserControl
    {
        public MyControl()
        {
            InitializeComponent();
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public Panel ContentPanel
        {
            get
            {
                return this.panel1;
            }
        }
    }

    internal class MyControlDesigner : ControlDesigner
    {
        public override void Initialize(IComponent component)
        {

            base.Initialize(component);

            MyControl control = component as MyControl;

            EnableDesignMode(control.ContentPanel, "ContentPanel");
        }
    }
}

更新:

我发现在设计时将控件拖放到自定义用户控件中的容器控件上的问题的另一个相关问题/答案:

  1. 参考文章

我按照上面的文章,在设计时成功地将工具箱控件拖放到我的自定义用户控件中的容器控件上。

问题非常简单。 你说你已经创建了一个UserControl,然后为它添加了一个Panel控件,它被停靠以填充整个UserControl。

因此,无论何时在设计时向UserControl添加控件,您都不会将它们添加到Panel控件中,而是添加到UserControl本身。 这会导致它们被填充整个UserControl的Panel控件所掩盖

将它们放在前面是一个临时解决方案,因为它将它们置于 Panel控件之上 但是,它不会将它们放在 Panel控件中,这就是为什么它们在重建项目时会再次“消失”。 它们实际上并没有消失,它们只是再次被Panel控件隐藏,因为默认的Z顺序排列使它们位于Panel 下面

目前尚不清楚为什么你需要Panel控件。 如果它填满整个UserControl,它似乎没有任何用途。 UserControl已经是容器控件,因此您不需要Panel。 尝试从UserControl中删除它,然后您可以在设计时添加您想要的任何其他控件,而不会被停靠的面板覆盖。

如果您必须使用Panel控件填充UserControl,则需要添加一行代码,以便在设计时自动将要删除的控件添加到Panel控件的Controls集合中。

您是否尝试在设计时将用户控件设置到底部?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM