确定面板的父控件

Determine the parent control of a panel

我有一个名为 class 的工厂继承了一个名为 Block 的 class。块又继承了对象面板。在每个工厂内,我有 3 个用于生产按钮的面板。单击生产按钮时,将触发生产计时器。

我的问题是,例如,当我点击我的混凝土工厂的 1 分钟生产时,我的钢铁生产开始了。这对所有工厂的所有按钮都是一样的;它们都会触发钢铁生产。

我需要知道的是,当我在混凝土工厂中单击我的 1 分钟按钮时,我如何确定它来自混凝土工厂中的按钮,而不是钢制按钮?

这是工厂中按钮的处理程序事件 class:

    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

有什么建议吗?

谢谢

这看起来不太好,Factory 中的事件处理程序一定不知道程序可能使用的不同 Factory 实例的任何信息。这里需要的是 Factory 在单击 Block 时引发事件。这可能看起来像这样:

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 事件并执行适当的操作。