Hi. i need to count the number of times a button has been clicked in C# windows application. for asp websites i know how to do it using viewstates but i dont know how to do it with windows form application. Any help will be greatly appreciated.

Recommended Answers

All 3 Replies

Update a static int in the form class via your Click event handler.

Yee, look this code:

int intClicks;
        private void button1_Click(object sender, EventArgs e)
        {
            intClicks++;
            MessageBox.Show("button has been clicked " + intClicks + (intClicks == 1 ? " time." : " times."));
        }
private void btnlogin_Click(object sender, EventArgs e)
        {
            
            count++;
            if (count <= 3)
            {
                login();
            }
            else
            {
                MessageBox.Show("3");
            }
        }

this is what i was doing!! and i found the error! the "int count = 0;" was setting count to zero outputing 1 every time! so ive removed it and added it as a global variable. But still thanks a lot for replying!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.