简体   繁体   English

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

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

I created a custom UserControl, where I support dragging and dropping of controls at design time. 我创建了一个自定义UserControl,我支持在设计时拖放控件。 My controls are dropped into my user control correctly, however they are hidden once dropped onto the user control. 我的控件正确地放入我的用户控件中,但是一旦放到用户控件上就会隐藏它们。 To get the added control to become visible, I have to select it and click the design time IDE button "Bring to Front" to see the control added. 为了使添加的控件变得可见,我必须选择它并单击设计时IDE按钮“Bring to Front”以查看添加的控件。 When I rebuild the solution, the controls become hidden again. 当我重建解决方案时,控件再次隐藏。

I reproduced the issue with the following code. 我用以下代码重现了这个问题。 In the IDE I created a simple user control "MyControl", to which I added a single Panel control docked to "Fill". 在IDE中我创建了一个简单的用户控件“MyControl”,我在其中添加了一个停靠在“Fill”的Panel控件。 This user control "MyControl" should then be dropped onto a Windows panel. 然后应将此用户控件“MyControl”放到Windows面板上。 Then, drag and drop another control, such as a Label or Buton control onto the user control and it becomes hidden. 然后,将另一个控件(例如Label或Buton控件)拖放到用户控件上,它将变为隐藏状态。

Below is the code for the user control. 以下是用户控件的代码。 How can I get the controls that are dropped into the user control at design time be brought to the front automatically? 如何在设计时将放入用户控件的控件自动置于最前面?

MyControl.Designer.cs: 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: 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");
        }
    }
}

Update: 更新:

I found another related question/answer to this problem of dragging and dropping controls at design time onto a container control within the custom user control: 我发现在设计时将控件拖放到自定义用户控件中的容器控件上的问题的另一个相关问题/答案:

  1. Question
  2. Referenced Article 参考文章

I followed the above article and successfully dragged and dropped toolbox controls onto a container control within my custom user control at design time. 我按照上面的文章,在设计时成功地将工具箱控件拖放到我的自定义用户控件中的容器控件上。

The problem is really pretty simple. 问题非常简单。 You say that you've created a UserControl, then added a Panel control to it that is docked to fill the entire UserControl. 你说你已经创建了一个UserControl,然后为它添加了一个Panel控件,它被停靠以填充整个UserControl。

Thus, whenever you add controls to the UserControl at design time, you're not adding them to the Panel control, but instead the UserControl itself. 因此,无论何时在设计时向UserControl添加控件,您都不会将它们添加到Panel控件中,而是添加到UserControl本身。 That causes them to be covered up by the Panel control that fills the entire UserControl. 这会导致它们被填充整个UserControl的Panel控件所掩盖

Bringing them to the front is a temporary solution, as that places them on top of the Panel control. 将它们放在前面是一个临时解决方案,因为它将它们置于 Panel控件之上 It does not, however, place them inside the Panel control, which is why they "disappear" again when you rebuild the project. 但是,它不会将它们放在 Panel控件中,这就是为什么它们在重建项目时会再次“消失”。 They don't actually disappear, they just get hidden by the Panel control again, as the default Z order arrangement has them located underneath the Panel. 它们实际上并没有消失,它们只是再次被Panel控件隐藏,因为默认的Z顺序排列使它们位于Panel 下面

It's not clear why you need the Panel control at all. 目前尚不清楚为什么你需要Panel控件。 If it fills the entire UserControl, it doesn't seem to serve any purpose. 如果它填满整个UserControl,它似乎没有任何用途。 A UserControl is already a container control, so you don't need a Panel. UserControl已经是容器控件,因此您不需要Panel。 Try removing it from your UserControl, and then you can add whatever other controls you want at design time without them being covered up by the docked Panel. 尝试从UserControl中删除它,然后您可以在设计时添加您想要的任何其他控件,而不会被停靠的面板覆盖。

If you absolutely have to have a Panel control filling your UserControl, you need to add a line of code that automatically adds the control being dropped onto the UserControl at design time to the Controls collection for the Panel control. 如果您必须使用Panel控件填充UserControl,则需要添加一行代码,以便在设计时自动将要删除的控件添加到Panel控件的Controls集合中。

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

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

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