Click to See Complete Forum and Search --> : How can I make a label display how many times a button is hit?
dejan1
May 1st, 2008, 09:31 PM
I have a button and a label. I want to press the button and display on the label the total of the times pressed multiplied by $5.
For example, I press the button 3 times and it displays in the label $15.
Below is what I have so far....
private void button1_Click(object sender, EventArgs e)
{
label1.Text = "0";
int count = System.Convert.ToInt32(label1.Text);
count++;
label1.Text = count.ToString();
}
It displays "1" and allows me to hit the button only once....
Cybrax
May 1st, 2008, 09:51 PM
you initialize int count evrytime the button is pressed
place int count = 0; outside the button1_Click function
int count = 0;
private void reset()
{
label1.Text = "0";
count = 0;
}
private void button1_Click(object sender, EventArgs e)
{
count++;
label1.Text = count.ToString();
}
dejan1
May 1st, 2008, 10:27 PM
Great! Thanks. It worked. Now, it leads me to another question...how can I format this to output in currency. For example, if the button is pressed once, then 4 would appear....but how can I make it $4.00?
Thanks
public partial class Form1 : Form
{
public int count = 0;
public Form1()
{
InitializeComponent();
}
private void reset()
{
label1.Text = "0";
count = 0;
}
private void button1_Click(object sender, EventArgs e)
{
double total = 0;
count++;
total = count * 4.00;
label1.Text = total.ToString();
}
}
dejan1
May 1st, 2008, 10:31 PM
Ok, I kind of figured it out, but isn't there a more efficient way of doing it than below....
label1.Text = total.ToString("$" + total);
Cybrax
May 1st, 2008, 10:34 PM
String.Format("Money: {0:C}", total)
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.