【问题标题】:Edit UserControl subclass in Windows Form Designer?在 Windows 窗体设计器中编辑 UserControl 子类?
【发布时间】:2011-09-26 17:31:50
【问题描述】:

在针对 Framework 4.0 的 VS2010 中,我有一个 UserControl,其中包含一个 TableLayoutPanel,第二行为空。

是否可以使用 Windows 窗体设计器将项目添加到第二行,还是我必须通过代码完成所有操作?

编辑:在回复 Hans Passant 的评论时,链接的问题引用了在另一个控件中使用的 UserControl。我在问一个关于继承控件的问题。

我创建了以下测试代码,其中 TestControl 是一个 UserControl,其中包含一个名为“TableLayoutPanel1”的 TableLayoutPanel:

Imports System.ComponentModel
Imports System.Windows.Forms.Design

<Designer(GetType(testDesigner))>
Public Class TestControl
End Class

Public Class testDesigner
    Inherits ControlDesigner
    Public Overrides Sub Initialize(component As System.ComponentModel.IComponent)
        MyBase.Initialize(component)
        Dim X As TestControl = CType(component, TestControl)
        EnableDesignMode(X.TableLayoutPanel1, "TableLayoutPanelX")
    End Sub
End Class

在新文件中创建以下类:

Public Class TestInheritedControl
    Inherits TestControl
End Class

创建一个控件,当在设计器中查看时,该控件包含一个名为“TableLayoutPanel1”的 TableLayoutPanel,它不是设计器可编辑的。这是我原来的问题中描述的情况,没有解决。

创建一个新的用户控件,而不是从任何一个以前的用户控件继承,然后将一个 TestControl 放入其中创建一个 TestControl 的实例,该实例具有一个名为“TestControl1.TableLayoutPanelX”的设计器可编辑的 TableLayoutPanel。

有什么方法可以让TestInheritedControl 类(如上所示)在设计器中可编辑?

【问题讨论】:

标签: .net vb.net winforms visual-studio-2010


【解决方案1】:

我今天只需要做类似的事情。我解决它的方法是在基类中使用未实例化的引用,但在继承类上进行实际实例化。

您必须添加几个步骤才能使所有内容都完全插件化,但它可以为您提供所需的控制。您必须将 TableLayoutPanel 拖放到每个继承控件类型上,并在构造函数中,将基类的版本设置为当前实例化版本(在调用 initializecomponents 之后)。

这样,您可以在继承控件中“设计”它,但可以从基本控件以编程方式使用它。

【讨论】:

    猜你喜欢
    • 2011-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-30
    相关资源
    最近更新 更多