CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    May 2008
    Posts
    21

    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....

  2. #2
    Join Date
    Apr 2008
    Location
    Holland
    Posts
    51

    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.
    Cybr@x Cybersp@ce If your found my contribution usefull please rate it.
    ------------------------------------------
    .: Whats the use of your knowledge, if you cannot share it? :.

  3. #3
    Join Date
    May 2008
    Posts
    21

    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();         
            }
        }

  4. #4
    Join Date
    May 2008
    Posts
    21

    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);

  5. #5
    Join Date
    Apr 2008
    Location
    Holland
    Posts
    51

    Re: How can I make a label display how many times a button is hit?

    Code:
     String.Format("Money: {0:C}", total)
    Cybr@x Cybersp@ce If your found my contribution usefull please rate it.
    ------------------------------------------
    .: Whats the use of your knowledge, if you cannot share it? :.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured