问题描述
我有一个自定义窗体(来自System.Windows.Forms类),可以在其中添加许多ZonePanel用户控件(从Panel控件派生),这些控件会将窗体分成多个区域(类似于ASP.NET的区域).区域控制).我在自定义表单上有一个属性,该属性实际上是ZonePanel类型的集合,因此可以将ZonePanel添加到表单.
可以,可以通过调出ZonePanel集合编辑器来正常工作.我可以添加任意数量的ZonePanel控件并设置其属性,但是,当我单击确定"时,除非关闭窗体并在Visual Studio中重新打开它,否则ZonePanels不会显示在窗体上.
我怀疑我需要为自定义表单创建一个设计器组件模型,但是我不知道要实现什么以使表单更新其子控件.
顺便说一句,FormX_Designer.cs表单包含我添加到集合中的控件的数量.
另外,作为另一个相关问题,有没有一种方法可以防止控件从设计图面上删除(即仅允许从集合编辑器中删除该控件)或相反地在集合编辑器中更新集合. >
任何帮助将不胜感激.
谢谢
安迪
(MCTS)
Hi,
I have a custom form (derived from the System.Windows.Forms class) whereby I can add a number of ZonePanel user controls (derived from the Panel control) which will split the form up into Zones (similar to that of the ASP.NET Zone control). I have a property on the custom form which is actually a collection of the type ZonePanel, whereby I can add the ZonePanel to the form.
This is OK and works fine by bringing up the ZonePanel collection editor. I can add any number of ZonePanel controls and set their properties, however, when I click OK, the ZonePanels are not shown on the form unless I close the form and reopen it in Visual Studio.
I am suspecting I need to create a designer component model for the custom form, but I don’t know what I need to implement to cause the form to update it’s child controls.
Incidentally, the FormX_Designer.cs form contains the number of controls that I added in the collection.
Also, as another related question, is there a way to either prevent a control from being deleted from the design surface (i.e. only allow the control to be removed from the collection editor) or conversely update the collection in the collection editor.
Any help would be most appreciated.
Thanks
Andy
(MCTS)
推荐答案
if(null != this.Parent)
this.Parent.Invalidate();
在Add
,AddRange
,Insert
等方法的末尾?
at the end of your Add
, AddRange
, Insert
etc methods?
public partial class DynamicForm : Form
{
#region Zone Collection Class
/// <summary>
/// Zone collection class.
/// </summary>
public class ZoneItems : CollectionBase
{
#region Constructor
// Define in here!
#endregion
#region Methods
/// <summary>
/// Gets or sets a zone by index.
/// </summary>
/// <param name="index">int: index of zone.</param>
/// <returns>ZonePanel: zone at index.</returns>
public ZonePanel this[int index]
{
get { return ((ZonePanel)List[index]); }
set { List[index] = value; }
}
/// <summary>
/// Adds a zone to the collection.
/// </summary>
/// <param name="zone">ZonePanel: zone to add.</param>
public void Add(ZonePanel zone)
{
List.Add(zone);
// Raise the zone added event.
this.raiseZoneAddedEvent(new EventArgs());
}
#endregion
#region Events
// Create an event delegate and an event.
public delegate void ZoneAddedEventHandler(object sender, EventArgs e);
/// <summary>
/// Occurs when a zone is added.
/// </summary>
public event ZoneAddedEventHandler ZoneAdded;
// Wrap raising the event in a method.
protected void raiseZoneAddedEvent(EventArgs e)
{
if (ZoneAdded != null)
{
ZoneAdded(this, e);
}
}
#endregion
}
#endregion
}
//自定义表单类
// The custom form class
public partial class DynamicForm : Form
{
// Property related fields.
protected ZoneItems pZoneRegion;
...
#region Event Handlers
// Handle the items zone added event handler.
protected virtual void Items_ZoneAdded(object sender, EventArgs e)
{
// Add the zone to the forms control collection.
this.Controls.Add(((ZoneItems)sender)[((ZoneItems)sender).Count - 1]);
}
#endregion
...
#region Properties
/// <summary>
/// Gets or sets the zone control collection.
/// </summary>
[Category("Design")]
[Description("Adds zone regions to the forms zone collection.")]
// Must serialize contents of the panel since it holds controls.
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public virtual ZoneItems ZoneRegion
{
get
{
return this.pZoneRegion;
}
set
{
this.pZoneRegion = value;
}
}
#endregion
}
因此,在DESIGN TIME中,直到在VS中关闭并重新打开表单后,添加的ZoneItems才会出现在表单上.
So in DESIGN TIME the ZoneItems added do not appear on the form until the form is closed in VS and re-opened.
这篇关于通过集合添加控件时更新表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!