简介
这是现成的使用它可以帮助您创建你自己的设计时间控件属性编辑在下拉一个ModalForm窗口或在Visual Studio IDE中的设计模式UITypeEditors的代码。要使用它,从它继承的类,将该属性添加到您的控制项属性(IES):<Editor(GetType(MyPropertyEditor),
GetType(System.Drawing.Design.UITypeEditor))>
有些样品在下面的图像所示:
{S0}{S1}图1)使用一个ListBox编辑器图2)使用TreeView编辑
{S2}图3)使用UserControl的编辑器{S3}图4)使用模态窗体编辑器"PropertyEditorBase"类
这是一个抽象(MustInherit)类,这是自己从System.Drawing.Design.UITypeEditor继承。使用UITypeEditor类,因为需要一些特殊的代码,我们可以把他们到一个基类(辅助)和更容易使用它。
例如当用户按下Esc键,在编辑过程中应取消以前的属性值应返回到IDE的PropertyGrid。或者你总是需要一个类似的代码行IWindowsFormsEditorService服务:
这些工作对于开发人员来说,谁愿意写一个UITypeEditor的迅速不那么明确。我不会多讲这只是提供了基本的辅助类,你可以用它来简化这个过程。 (如果你想看到只是如何使用它,看到的文章。)
下面是代码:
如何使用Imports System.Drawing
Imports System.Drawing.Design
Imports System.Windows.Forms
Imports System.Windows.Forms.Design
Imports System.ComponentModel
''' <summary>
''' This is a UITypeEditor base class usefull for simple editing of control
''' properties in a DropDown or a ModalForm window at design mode (in
''' Visual Studio IDE). To use this, inherits a class from it and add this
''' attribute to your control property(ies):
''' <Editor(GetType(MyPropertyEditor),
''' GetType(System.Drawing.Design.UITypeEditor))>
''' </summary><
/span>
Public MustInherit Class PropertyEditorBase
Inherits System.Drawing.Design.UITypeEditor
''' <summary>
''' The driven class should provide its edit Control to be shown in the
''' DropDown or DialogForm window by means of this function.
''' If specified control be a Form, it is shown in a Modal Form, otherwise
''' in a DropDown window. This edit control should return its final value
''' at GetEditedValue() method.
''' </summary>
Protected MustOverride Function GetEditControl(ByVal PropertyName As _
String, ByVal CurrentValue As Object) As Control
''' <summary>The driven class should return the New Edited Value at this
''' function.</summary>
''' <param name="EditControl">
''' The control shown in DropDown window and used for editing.
''' This is the control you pass in GetEditControl() function.
''' </param>
''' <param name="OldValue">The original value of the property before
''' editing</param>
Protected MustOverride Function GetEditedValue(ByVal EditControl As _
Control, ByVal PropertyName As String, _
ByVal OldValue As Object) As Object
Protected IEditorService As IWindowsFormsEditorService
Private WithEvents m_EditControl As Control
Private m_EscapePressed As Boolean
''' <summary>
''' Sets the edit style mode based on the type of EditControl: DropDown or
''' Modal(Dialog).
''' Note that the driven class can also override this function and
''' explicitly set its value.
''' </summary>
Public Overrides Function GetEditStyle(ByVal context As _
ITypeDescriptorContext) As UITypeEditorEditStyle
Try
Dim c As Control
c = GetEditControl(context.PropertyDescriptor.Name, _
context.PropertyDescriptor.GetValue(context.Instance))
If TypeOf c Is Form Then
Return UITypeEditorEditStyle.Modal 'Using a Modal Form
End If
Catch
End Try
'Using a DropDown Window (This is the default style)
Return UITypeEditorEditStyle.DropDown
End Function
'Displays the Custom UI (a DropDown Control or a Modal Form) for value
'selection.
Public Overrides Function EditValue(ByVal context As ITypeDescriptorContext,_
ByVal provider As IServiceProvider, ByVal value As Object) As Object
Try
If context IsNot Nothing AndAlso provider IsNot Nothing Then
'Uses the IWindowsFormsEditorService to display a drop-down
'UI in the Properties window:
IEditorService = DirectCast( _
provider.GetService(GetType(IWindowsFormsEditorService)), _
IWindowsFormsEditorService)
If IEditorService IsNot Nothing Then
Dim PropName As String = context.PropertyDescriptor.Name
'get Edit Control from driven class
m_EditControl = Me.GetEditControl(PropName, value)
If m_EditControl IsNot Nothing Then
m_EscapePressed = False
'Show given EditControl
If TypeOf m_EditControl Is Form Then
IEditorService.ShowDialog(CType(m_EditControl, Form))
Else
IEditorService.DropDownControl(m_EditControl)
End If
If m_EscapePressed Then 'return the Old Value
'(because user press Escape)
Return value
Else 'get new (edited) value from driven class and
'return it
Return GetEditedValue(m_EditControl, PropName, value)
End If
End If 'm_EditControl
End If 'IEditorService
End If 'context And provider
Catch ex As Exception
'we may show a MessageBox here...
End Try
Return MyBase.EditValue(context, provider, value)
End Function
''' <summary>
''' Provides the interface for this UITypeEditor to display Windows Forms
''' or to display a control in a DropDown area from the property grid
''' control in design mode.
''' </summary>
Public Function GetIWindowsFormsEditorService() As _
IWindowsFormsEditorService
Return IEditorService
End Function
''' <summary>Close DropDown window to finish editing</summary>
Public Sub CloseDropDownWindow()
If IEditorService IsNot Nothing Then IEditorService.CloseDropDown()
End Sub
Private Sub m_EditControl_PreviewKeyDown(ByVal sender As Object, _
ByVal e As PreviewKeyDownEventArgs) _
Handles m_EditControl.PreviewKeyDown
If e.KeyCode = Keys.Escape Then m_EscapePressed = True
End Sub
End Class
它有两个MustOverride(摘要)应驱动的类定义的函数。它们是:Function GetEditControl(ByVal PropertyName As String, _
ByVal CurrentValue As Object) As Control
Function GetEditedValue(ByVal EditControl As Control, _
ByVal PropertyName As String, _
ByVal OldValue As Object) As Object
首先应返回要使用属性编辑器窗口的控制(例如,一个简单的列表框)和第二次下版应该返回属性的新值。驱动类可以使用这些函数的参数信息,返回正确的值。例如,ListBox中的样本,初始项目的下拉列表框选择的基础上的财产CurrentValue。ListBox的编辑示例
下面是一个示例控制使用这个辅助基类定义为"myProperty"的属性编辑:Imports System.ComponentModel
Public Class XTextBoxA
Inherits TextBox
Private m_myProperty As String = ""
'This property uses our custom UITypeEditor: myListBoxPropertyEditor
<EditorAttribute(GetType(myListBoxPropertyEditor),
GetType(System.Drawing.Design.UITypeEditor))> _
Public Property myProperty() As String
Get
Return m_myProperty
End Get
Set(ByVal value As String)
m_myProperty = value
End Set
End Property
End Class
' ////////////////////////////////////////////////////////////////////////////
' myListBoxPropertyEditor => using a ListBox as EditControl<
/span>
' ////////////////////////////////////////////////////////////////////////////
'
Public Class myListBoxPropertyEditor
Inherits PropertyEditorBase
Private WithEvents myListBox As New ListBox 'this is the control to be used
'in design time DropDown editor
Protected Overrides Function GetEditControl(ByVal PropertyName As String, _
ByVal CurrentValue As Object) As Control
myListBox.BorderStyle = System.Windows.Forms.BorderStyle.None
'Creating ListBox items...
'Note that as this is executed in design mode, performance is not
'important and there is no need to cache listbox items if they can
'change each time.
myListBox.Items.Clear() 'clear previous items if any
span>
myListBox.Items.Add("AAA")
'... Add other items...
'Select current item based on CurrentValue of the property:
myListBox.SelectedIndex = myListBox.FindString(CurrentValue)
myListBox.Height = myListBox.PreferredHeight
Return myListBox
End Function
Protected Overrides Function GetEditedValue(ByVal EditControl As Control, _
ByVal PropertyName As String, ByVal OldValue As Object) As Object
Return myListBox.Text 'return new value for property
End Function
Private Sub myTreeView_Click(ByVal sender As Object, ByVal e As _
System.EventArgs) Handles myListBox.Click
Me.CloseDropDownWindow() 'when user clicks on an item, the edit
'process is done.
End Sub
End Class
你应该调用CloseDropDownWindow方法将在适当的事件下拉编辑器窗口。这是为ListBox ListBox的"Click"事件,而是一个TreeView,它可能是在TreeView"的DblClick"事件。注意:
您可以使用这个基类定义编辑器使用任何特殊的控制。例如,你可以定义一个自定义的UserControl设置一个属性,它控制你的主要控制图形视图显示一些图像(而不是一个文本式的枚举列表)。
下载的源代码,看到所有四个样品图1到图4另请参见:帮助开发人员构建定制的扩展设计时,通过智能标记设计师面板组件和控制。