hi guys..
am having 10 buttons on my windows form and if at least 3 of the buttons are clicked, an error should be thrown......pls i need ur help...thanks

Recommended Answers

All 4 Replies

Something like this,

declare an integer variable in your class

   int buttonsClicked = 0;

Then in each click handler for each button just increment this variable, followed by a call to a function to be written a bit later...

 buttonsClicked++;
 error();

now write a simple function that will check whether 3 buttons have been clicked and don't forget to reset the number of clicked buttons back to 0...

void error()
    {
       try
           {
               if (buttonsClicked == 3)
               {
                   buttonsClicked = 0;
                   throw new Exception("Too many buttons");
               }
        }
        catch(Exception e)
        {
            MessageBox.Show(e.Message);
        }
    }

the above will throw the exception you talk about and catch it showing the message the exception was created with.

You could optimize this a little bit but hey..good luck...

Create a commone event for all the buttons, and then create a class variable (an integer type) and count when click event occures. When counter reashes 3, show a warning message, and reset it back to zero.
Will it go?

int cnt = 0;

this.button1.Click += new System.EventHandler(button_Click);
this.button2.Click += new System.EventHandler(button_Click);
this.button3.Click += new System.EventHandler(button_Click);

void button_Click(object sender, System.EventArgs e)
{
    cnt++;

    if (cnt >= 3)
        MessageBox.Show("Click Three Times");
}

guys thaanks a lot i really appreciate

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.