EventHandler没有开火

时间:2014-02-08 22:08:24

标签: c# .net winforms events

我遇到了一些问题,我正在研究一个练习程序。该应用程序是一个卡片组,它被洗牌并分配正确的图像,然后我们可以将它们放在屏幕上。当甲板是空的时,一个事件应该开火,但是我无法开火,不知道我错过了什么。 Deck.cs是发布者,Form1是监听者。使用标准的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;
}

我发现的其他事件虽然没有任何问题但仍然可以找到一个ace。有点累,我可能错过了一些非常愚蠢的东西..任何帮助都赞赏!

1 个答案:

答案 0 :(得分:1)

EmptyDeck事件未触发,因为您的表单代码在绘制之前已经检查过牌中有牌:

while (TheDeck._Cards.Count != 0)
{
    newCard = TheDeck.Draw();
相关问题