簡體   English   中英

自定義控件設計時

[英]custom control design-time

我想用設計時支持創建我自己的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不再存在了。 為什么?

我曾經創建過一個控件設計器,而不是表單設計者使用的控件設計器,但更像是單個控件的實際表單設計器。 我不記得該項目,但我確實記得有同樣的問題,我不得不提醒視覺工作室,我已經以某種方式改變了一個值。

保存設計器選項卡后,Visual Studio將繼續並根據語言將更改轉換為代碼,並將其寫入表單的設計器文件中。 您也可以使用codeDom編寫自己的特定代碼生成器,但這是非常先進的。

這不是你想要的,但我打賭這個問題是相關的,所以請耐心等待。 我能在這個領域找到的唯一例子是一個名為“Shape designer”的東西c#項目。 我是從淺藍色的網站上得到的。 我做了一些搜索,但我找不到頁面。 無論如何,代碼都有很好的文檔記錄,他完全解釋了你的問題。

我也有我的項目,這是一個標簽,當你去另一個頁面時,它會在頁面之間激活。 它在vb.net中,沒有詳細記錄,但你可能想要一瞥它。

這是項目這里是設計師的圖像 ,以及最終結果動畫GIF

編輯:

在我的項目中,在“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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM