由于我目前正在从事的其他项目需要,我有一个在程序集中的课程。一个类可以称之为类工厂,创建一组需要附加单击事件处理程序的控件,我有基于某些特性确定多态行为的算法,但这相对无关紧要。由于事件处理程序必须打开一个不属于我的程序集的特定表单,并且该表单需要这个“工厂”类。在不创建循环引用的情况下,有什么方法可以本质上“委托”处理程序事件以定义表单?
作为一项简单的工作,我必须维护两个单独的类,一个在项目中使用表单,一个在程序集中。这显然是错误的做法。
不是 100% 确定标题,因为我知道我正在尝试做什么,但不确定如何去做。
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Width = 1500
Dim panel As New Panel
panel.Location = New Point(0, 0)
panel.Size = New Size(1000, 200)
panel.BackColor = Color.Yellow
Me.Controls.Add(panel)
Dim factory As New DrawFactory
factory.DrawPanel(panel, 5)
End Sub
以上与类库中的以下代码在一个单独的项目中:
Public Class DrawFactory
Private xOffset As Integer = 0
Private Const widthConst As Integer = 50
Public Sub DrawPanel(ByRef panel As Panel, ByVal drawPanels As Integer)
Application.DoEvents()
For i As Integer = 0 To drawPanels
Dim childPanel As New Panel
childPanel.Location = New Point(xOffset, 0)
childPanel.Size = New Size(widthConst, panel.Height)
If i < 1 Then
childPanel.BackColor = Color.Blue
Else
childPanel.BackColor = Color.Pink
End If
xOffset = xOffset + widthConst
''want to add a handler here to open say form5 which is unknown and undefined in the scope of this class
panel.Controls.Add(childPanel)
Next
End Sub
End class
这不是我的实际代码,但它描述了我面临的问题的要点,我不介意用 C# 或 VB.NET 给出答案。