Vb.net 如何在ControlDesigner中获取继承的表单类型
我正在构建一个自定义设计器,它将控件与表单上的业务属性相关联。表单DealUI具有属性Instrument和Product,它们是一个业务项:Vb.net 如何在ControlDesigner中获取继承的表单类型,vb.net,winforms,inheritance,designer,Vb.net,Winforms,Inheritance,Designer,我正在构建一个自定义设计器,它将控件与表单上的业务属性相关联。表单DealUI具有属性Instrument和Product,它们是一个业务项: Public Class DealUI Inherits System.Windows.Forms.Form ' repetition of Inherits in Deal.Designed.vb, just to make the point Sub New() InitializeComponent
Public Class DealUI
Inherits System.Windows.Forms.Form ' repetition of Inherits in Deal.Designed.vb, just to make the point
Sub New()
InitializeComponent()
End Sub
<Business(True)> _
Public Property Product As String
<Business(True)> _
Public Property Instrument As String
End Class
该表单包含一个自定义控件,即PilotTextBox类型的ProductTextBox:
<DesignerAttribute(GetType(PilotControlDesigner)), _
ToolboxItem(GetType(PilotToolboxItem))> _
Public Class PilotTextBox
Inherits TextBox
Public Property Source As String
End Class
所选控件是一个交易(从表单继承),但设计器中的组件是一个表单,而不是一个交易(!!在注释中)。我需要检查仪器和产品属性,它们只存在于交易中
如何在设计器中获得Deal对象?请改用Me.Control。@hans谢谢,但是Me.Control还返回Form类型的对象,而不是type类型的对象DealUI@hans我希望我能:(我觉得我在某个地方出丑,但我看不到>;-)问题是您试图在设计器中获取至少一个实例的数据。我得到了一些你似乎想要工作的东西。(即,将表单中定义的属性链接到某些控件)。让我知道,如果你仍然在这方面的工作,我会张贴一个新的答案。
<DesignerAttribute(GetType(PilotControlDesigner)), _
ToolboxItem(GetType(PilotToolboxItem))> _
Public Class PilotTextBox
Inherits TextBox
Public Property Source As String
End Class
Public Class PilotControlDesigner
Inherits ControlDesigner
Private Sub InitializeServices()
Me.selectionService = GetService(GetType(ISelectionService))
If (Me.selectionService IsNot Nothing) Then
AddHandler Me.selectionService.SelectionChanged, AddressOf selectionService_SelectionChanged
End If
End Sub
Private Sub selectionService_SelectionChanged(ByVal sender As Object, ByVal e As EventArgs)
If Me.selectionService IsNot Nothing Then
If Me.selectionService.PrimarySelection Is Me.Control Then
Dim form As Object = DesigningForm()
If form IsNot Nothing Then
For Each prop As PropertyInfo In form.GetType.GetProperties
Dim attr As Attribute = GetCustomAttribute(prop.ReflectedType, GetType(BusinessAttribute), False)
If attr IsNot Nothing Then
' we've found a Business attribute
End If
Next
End If
End If
End If
End Sub
Private Function DesigningForm() As Object ' in fact, a form, or more precisely something that inherits from Form
Dim host As IDesignerHost = CType(Me.Component.Site.GetService(GetType(IDesignerHost)), IDesignerHost)
Dim container As IContainer = host.Container
For Each comp As Component In container.Components
If comp.GetType.IsAssignableFrom(GetType(Form)) Then ' or anything that inherits 'Form'
return comp ' returns a Form, not a Deal!!
End If
Next comp
Return nothing
End Function
End Class