Getting 'Unable to cast object of type' error when trying to loop through Button controls on Form

巧了我就是萌 提交于 2019-12-24 12:20:59

问题


In a Form, I added a TableLayoutPanel, and within that I add 5 buttons.

At runtime, I add 10 buttons to Form1 in a loop. Then I use foreach to do something with those 10 buttons.

foreach (Button C in this.Controls)
    // do something

When I the run the program, an error appears:

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

I think this error happens because the TableLayoutPanel contains 5 buttons in it.

Yeah I can delete this TableLayoutPanel and add 5 buttons directly in the Form, but the TableLayoutPanel helps a lot in my code.

So is there any solution to traverse those 10 buttons and still keep the TableLayoutPanel?

Furthermore, can I traverse "buttons in Form" and "buttons in TableLayoutPanel" separately ?


回答1:


Your current code is going to try to iterate over ALL controls on the Form (well, all top-level controls anyway.. you'd need to use recursion to iterate through all controls nested within other controls), then cast each to a Button, hence the exception you're getting.

Just specify which controls you want to iterate over:

foreach (var button in this.Controls.OfType<Button>())
{
    // now you'll iterate over just the Button controls
}

If you want to just iterate over the controls in the TableLayoutPanel (I don't think that's the case; I think you've got Buttons directly on the Form and more Buttons in the TableLayoutPanel, and you want to loop over the Buttons on the Form itself), then reference that child control instead:

foreach (var button in tableLayoutPanel1.Controls.OfType<Button>())
{
    // iterate over the Button controls inside the TableLayoutPanel
}



回答2:


System.Windows.Forms.TableLayoutPanel' to type 'System.Windows.Forms.Button'

As the error explains you try to caste elements for a type that it cannot be caste.

Reason : foreach loop

foreach (Button C in this.Controls) // Button is the wrong type caste

this.Controls will return every control in your current form, this include other form elements like TableLayoutPanel which cannot be cast to a button. So filter them as follow.

Answer :

foreach (var C in this.Controls){
     if(c.GetType()== typeof(Button)){
          Button btn = (Button)item; //do work using this
      } 
}

Note : If buttons are reside inside another controller this approach will not provide them. Instead you need to access the specific control and loop in it.




回答3:


Form.Controls is of type ControlCollection, so your code might not run! Use below code instead:

foreach (Control C in this.Controls)
{
    // do something
}

or

foreach(Button b in this.Controls.OfType<Button>())
{
   //do something
}


来源:https://stackoverflow.com/questions/28468613/getting-unable-to-cast-object-of-type-error-when-trying-to-loop-through-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!