自定义控件设计时

时间:2013-03-15 15:36:36

标签: c# .net winforms

我想用设计时支持创建我自己的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个Tabpages消失了。我回到设计师那里,Tabpages不再存在了。为什么?

1 个答案:

答案 0 :(得分:0)

我曾经创建过一个控件设计器,而不是表单设计器使用的控件设计器,但更像是单个控件的实际表单设计器。我不记得该项目,但我确实记得有同样的问题,并且我不得不提醒视觉工作室,我已经以某种方式改变了一个值。

保存设计器选项卡后,Visual Studio将继续并根据语言将更改转换为代码,并将其写入表单的设计器文件中。您也可以使用codeDom编写自己的特定代码生成器,但这是非常先进的。

这不是你想要的,但我打赌这个问题是相关的,所以请耐心等待。我能在这个领域找到的唯一例子是一个名为“Shape designer”的东西c#项目。我是从浅蓝色的网站上得到的。我做了一些搜索,但我找不到页面。无论如何,代码都有详细记录,他完全解释了你的问题。

我也有我的项目,这是一个标签式的东西,当你转到另一个页面时,它会在页面之间激活。它在vb.net中,没有详细记录,但你可能想要一瞥。

Here's the projecthere's a image of the designer,和 animated gif of the end result

编辑:

在我的项目中,在“DesignerControl.vb”文件中,第187行:

' The controldesigner don't always save properties which are changed directly :/
Private Sub SetValue(itm As Item, pName$, val As Object)
    Dim Prop As PropertyDescriptor = TypeDescriptor.GetProperties(itm.Page)(pName)
    If Prop IsNot Nothing AndAlso Prop.PropertyType Is val.GetType Then Prop.SetValue(itm.Page, val)
End Sub