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.

Dani AI

Generated

Good catch on the scope bug, . In WinForms, a variable declared inside the click handler is re-initialized on every click. Keep the counter as a field on the form (per-form count), or make it static if you truly want one shared count across all instances. That aligns with ’s note and @Mitja Bonca’s sample, but here is a version you can drop in that also shows how to share one handler and stop login after 3 tries.

public partial class LoginForm : Form
{
    private int _clicks; // field on the form, not local to the handler

    public LoginForm()
    {
        InitializeComponent();
        btnLogin.Click += CountAndMaybeLogin;
        // If you have multiple buttons to count:
        // foreach (var b in new[] { btnLogin, btnOther }) b.Click += CountAndMaybeLogin;
    }

    private void CountAndMaybeLogin(object sender, EventArgs e)
    {
        _clicks++;
        lblClickCount.Text = $"Clicked {_clicks} time{(_clicks == 1 ? "" : "s")}";

        if (_clicks <= 3)
        {
            Login();
        }
        else
        {
            btnLogin.Enabled = false; // prevents further attempts
            MessageBox.Show("Too many attempts.");
        }
    }
}

Need the count to persist between app runs (not just while the form is open)? Store it as a user-scoped setting or in a small file. With user settings, create an int setting named ClickCount, then update and save it when you increment:

Properties.Settings.Default.ClickCount++;
Properties.Settings.Default.Save(); // or defer to FormClosing to reduce disk writes

Notes:

  • Keep UI updates (like changing a label or disabling the button) inside the UI thread; normal Click events already run on the UI thread.
  • Prefer a form field over static unless you explicitly want one global counter shared by all forms.

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.