我有一个名为factory的类,它继承了一个名为Block的类。块依次继承对象面板。在每个工厂内,我有3个生产按钮面板。单击生产按钮时,将触发生产计时器。
我的问题是,当我点击,例如,在我的混凝土工厂生产1分钟时,我的钢铁生产就开始了。所有工厂的所有按钮都是一样的;它们都会引发钢铁生产。
我需要知道的是,当我在混凝土工厂点击我的1分钟按钮时,我如何确定它来自混凝土工厂的按钮,而不是钢制工具?
以下是工厂类按钮的处理程序事件:
min = New Block(Me, 0, 0, 20, 20, Color.Maroon, My.Resources._1min)
hour = New Block(Me, 40, 0, 20, 20, Color.Maroon, My.Resources._1hour)
fifteenHour = New Block(Me, 80, 0, 20, 20, Color.Maroon, My.Resources._15hour)
AddHandler min.Click, AddressOf startProduction1min
AddHandler hour.Click, AddressOf startProduction1hour
AddHandler fifteenHour.Click, AddressOf startProduction15hour
这是我的startProduction1min()
Sub startProduction1min(ByVal sender As Object, ByVal e As EventArgs)
If stlFac.Bounds.Contains(sender.Bounds) Then
prodCost = (1 / 30)
If Not prodCost > goldVol Then
goldVol -= prodCost
Else
Return
End If
startProduction(steelProduction, 60)
ElseIf conFac.Bounds.Contains(sender.Bounds) Then
prodCost = (1 / 60)
If Not prodCost > goldVol Then
goldVol -= prodCost
Else
Return
End If
startProduction(concreteProduction, 60)
ElseIf gldFac.Bounds.Contains(sender.Bounds) Then
prodCost = (0.005)
If Not prodCost > goldVol Then
goldVol -= prodCost
Else
Return
End If
startProduction(goldProduction, 60)
End If
End Sub
问题似乎在于:
If stlFac.Bounds.Contains(sender.Bounds) Then
有什么建议吗?
由于
答案 0 :(得分:1)
这看起来不太好, Factory 中的事件处理程序一定不能知道程序可能使用的不同Factory实例。这里需要的是Factory在单击块时引发事件。这可能看起来像这样:
Public Class Factory
Public Class ProduceDetail
Inherits EventArgs
Public Property Interval As Integer
End Class
Public Event Produce As EventHandler(Of ProduceDetail)
Public Sub New()
'' Your Block initialization code here...
End Sub
Sub startProduction1min(ByVal sender As Object, ByVal e As EventArgs)
Dim arg = New ProduceDetail With {.Interval = 1}
RaiseEvent Produce(Me, arg)
End Sub
Sub startProduction1hour(ByVal sender As Object, ByVal e As EventArgs)
Dim arg = New ProduceDetail With {.Interval = 3600}
RaiseEvent Produce(Me, arg)
End Sub
'' etc..
End Class
现在,客户端代码可以简单地订阅 Produce 事件并实施相应的操作。