|
-
May 1st, 2008, 09:31 PM
#1
How can I make a label display how many times a button is hit?
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....
-
May 1st, 2008, 09:51 PM
#2
Re: How can I make a label display how many times a button is hit?
you initialize int count evrytime the button is pressed
place int count = 0; outside the button1_Click function
Code:
int count = 0;
private void reset()
{
label1.Text = "0";
count = 0;
}
private void button1_Click(object sender, EventArgs e)
{
count++;
label1.Text = count.ToString();
}
Last edited by Cybrax; May 1st, 2008 at 09:56 PM.
-
May 1st, 2008, 10:27 PM
#3
Re: How can I make a label display how many times a button is hit?
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
Code:
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();
}
}
-
May 1st, 2008, 10:31 PM
#4
Re: How can I make a label display how many times a button is hit?
Ok, I kind of figured it out, but isn't there a more efficient way of doing it than below....
Code:
label1.Text = total.ToString("$" + total);
-
May 1st, 2008, 10:34 PM
#5
Re: How can I make a label display how many times a button is hit?
Code:
String.Format("Money: {0:C}", total)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|