Ari_Stoteles
Ari_Stoteles

Reputation: 29

EventHandler not firing

I'm having some problem getting the last details working on a practise program I've been working on. The application is a card deck, which gets shuffled and assigned proper images and then we can place them on screen. When the deck is empty an event should fire, but I can't get it to fire, not sure what I'm missing. Deck.cs is publisher, Form1 is listener. Using standard EventArgs.

Deck.cs:

public event EventHandler<EventArgs> EndOfDeck;

public Card Draw()
{
    FindAce aceFound;
    EventArgs emptyDeck;

    if (_cards.Count != 0)
    {
        Card card = _cards[0];
        _cards.RemoveAt(0);

        if (card.FaceVal == FaceValue.Ace)
        {
            switch (card.Suit)
            {
                case Suit.c:
                    aceFound = new FindAce("Cloves");
                    AceFound(aceFound);
                    break;
                case Suit.d:
                    aceFound = new FindAce("Diamonds");
                    AceFound(aceFound);
                    break;
                case Suit.h:
                    aceFound = new FindAce("Hearts");
                    AceFound(aceFound);
                    break;
                case Suit.s:
                    aceFound = new FindAce("Spades");
                    AceFound(aceFound);
                    break;
            }
        }

        return card;
    }
    else
    {
        emptyDeck = new EventArgs();
        EmptyDeck(emptyDeck);

        return null;
    }
}

public void EmptyDeck(EventArgs e)
{
    if (EndOfDeck != null)
        EndOfDeck(this, e);
}

Form1.cs

protected void button1_Click(object sender, EventArgs e)
{
    TheDeck = new Deck();
    TheDeck.Shuffle();

    HandInterface.Reset();

    //Listener for FindAce
    TheDeck.FindAce += OnAceFound;
    TheDeck.EndOfDeck += OnEmptyDeck;

    drawBox.Enabled = true;
    aceView.Clear();

    Controls.Add(handInterface);
    this.Update();
}

public void button2_Click(object sender, EventArgs e)
{
    if (TheDeck._Cards.Count != 0)
    {
        TempCard = TheDeck._Cards[0];

        if (oneCardBtn.Checked == true)
        {
            newCard = TheDeck.Draw();
            this.Controls.Add(handInterface);
            handInterface.CreateCard(newCard, 1);
        }

        if (allCardBtn.Checked == true)
        {
            while (TheDeck._Cards.Count != 0)
            {
                newCard = TheDeck.Draw();
                this.Controls.Add(handInterface);
                handInterface.CreateCard(newCard, 1);
            }
        }
    }

   // EmptyDeck event should fire when above is no longer true, but doesn't
}

private void OnEmptyDeck(object sender, EventArgs e)
{
    MessageBox.Show("The deck is empty!");
    drawBox.Enabled = false;
}

My other event which fires on finding an ace works without any problem though. Kinda tired, I'm probably missing something really stupid .. any help appreciated!

Upvotes: 1

Views: 123

Answers (1)

stuartd
stuartd

Reputation: 73313

The EmptyDeck event is not firing because your form code is already checking that there are cards in the deck before drawing:

while (TheDeck._Cards.Count != 0)
{
    newCard = TheDeck.Draw();

Upvotes: 1

Related Questions