返回首页


{S0}简介
本文提供了一个基类,帮助开发人员构建定制的扩展设计时,通过智能标记设计师面板组件和控制。如何使用这个助手类(基地)
您可以很容易地定义您的自定义智能标记面板,包含你的行动清单项目(属性,方法(动词)和文本)。下面是一个示例控制来自TextBox控制项,并使用自定义设计(mySmartTagActionList)。该项目被添加在AddActionItems()方法。应界定在您的SmartTagActionList从SmartTagActionListBase类派生的类,这是您要添加任何属性或方法。
在这里是必要的步骤来创建自定义控件设计师:定义您的控制(或组件)类(如XTextBox)定义DesignerActionList类继承从SmartTagActionListBase。这部分的代码也应该插入到它的身体:

Public Class mySmartTagActionList1

    Inherits SmartTagActionListBase



    'this variable is used to cache a reference 

    'to the associated control in this class



    Private m_Control As XTextBox 



    Sub New(ByVal component As IComponent)

        MyBase.New(component)

        'Me.AutoShow = True

        'AutoExpand this designer whenever user drag it to the Form



        m_Control = CType(component, XTextBox)

        'cache the control assciated to this designer



    End Sub

End Class

XTextBox是你的控制类型(或类名)。在这里,m_Control变量是用来缓存稍后使用此设计相关控制的参考。
您可能还可以使用此构造例程Me.AutoShow = TRUE自动扩展(自动显示),用户只要拖动到表单的控制。 定义你想要(例如,背景色,前景色,和IsMultiline,在下面的示例)的属性/方法。有些属性已经定义在基类(SmartTagActionListBase),如:名称,文字,字体,从右至左。 覆盖AddActionItems()方法来增加您的智慧标签面板的项目(ActionList项目)。您可以使用AddActionProperty,AddActionMethod,AddActionHeader,和AddActionText方法,轻松地做到这一点(见示例代码)。你这样的控制类设计器属性:{C}
mySmartTagActionList是你的SmartTagActionList类的名称。
注意:如果你想定义一个组件(不是一个控制)的设计师,只是使用而不是SmartTagControlDesigner SmartTagComponentDesigner:
<Designer(GetType(SmartTagComponentDesigner(Of mySmartTagActionList)))<
示例代码
下面是一个示例使用一个自定义SmartTagPanel设计器的控制:
Imports System.ComponentModel



<Designer(GetType(SmartTagControlDesigner(Of mySmartTagActionList)))> _

Public Class XTextBox

    Inherits TextBox



    '... customizations





End Class



Public Class mySmartTagActionList

    Inherits SmartTagActionListBase



    Private m_Control As XTextBox



    Sub New(ByVal component As IComponent)

        MyBase.New(component)

        m_Control = CType(component, XTextBox)

    End Sub



    Public Property BackColor() As Color

        Get

            Return m_Control.BackColor

        End Get

        Set(ByVal value As Color)

            Me.SetPropertyByName(m_Control, "BackColor", value)

        End Set

    End Property



    Public Property ForeColor() As Color

        Get

            Return m_Control.ForeColor

        End Get

        Set(ByVal value As Color)

            Me.SetPropertyByName(m_Control, "ForeColor", value)

        End Set

    End Property



    Public Property IsMultiline() As Boolean

        Get

            Return m_Control.Multiline

        End Get

        Set(ByVal value As Boolean)

            Me.SetPropertyByName(m_Control, "Multiline", value)

        End Set

    End Property



    Public Sub SwapColors()

        Dim c As Color = Me.ForeColor

        Me.ForeColor = Me.BackColor

        Me.BackColor = c

        RefreshDesigner()

    End Sub



    Public Overrides Sub AddActionItems()

        'These properties are already defined 

        'in base (SmartTagActionListBase) class:



        '  => Name, Text, Font, RightToLeft



        'Other properties/Methods should be defined in current class



        AddActionHeader("Main")

        AddActionProperty("Name", "Name:", "Main", "")

        AddActionProperty("Text", "Text:", "Main", "")

        AddActionProperty("Font", "Font:", "Main", "")

        AddActionProperty("IsMultiline", "Multiline:", "", "")

        AddActionHeader("Colors")

        AddActionProperty("ForeColor", "ForeColor:", _

                          "Colors", "Sets the ForeColor")

        AddActionProperty("BackColor", "BackColor:", _

                          "Colors", "Sets the BackColor")

        AddActionText("This is my info...", "Colors")

        AddActionMethod("SwapColors", "Swap Colors", _

                        "Colors", "Swap ForeColor/BackColor", True)



    End Sub



End Class
注释
你可以看到这篇文章:{A},营造UITypeEditor的的轻松。一个"编辑器"相关的属性,但一个"设计师"是关系到一个控件或组件(A类)!通过UITypeEditors手段,可以提供一个定制的编辑器窗口(例如,一个下拉窗口)编辑控件的每个属性。您可能混合的"设计"和"编辑器"的属性,以达到更好的设计时的组件和控件扩展。

回答

评论会员:aaroncampf 时间:2011/12/07
每次我生成项目的智能标记消失

这是为什么,我可以不使用它

它保持的组件,但不是你的Xtextbox
whats回事!!
我试图搞乱控制,并创建一个DLL,但只有XToolTip的特别标签
2011年1月13日(星期四),下午11:58
修改
评论会员:德里克-哈特 时间:2011/12/07
SwapColors控制在示例代码中,有一行代码RefreshDesigner() - 如果我把智能标签,并单击"交换颜色,然后单击"控制,然后单击控制和尝试再次带来了智能标记,它崩溃了。"未将对象引用设置到对象的实例"如果我删除RefreshDesigner,它没有错误,但设计师当然不会自动交换颜色。任何想法
评论会员:德里克哈特 时间:2011/12/07
如果我弹出一个控制的智能标签,并输入到属性无效的信息,弹出消息。起初它看起来像我可以覆盖的SetPropertyByName方法,因为这是一个错误报告。但是,事实并非如此。是否有一种方法来覆盖这样我就可以创建自己的错误消息框
评论会员:?magiel_van_gaalen 时间:2011/12/07
首先感谢很多代码,这是非常好的

我继承了面板,其"智能标记添加我的一些属性??原本只包含在父码头。这里一切工作正常。但"MoveSmartTag?易绕着我的面板,特别是因为即使在设计时,我可以切换故意通过点击我的透明控制,是消失了。我不知道正确的名称为"MoveSmartTag??但它是一个"箭头框智能标签??在右上角走动的控制。

我希望你帮助。在此先感谢。

最佳Regars,
Magiel来自荷兰
评论会员:会员2046625 时间:2011/12/07
我有同样的问题,我想通了,所以我将分享我的发现和解决方案
。默认情况下,小组使用一个PanelDesigner增加了"移动"图标,以及"在父母的Dock"的方法。然而,我们SmartTagControlDesigner从一个基本的ControlDesigner,没有这些新增项目。
继承这里是继承:(我用NET的反射工具)
PanelDesigner(添加面板图(小虚线))
继承
ScrollableControlDesigner(增加了滚动功能)
继承
ParentControlDesigner(添加移动图标和停靠在父)
继承
ControlDesigner(只具有基本功能)

最好的解决办法,以改变SmartTagControlDesigner代码,并从一个ControlDesigner继承,而不是从一个PanelDesigner继承。然而,令人惊讶的是,这将无法正常工作,因为PanelDesigner是宣布的朋友和无法访问。因此,未来最好的事情是继承从ScrollableControlDesigner(被声明为公共的和可访问的)。从ScrollableControlDesigner继承会给你回原来的"PanelDesigner"规定的一切,除了为虚线边界。

因此,要总结:
这部分代码:
Public Class SmartTagControlDesigner(Of DesignerActionList_Class As DesignerActionList)

	Inherits ControlDesigner

替换:
Public Class SmartTagControlDesigner(Of DesignerActionList_Class As DesignerActionList)

	Inherits ScrollableControlDesigner

(诺尔:您可能需要导入或添加System.Windows.Forms.Design和System.Design的引用)

最后,如​​果你真的想保留原设计的所有功能,你可以做什么,我没有和使用。NET的反射工具拆卸原PanelDesigner,复制并粘贴到您的项目代码。
PanelDesigner:

Imports System.ComponentModel

Imports System.ComponentModel.Design

Imports System.Windows.Forms.Design

Imports System.Drawing.Drawing2D

 

Public Class PanelDesigner

  Inherits ScrollableControlDesigner

  ' Methods

  Public Sub New()

    MyBase.AutoResizeHandles = True

  End Sub

  Protected Overridable Sub DrawBorder(ByVal graphics As Graphics)

    Dim component As Panel = DirectCast(MyBase.Component, Panel)

    If ((Not component Is Nothing) AndAlso component.Visible) Then

      Dim borderPen As Pen = Me.BorderPen

      Dim clientRectangle As Rectangle = Me.Control.ClientRectangle

      clientRectangle.Width -= 1

      clientRectangle.Height -= 1

      graphics.DrawRectangle(borderPen, clientRectangle)

      borderPen.Dispose()

    End If

  End Sub

  Protected Overrides Sub OnPaintAdornments(ByVal pe As PaintEventArgs)

    Dim component As Panel = DirectCast(MyBase.Component, Panel)

    If (component.BorderStyle = BorderStyle.None) Then

      Me.DrawBorder(pe.Graphics)

    End If

    MyBase.OnPaintAdornments(pe)

  End Sub

  ' Properties

  Protected ReadOnly Property BorderPen() As Pen

    Get

      Dim color As Color = IIf((Me.Control.BackColor.GetBrightness < 0.5), ControlPaint.Light(Me.Control.BackColor), ControlPaint.Dark(Me.Control.BackColor))

      Dim pen As New Pen(color)

      pen.DashStyle = DashStyle.Dash

      Return pen

    End Get

  End Property

 

End Class

然后,更改SmartTagControlDesigner:
Public Class SmartTagControlDesigner(Of DesignerActionList_Class As DesignerActionList)

	Inherits PanelDesigner


这实际上是一个好主意,任何人是创建一个自定义的控制,根据现有的控制。例如,如果你想创建一个自定义的TabControl基于现有的,你应该使用。NET的反射拆解TabControlDesigner,然后将其复制并粘贴到您的项目,然后创建另一个SmartTagControlDesigner继承TabControlDesigner

我觉得我只是写我自己的文章!对不起,这个答复是如此冗长
评论会员:!sabrown100 时间:2011/12/07
这是一个真正伟大的文章。你能提供一个C#版本吗?
评论会员:bmwgamil 时间:2011/12/07

我建立一个复合控件,其中包含一些custoemr Web控件。我需要建立智能标记控制的是一个或每一个separte?

感谢。
评论会员:游客 时间:2011/12/07
KristipatiSubramanyam
您好,
&# 160; 我Kristipati Subramanyam,我创建多列组合框。其做工精细。
我想添加设计时支持我的控制。为此我在MSDN / CodeProject上搜索。
相比之下,你的解决方案是简单的implemention容易。
但是,当我试图使用您的解决方案添加智能标记,我无法得到智能标记。
为什么呢?
请告诉我,如果有任何特殊照顾,我们需要照顾自定义控件添加智能标记,从ComboBox的继承


Kristipati
评论会员:DevDiver 时间:2011/12/07
您好,

我按照你的文章,现在我有我的控制与智能标记,就像我想,但现在我的控制与自定义UITypeEditor的(打开一个与电网导航,选择,...).的形式属性(字符串)我要像它在属性网格中显示的智能标记,但与自定义UITypeEditor的财产,不只是文本框。

我怎样才能做到这一点。

在此先感谢,
D
评论会员:DevDiver 时间:2011/12/07
您可以这样做,以及你做你的控制,即,为它指定的编辑器属性的属性。这里是一个例子使用myEditor1 myProperty在智能标签设计师命名的属性:

 

Public Class mySmartTagActionList1

	Inherits SmartTagActionListBase

 

	...

 

	'This property uses our custom UITypeEditor:

	<EditorAttribute(GetType(myEditor1), _

	GetType(System.Drawing.Design.UITypeEditor)) , DefaultValue("")> _

	Public Property myProperty() As String

		...

	End Property

 

	...

 



	Public Overrides Sub AddActionItems()

		...

		AddActionProperty("myProperty", "myProperty:", "", "")

	End Sub

 

End Class

 


评论会员:恩里克卡瓦略 时间:2011/12/07
完成!
感谢您的关注很多,

D
评论会员:恩里克卡瓦略 时间:2011/12/07
好文章,它帮助我很多

我想有一个像一个在DataGridView smartTike,我的意思是自动,尽快展开面板控制下降到设计。我该怎么办呢?

帮助真的很感激
评论会员:babakzawari 时间:2011/12/07
您可以通过设计师的构造函数中自动显示属性设置为True。例如:

Public Class MyDesigner

	Inherits XControlDesignerActionList

 

	Private myControl As XTextBox

 

	Public Sub New(ByVal component As IComponent)

		MyBase.New(component)

		Me.AutoShow = True '*** add this code

		'...

	End Sub

	...

End Class


- 修改,于10时14分星期一22nd一月,2007
评论会员:Saadz 时间:2011/12/07

我一直在寻找myControlDesigner
的属性
非常感谢
HC
评论会员:ahmed45 时间:2011/12/07
谢谢您的热门文章

(Dastet准nakone kheily khob BOD)
巴巴克Zawari