由于我目前正在处理的其他项目需要,因此我有一个程序集中的类。 一个类可以称之为Class Factory,创建一组需要附加点击事件处理程序的控件,我有基于某些特征来确定多态行为的算法,但这是相对无关紧要的。 由于事件处理程序必须打开一个不属于我的程序集的特定表单,并且该表单需要此“工厂”类。如果不创建循环引用,有什么办法可以基本上“委托”表单的处理程序事件来定义?将事件委托给表单而不创建循环引用
作为一个简单的解决方法,我不得不维护两个独立的类,一个在窗体项目中,另一个在程序集中。这显然是错误的路要走。
不是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
这不是我的实际代码,但它描绘的是我的问题JIST米面临,我不介意在C#或VB.NET中的答案。
请处理一个简单的虚构示例来说明您的问题。 –
@SteveDog我已经按要求添加了一个示例 – Tyson
您是否尝试将表单上存在的事件处理程序添加到childPanels上的事件?如果是这样,哪个事件? –