I'm working on a video poker style arcade game using WinForms in C#. When the player clicks the "Deal" button, the 5 PictureBoxes on the form switch from a face down card image to face up card images. I have no trouble getting them to flip all at once, but I wanted there to be a slight delay between each card so they reveal from left-to-right.
I found an example from the official Microsoft .NET docs (second example on this page) which uses the Timer in such a way that it will only loop through a set number of times, perfect for me since I just want to flip the 5 cards.
When I use that example in my game though, something odd happens. The cards reveal in pairs of two from left-to-right instead of one at a time. When I set a breakpoint and step through, my counter is indeed incrementing by one, but the form only updates the images on every other pass.
Why is this happening? And what can I do to fix it?
Deal Button Click:
private void dealdraw_button1_Click(object sender, EventArgs e)
{
// If we are dealing a new hand...
if (dealPhase == "deal")
{
// Change game phase
dealPhase = "draw";
// Generate a new deck of cards
deck = new Deck();
// Shuffle the deck
deck.Shuffle();
// Deal five cards to the player
playerHand = deck.DealHand();
// Start timer loop
InitializeTimer();
// Enable "Hold" buttons
for (int i = 0; i < 5; i++)
{
playerHoldButtons[i].Enabled = true;
}
}
}
InitializeTimer():
private void InitializeTimer()
{
counter = 0;
timer1.Interval = 50;
timer1.Enabled = true;
// Hook up timer's tick event handler.
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
}
Timer1_Tick:
private void timer1_Tick(object sender, EventArgs e)
{
if(counter >= 5)
{
// Exit loop code
timer1.Enabled = false;
counter = 0;
}
else
{
playerHandPictureBoxes[counter].Image = cardFaces_imageList1.Images[playerHand[counter].imageListIndex];
counter = counter + 1;
}
}