Kris Rice
Kris Rice

Reputation: 805

Determine the parent control of a panel

I have a class called factory that inherits a class called Block. Block in turn inherits the object panel. Within each factories, i have 3 panels for production buttons. 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. 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?

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()

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

Upvotes: 0

Views: 80

Answers (1)

Hans Passant
Hans Passant

Reputation: 941277

This does not look good, an event handler inside Factory must not know anything about the different Factory instances that the program might uses. What is required here is that Factory raises an event when a Block is clicked. 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.

Upvotes: 1

Related Questions