【问题标题】:Determine the parent control of a panel确定面板的父控件
【发布时间】:2015-04-18 18:44:31
【问题描述】:

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

有什么建议吗?

谢谢

【问题讨论】:

    标签: .net vb.net parent basic


    【解决方案1】:

    这看起来不太好,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 事件并执行相应的操作。

    【讨论】:

    • 我对如何在我的代码中实现这一点感到困惑。 startProduction1min() 是定时器启动的事件处理程序。它与 Factory 位于一个单独的类中。我如何获得你提供给我的代码来启动计时器?
    • 现在您为 Produce 事件编写事件处理程序。使用 Interval 属性选择计时器的间隔。
    • 但是我怎样才能得到它来增加钢铁厂的钢变量,混凝土的混凝土变量等等
    • 您有 3 个不同的 Factory 对象,每个对象都有自己的 Produce 事件。让一个事件处理程序生产钢铁,另一个生产混凝土等等,现在完全是微不足道的。
    • 感谢您的帮助,我已经设法让它工作了 :)
    猜你喜欢
    • 2014-08-15
    • 2013-03-02
    • 2012-07-19
    • 2015-08-24
    • 1970-01-01
    • 1970-01-01
    • 2015-01-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多