简体   繁体   中英

How can i foreach all buttons in winforms (C#)

Please can you help me? I have all buttons in winforms.designer.cs and I assign all buttons the same handler. The handler is MouseEnter and MouseLeave . I need to find all buttons and assign each a different MouseEnter and MouseLeave .

I tried this on a button, but it doesn't work.

private void createButton_MouseEnter(object sender, EventArgs e)
{
    createButton.Cursor = NativeMethods.LoadCustomCursor(Path.Combine(collection.source, collection.cursor_hand));
    switch (Name)
    {
        case "createButton":
            this.createButton.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.create_on));
            break;
    }
}

You can easily loop through your all buttons using OfType extension method like this:

foreach(var button in this.Controls.OfType<Button>())
{
    button.MouseEnter += createButton_MouseEnter;
    button.MouseLeave += createButton_MouseEnter;
}

And in your createButton_MouseEnter if you want to get current Button's Name you can do the following:

private void createButton_MouseEnter(object sender, EventArgs e)
{
   var currentButton = sender as Button;
   var name = currentButton.Name;
   ...
}

you can access all the buttons on your winfrom in this way :

  foreach (var control in this.Controls)
         {
             if (control.GetType() == typeof(Button))
             {

                 //do stuff with control in form
             }
         }

Use this code behind any event handler to loop through all of your controls inside your winform.Thanks

You perhaps should consider making a recursive method as you might have a panel with a button on it.

so in your form's load you can go:

foreach(Control control in form.Controls)
{
       LoopControls(control);
}

the you can "play" with your button's like this as it's now a variable in your case I will add some samples of other controls for some one who is looking to do similar

static void LoopControls(Control control)
{
    switch(control)
    {
        case Button button:
            if(button.Name.Equals("createButton",StringComparison.Ordinal)
            button.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.create_on));

            // other stuf if you need to...
            break;
        case ListView listView:
            // other stuf if you need to...
            break;
        case Label label:
            // other stuf if you need to...                  
            break;
        case Panel panel:
            // other stuf if you need to...
            break;
        case TabControl tabcontrol:
            // other stuf if you need to...
            break;
        case PropertyGrid propertyGrid:
            // other stuf if you need to...
            break;
    }
    foreach(Control child in control.Controls)
        LoopControls(child);
}

I think it's very simple, i'm sorry if i didn't understand the question:

if (sender == createButton)
{
    createButton.Cursor = NativeMethods.LoadCustomCursor(Path.Combine(collection.source, collection.cursor_hand));
    createButton.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.create_on));
}
 if (sender == otherButton)
 {
     //otherCode
 }

If you want to create some common function for Multiple Forms then you can pass the Current Form object too

public class CommonHelper
{
   public void CreateButton_MouseEnter(Form currentForm)
   {
      var buttonsOnForm = currentForm.Controls.OfType<Button>();
      foreach (var btn in buttonsOnForm)
      {
         switch (btn.Name)
         {
             case "createButton":
             this.createButton.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.create_on));
             break;
         }
      }
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM