Windows Forms - how to exclude button from the whole set

c#, visual-studio-2010, winforms

Solution

Quite possibly the easiest solution is to put the Grid in its own Panel (`pnlGrid`). Put all of the buttons in there, then you could just do the following instead:

foreach (Control ctl in pnlGrid.Controls) { 
    if (ctl is Button) {
        // Do your logic here
    }
}

Problem

I have a `Windows Form` that contains only `buttons`. The final goal is to make a simple logic game I saw but for now the problem is that I want to perform different actions when my `New` button is clicked, but now it is part from all the `buttons` in the form so sometimes an action is performed on him too which should not happen. To make myself clear I have two screenshots : So this is how I want it to be - I have a matrix - 3x3 (in this case, at the end it can be `NxN`). By clicking `New` I want to be able to do various things one of which is to make `N` buttons colored red. What happens now is sometimes my `New` button also get painted because I go over the buttons like this: ``` foreach (Control c in this.Controls) { if (c is Button) { ... ``` and thus sometimes `New` get selected too, so I end up with this: What I'm thinking right now is just to perform check whenever I need in the code and exclude my `New` button explicitly but I don't think it's a good way cause I may end up with a code doing this thing in a lot of places in my program so what is the right solution in this case? If some code is needed please ask.

Original source