将控件拖放到自定义用户控件上将变得隐藏
我创建了一个自定义 UserControl,在设计时支持拖放控件。我的控件已正确放入我的用户控件,但是一旦放入用户控件,它们就会隐藏。为了使添加的控件变得可见,我必须选择它并单击设计时 IDE 按钮“Bring to Front”以查看添加的控件。当我重建解决方案时,控件再次隐藏。
我用以下代码重现了该问题。在 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");
}
}
}
更新:
我发现了另一个与设计时拖放控件问题相关的问题/答案将时间放在自定义用户控件内的容器控件上:
我按照上面的文章成功将工具箱控件拖放到容器控件上在设计时我的自定义用户控件中。
I created a custom UserControl, where I support dragging and dropping of controls at design time. 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. 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". This user control "MyControl" should then be dropped onto a Windows panel. Then, drag and drop another control, such as a Label or Buton control onto the user control and it becomes hidden.
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:
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");
}
}
}
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:
I followed the above article and successfully dragged and dropped toolbox controls onto a container control within my custom user control at design time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题其实很简单。您说您创建了一个 UserControl,然后向其中添加了一个 Panel 控件,该控件停靠以填充整个 UserControl。
因此,每当您在设计时向 UserControl 添加控件时,您都不会将它们添加到 Panel 控件,而是添加到 UserControl 本身。这导致它们被填充整个 UserControl 的 Panel 控件覆盖。
将它们放在前面是一个临时解决方案,因为这会将它们放置在面板控件的顶部。但是,它不会将它们放置在面板控件内,这就是为什么当您重建项目时它们会再次“消失”。它们实际上并没有消失,只是再次被面板控件隐藏,因为默认的 Z 顺序排列使它们位于面板下方。
目前还不清楚为什么需要面板控件。如果它填满了整个 UserControl,它似乎没有任何作用。 UserControl 已经是一个容器控件,因此您不需要面板。尝试将其从用户控件中删除,然后您可以在设计时添加所需的任何其他控件,而不会被停靠的面板覆盖。
如果您绝对必须有一个面板控件填充您的 UserControl,则需要添加一行代码,自动将设计时拖放到 UserControl 上的控件添加到 Panel 控件的 Controls 集合。
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.
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. That causes them to be covered up by the Panel control that fills the entire UserControl.
Bringing them to the front is a temporary solution, as that places them on top of the Panel control. It does not, however, place them inside the Panel control, which is why they "disappear" again when you rebuild the project. 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.
It's not clear why you need the Panel control at all. If it fills the entire UserControl, it doesn't seem to serve any purpose. A UserControl is already a container control, so you don't need a 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.
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.
您是否尝试过在设计时将用户控件设置到底部?
Have you tried setting your user control to the bottom at design time?