我使用UITypeEditor在一个项目中创建了一个自定义组件。在组件类中,我有一个属性MyFormSettings:它是一个打开的表单。它有一个组合框,显示条目组件中的所有表单名称。 在测试项目中,我在工具箱中添加了自定义组件,当我将组件拖放到测试项目新表单时。我可以使用Myformsettings属性获取属性窗口。但是我没有设置错误对象引用 类型formType = typeof(表格); 汇编myAssembly = Assembly.GetEntryAssembly(); foreach(myAssembly.GetTypes()中的类型类型) { // foreach(类型类型)在类型中) if(formType.IsAssignableFrom(type)) { ComboBox1.Items.Add(type.Name); } } 如何在设计中获得Entry组件时间。? 我尝试了什么: 我得到了无法在设计时获取条目汇编信息。
解决方案这是VB中的代码,允许组件检测它的Parentform(在设计时)。 br /> Imports System.ComponentModel Imports System.ComponentModel.Design Imports System.Runtime.CompilerServices
组件本身实现ISupportInitialize
实现 ISupportInitialize 公共 Sub BeginInit() Implements ISupportInitialize。 BeginInit 结束 Sub Public Sub EndInit() Implements ISupportInitialize.EndInit GetParentForm( ) 结束 Sub#Region检测ParentForm 私有 Sub GetParentForm() Dim myHost As IDesignerHost = Nothing 如果 MyBase .Site IsNot 没什么 然后 myHost = CType ( MyBase .Site.GetService( GetType (IDesignerHost)),IDesignerHost) 如果 myHost IsNot Nothing 然后 myParentForm = CType (myHost.RootComponent,Form) 退出 Sub 结束 如果 结束 Sub 公共 覆盖 属性 Site() As System.ComponentModel.ISite 获取 返回 MyBase .Site 结束 获取 设置( ByVal 值 As System.ComponentModel.ISite) MyBase .Site =价值 GetParentForm() 结束 设置 结束 属性 <类别( Info-Data),Description( 组件的Parentform )GT; 属性 ParentForm()作为表单 获取 返回 myParentForm 结束 获取 设置(值作为表单) myParentForm = value 结束 设置 结束 属性 私有 WithEvents myParentForm As 表格 #End Region
I have created a custom component in one project with UITypeEditor. In component class I have a one attribute MyFormSettings: its a opens form. it has one combobox which displays all the forms name in the entry assembly. In Test Project I have added the custom component in toolbox and when i drag and drop the component to the test project new form. I can get the properties window with Myformsettings property. but I am getting error object reference not set Type formType = typeof(Form); Assembly myAssembly = Assembly.GetEntryAssembly(); foreach (Type type in myAssembly.GetTypes()) { // foreach (Type type in types) if (formType.IsAssignableFrom(type)) { ComboBox1.Items.Add(type.Name); } } How can i get the Entry assembly in the design time.? What I have tried: I am getting the not able to get the entry assembly info in design time.
解决方案 Here is the Code in VB which allows a Component to detect it's Parentform (during Designtime). Imports System.ComponentModel Imports System.ComponentModel.Design Imports System.Runtime.CompilerServicesthe Component itself implements ISupportInitialize
Implements ISupportInitialize Public Sub BeginInit() Implements ISupportInitialize.BeginInit End Sub Public Sub EndInit() Implements ISupportInitialize.EndInit GetParentForm() End Sub#Region "Detect the ParentForm" Private Sub GetParentForm() Dim myHost As IDesignerHost = Nothing If MyBase.Site IsNot Nothing Then myHost = CType(MyBase.Site.GetService(GetType(IDesignerHost)), IDesignerHost) If myHost IsNot Nothing Then myParentForm = CType(myHost.RootComponent, Form) Exit Sub End If End Sub Public Overrides Property Site() As System.ComponentModel.ISite Get Return MyBase.Site End Get Set(ByVal value As System.ComponentModel.ISite) MyBase.Site = value GetParentForm() End Set End Property <Category("Info-Data"), Description("the Parentform of the Component")> Property ParentForm() As Form Get Return myParentForm End Get Set(value As Form) myParentForm = value End Set End Property Private WithEvents myParentForm As Form #End Region
更多推荐
如何在C#的设计时间内获得入口汇编?
发布评论