2015-04-18 39 views
0

我有一个名为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 

有什么建议么?

感谢

回答

1

这看起来并不好,在事件处理中不能了解不同工厂的实例任何程序可能用途。这里需要的是当一个Block被点击时,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 

现在客户端代码可以简单地认购农产品事件并执行相应的动作。

+0

我很困惑,我可以如何实现这个到我的代码。 startProduction1min()是定时器启动的事件处理程序。它与工厂分开。我如何获得您提供给我的代码以启动计时器? –

+0

现在您改为为Produce事件编写事件处理程序。使用* Interval *属性选择计时器的间隔。 –

+0

但是我怎样才能得到它增加钢铁厂的变量,混凝土等的混凝土变量 –