【问题标题】:Dragging and dropping controls onto a custom user control become hidden将控件拖放到自定义用户控件上变得隐藏
【发布时间】:2011-08-07 13:41:08
【问题描述】:

我创建了一个自定义 UserControl,我支持在设计时拖放控件。我的控件已正确放入我的用户控件中,但是一旦放入用户控件中,它们就会隐藏起来。要使添加的控件可见,我必须选择它并单击设计时 IDE 按钮“置于前面”以查看添加的控件。当我重建解决方案时,控件再次隐藏。

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

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

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. Question
  2. Referenced Article

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

【问题讨论】:

    标签: c# winforms user-controls drag-and-drop design-time


    【解决方案1】:

    这个问题真的很简单。您说您创建了一个 UserControl,然后向其中添加了一个 Panel 控件,该控件停靠以填充整个 UserControl。

    因此,每当您在设计时向 UserControl 添加控件时,您不是将它们添加到 Panel 控件,而是将其添加到 UserControl 本身。这会导致它们被填充整个 UserControl 的 Panel 控件覆盖

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

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

    如果您绝对必须有一个 Panel 控件填充您的 UserControl,您需要添加一行代码,该代码会自动将在设计时拖放到 UserControl 上的控件添加到 PanelControls 集合中/em> 控制。

    【讨论】:

    • 谢谢!那解决了它。我不需要面板,并且看到我现在也不再需要内部的“MyControlDesigner”类。我从另一篇关于此主题的在线文章中提取了此代码,并希望将控件添加到特定的面板中。我曾假设此行导致面板接收拖动的控件:EnableDesignMode(control.ContentPanel, "ContentPanel");
    • @Elan:我怀疑这不起作用,因为您没有通过站点将INestedContainer 类作为服务公开。 documentation 有更多详细信息。但总的来说,如果可能的话,您应该避免编写自定义设计器。这是一个记录很少的魔法领域,很难做到正确。
    • 在“正常”情况下(未实施设计工具):一个新的用户控件,其中有一个面板,'Dock 属性设置为'Fill,将成为其他控件的父控件拖放到用户控件。有时使用填充 UserControl 的 Panel 有一些很好的理由:通过设置 Margin、Padding,您可以在 Panel 周围创建一个边框,以便当 UserControl 的多个实例被放入 Form 上的某个其他容器中时,这些实例的 'Dock 属性设置为 'Top,它们并非全部“卡住”在一起,没有可见的边框。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-31
      • 1970-01-01
      • 1970-01-01
      • 2018-12-03
      相关资源
      最近更新 更多