Click next once, Click back twice

  • Thread starter Thread starter MekkaNRG
  • Start date Start date
M

MekkaNRG

I'm just trying to make a simple flash card program when I click
"next" it goes. When I click "back" it does nothing unless I click it
twice.Then to goto the next one I have to click "next" twice.

What's the deal?
Thanks

Here is the code,

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Flashcard
{
public partial class Form1 : Form
{
private string[,] virtualCards =
{
{"One","Day"},
{"Two","Noah"},
{"Three","Ma"},
{"Four","Ra"},
{"Five","Law"},
{"Six","Jaw"},
{"Seven","Key"},
{"Eight","Fee"},
{"Nine","Bay"},
{"Ten","Daze"}
};
private int cardNumber = 0;

public Form1()
{
InitializeComponent();
}

private void btnNext_Click(object sender, EventArgs e)
{
if (cardNumber < (virtualCards.GetLength(0)))
{
lblCardTitle.Text = virtualCards[cardNumber, 0];
lblCardContent.Text = virtualCards[cardNumber, 1];
lblcardNumber.Text = Convert.ToString(cardNumber);
cardNumber++;

rtbxHistory.Text = rtbxHistory.Text + "\n<Next>"
+ lblCardTitle.Text + " "
+ lblCardContent.Text + " "
+ lblcardNumber.Text;

}
this.Update();
}

private void btnBack_Click(object sender, EventArgs e)
{
//if (cardNumber < virtualCards.GetLength(0) && cardNumber
//{

cardNumber--;
lblCardTitle.Text = virtualCards[cardNumber, 0];
lblCardContent.Text = virtualCards[cardNumber, 1];
lblcardNumber.Text = Convert.ToString(cardNumber);

rtbxHistory.Text = rtbxHistory.Text + "\n<Back>"
+ lblCardTitle.Text + " "
+ lblCardContent.Text + " "
+ lblcardNumber.Text;

//}
this.Update();
}
}
}
 
You have a fundamental problem in the semantics of your "cardNumber"
variable. For one button, you display the "card" for the current value
then update the value. For the other button you update the value and
_then_ display the "card" for the new current value.

In other words, for one button the "cardNumber" variable represents the
_next_ card that will be shown, not the current card. But for the other
button the "cardNumber" variable represents the _current_ card being
shown, not the next card.

You can't mix and match your logic this way. You need both buttons to
treat the variable in the same way.

Also, there's no need to call Update() after changing the value. The
controls will automatically invalidate themselves when you change any
property that affects its visual state. In fact, it's much less efficient
to call Update(), especially if you're calling it on the form containing
the controls rather than the controls themselves.

Pete

Thanks Pete! It worked that was it. on follow up question. How do I
start from zero if I both buttons update the variable before showing
the "card?"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top