0

I have an app where I have many buttons inside so to differentiate them, I create a class in which I put this: I have to say that my button is in a FlowLayoutPanel.

public static void SetButtonPos(Form f1,FlowLayoutPanel fk)
        {



            foreach (Button c in f1.Controls)
            {

               if(c.Name.Contains("BTN_Menu"))
                {
                    c.Size= new Size(247, 45);
                    c.BackColor = ColorTranslator.FromHtml("#373737");
                    c.ForeColor = ColorTranslator.FromHtml("#FFFFFF");
                    c.FlatStyle = FlatStyle.Flat;
                    c.FlatAppearance.BorderSize = 0;
                    c.TextAlign = ContentAlignment.MiddleLeft;
                    c.TextImageRelation = TextImageRelation.ImageBeforeText;
                    c.Height = 45;
                    c.Width = fk.Width - 6;

                }
            }

        }

But I got the error in the title, do you have any idea ?

Unable to cast object of type 'System.Windows.Forms.FlowLayoutPanel' to 'System.Windows.Forms.Button

Thank you.

Sheva07
  • 23
  • 1
  • 8
  • 1
    Possible duplicate of [Getting 'Unable to cast object of type' error when trying to loop through Button controls on Form](http://stackoverflow.com/questions/28468613/getting-unable-to-cast-object-of-type-error-when-trying-to-loop-through-button) – Sinatr Nov 09 '16 at 14:44
  • It is not clear where is located the button you are searching for. It is contained in the FlowLayoutPanel controls collection or inside the form controls collection? – Steve Nov 09 '16 at 14:49
  • my buttons are inside a flowpanel – Sheva07 Nov 09 '16 at 14:54
  • Yes this explains better the error message, looping over the form controls collection you will find the FlowLayoutPanel, the buttons are one level down, inside the FlowLayoutPanel controls collection – Steve Nov 09 '16 at 14:55

1 Answers1

1

This line is incorrect

foreach (Button c in f1.Controls)

Here you consider that each control in f1 is a button, so also textboxes and other controls will trigger your error. Instead if you want only buttons change your code to

foreach (Button c in f1.Controls.OfType<Button>())

Keep in mind that this will find only Buttons that are directly contained in the Controls collection of your form. If they are inside another container (like a groupbox or panel) the line above will not work and you should use the appropriate container or a recursive call to traverse every Controls collection

EDIT

If your button is inside the controls collection of the FlowLayoutPanel then the code should search your button in that collection

foreach (Button c in fk.Controls.OfType<Button>())
Steve
  • 213,761
  • 22
  • 232
  • 286
  • Going to need to recurse into child controls as well, though, e.g. http://stackoverflow.com/questions/1558127/how-can-i-get-all-controls-from-a-form-including-controls-in-any-container – stuartd Nov 09 '16 at 14:43
  • I was just writing about that. However that answer doesn't use the OfType extension. Searching for a dupe,,,, – Steve Nov 09 '16 at 14:45
  • According to the OP, the buttons aren't in `f1`; they're in `fk`. – adv12 Nov 09 '16 at 14:50
  • Exactly... this is the reason for the error message – Steve Nov 09 '16 at 14:52
  • Is it correct the condition ? if(c.Name.Contains) ? I need to only change on specific buttons – Sheva07 Nov 09 '16 at 14:56
  • The if will be taken only if the buttons names are for example BTN_Menu1, ABTN_Menu, Another_BTN_Menu (note the case sensitivity). If this conditions is correct or not I can't say. It depends by your requirement – Steve Nov 09 '16 at 14:58
  • Okay, so I have to loop in the flow not in the form – Sheva07 Nov 09 '16 at 15:00
  • Yes, this is how the controls collection work. Every control of the container kind (like the Form itself, but also the GroupBoxes or Panels) have their own child controls kept in their own Controls collection) – Steve Nov 09 '16 at 15:10