我想创建我自己的具有设计时支持的 TabControl 类。
这是我的设计师:
public class TabListDesigner : ParentControlDesigner
{
protected TabList TabListControl { get { return this.Control as TabList; } }
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case 0x7b: // WM_CONTEXTMENU
this.OnContextMenu(Cursor.Position.X, Cursor.Position.Y);
break;
default:
base.WndProc(ref m);
break;
}
}
protected override bool GetHitTest(Point point)
{
return this.TabListControl.HitTest(this.TabListControl.PointToClient(point)) != null;
}
protected override void OnPaintAdornments(PaintEventArgs pe)
{
base.OnPaintAdornments(pe);
ControlPaint.DrawFocusRectangle(pe.Graphics, this.Control.ClientRectangle);
}
public override void InitializeNewComponent(IDictionary defaultValues)
{
base.InitializeNewComponent(defaultValues);
this.AddTabListPage();
this.AddTabListPage();
}
protected virtual void AddTabListPage()
{
IDesignerHost host = (IDesignerHost)this.GetService(typeof(IDesignerHost));
if (host != null)
{
using (DesignerTransaction transaction = host.CreateTransaction(string.Format("Add TabListPage to '{0}'", this.TabListControl.Name)))
{
try
{
TabListPage page = (TabListPage)host.CreateComponent(typeof(TabListPage));
MemberDescriptor controlsProperty = TypeDescriptor.GetProperties(this.TabListControl)["Controls"];
this.RaiseComponentChanging(controlsProperty);
this.TabListControl.Add(page);
this.TabListControl.Controls.Add(page);
this.RaiseComponentChanged(controlsProperty, null, null);
transaction.Commit();
}
catch
{
transaction.Cancel();
throw;
}
}
}
}
}
在设计器中,我将 Tabcontrol 添加到表单中,并且 2 TabPages 正确显示。现在我测试项目,2 个标签页消失了。我回到设计师那里,标签页已经不存在了。为什么?