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

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

Edited 4 Years Ago by zid8ne

This article has been dead for over six months. Start a new discussion instead.