简体   繁体   English

确定面板的父控件

[英]Determine the parent control of a panel

I have a class called factory that inherits a class called Block. 我有一个名为factory的类,该类继承了一个名为Block的类。 Block in turn inherits the object panel. 块又继承了对象面板。 Within each factories, i have 3 panels for production buttons. 在每个工厂中,我有3个用于生产按钮的面板。 When the production buttons are clicked, production timers are triggered. 单击生产按钮后,将触发生产计时器。

My issue is when i click, for example, on the 1 minute production in my concrete factory, my steel production starts. 我的问题是,例如,当我单击混凝土工厂的1分钟生产时,我的钢铁生产就开始了。 This is the same for all buttons in all factories; 所有工厂中的所有按钮都是一样的。 they all trigger steel productions. 它们都触发了钢铁生产。

What i need to know is when i click my 1 min button in the concrete factory, how do i determine it has come from the button in the concrete factory, and not the steel one? 我需要知道的是,当我单击混凝土工厂中的1分钟按钮时,如何确定它来自混凝土工厂中的按钮,而不是钢制按钮?

Here is my handler events for the buttons in factory 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

Here is my startProduction1min() 这是我的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

The issue seems to be with: 问题似乎在于:

If stlFac.Bounds.Contains(sender.Bounds) Then

Any suggestions? 有什么建议么?

Thanks 谢谢

This does not look good, an event handler inside Factory must not know anything about the different Factory instances that the program might uses. 这看起来不太好, Factory中的事件处理程序必须不了解有关程序可能使用的不同Factory实例的任何信息。 What is required here is that Factory raises an event when a Block is clicked. 这里需要的是,单击“块”时Factory会引发一个事件。 That could look something like this: 可能看起来像这样:

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

Now the client code can simply subscribe the Produce event and implement the appropriate action. 现在,客户端代码可以简单地订阅Produce事件并执行适当的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM