【发布时间】: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");
}
}
}
更新:
我发现另一个相关问题/答案是在设计时将控件拖放到自定义用户控件中的容器控件上:
我按照上面的文章,在设计时成功地将工具箱控件拖放到我的自定义用户控件中的容器控件上。
【问题讨论】:
标签: c# winforms user-controls drag-and-drop design-time