For each statement

  • Thread starter Thread starter Lou
  • Start date Start date
L

Lou

I have a panel control with buttons and labels
I want to loop through only the buttons so I do this

Dim btn As Button

For Each btn In FlowPanelBottom.Controls

Next

I get an error that the control is a label control. Why is that when I am
only looping through button types?

Unable to cast object of type 'System.Windows.Forms.Label' to type



-Louie
 
Change this to:

For Each c as Control in FlowPanelBottom.Controls

If c is Button then
// do button stuff here
end if

Next
 
I have a panel control with buttons and labels
I want to loop through only the buttons so I do this

Dim btn As Button

For Each btn In FlowPanelBottom.Controls

Next

I get an error that the control is a label control. Why is that when I am
only looping through button types?

Unable to cast object of type 'System.Windows.Forms.Label' to type

-Louie

Dim btn As control

For Each btn In FlowPanelBottom.Controls
if typeof btn is button then
your commands here.
end if
Next
 
Lou,

I did not test it, but probably you don't have option strict in the top of
your program not set to ON.

Now it threath the button as a control (you are going through the collection
of controls).

For the rest see the other advices.

Cor
 
Chris Mullins said:
For Each c as Control in FlowPanelBottom.Controls

If c is Button then

I believe you wanted to type 'If TypeOf c Is Button Then' :-).
 
Herfried K. Wagner said:
I believe you wanted to type 'If TypeOf c Is Button Then' :-).

Yup, that's exactly what I meant.

C# and VB.Net are so blurred for me, I can't ever keep the syntax
straight...
 
Back
Top