For each p in ....

pachjo

Well-known member
Joined
Dec 12, 2006
Messages
370
Programming Experience
10+
I have a form with a tabcontrol.

On this tabcontrol I have a panel taking up the entire tabpage

In this panel I have buttons etc and a datagridview

I am trying to cycle through the controls and panels to set the enabled to either true or false.

If I do this:

VB.NET:
For Each t In Me.TabControl1.TabPages

   For Each c In t.Controls

        For Each cc In c.Controls

   
        Next

   Next

Next

C is the panel

but CC only identifies the datagridview?

However if I do this:

VB.NET:
For Each t In Me.TabControl1.TabPages

   For Each p In t.Controls

       For Each c In p.Controls

       Next

   Next

Next

p = the panel

c = all the controls including the datagridview

but after it has done that it errors on the next cycle through whereby it errors saying at the line For Each p In t.Controls that it cannot convert datagridview to a panel???

But why is it trying to do this there is only one panel so it should not cycle round for another run?
 
t.Controls(0) is the Panel, c is all the controls in each Panel, you can check the type of c and you can cast it to specific type if you need to access type specific properties.
VB.NET:
For Each t As TabPage In Me.TabControl1.TabPages
    For Each c As Control In t.Controls(0).Controls

if typeof c is DataGridView then
    directcast(c, DataGridView).RowCount = 999
end if
   
    Next
Next
As you see, you can declare the loop variable and its type directly in the loop, what you posted above doesn't make much sense because we can't see what type you have declared variables t,c,cc,p...
 
Hi, thanks for this, it will come in handy to be able to cast as detailed!

I finally got it sorted. It turns out that although visually the datagridview was in the panel it actually was not?

I copied the dgv, deleted it and then pasted into the panel and it worked?

Thnx
 
I have a form with a tabcontrol.

On this tabcontrol I have a panel taking up the entire tabpage
The TabPage is already a container, why add another container (the panel)?
I'm not sure if you are setting all the controls Enabled property to either True or False, but if you are you can instead set the Enabled property of the TabPage and all controls within will be Enabled or Disabled accordingly.
 
OMG!!!!

set the tabpage to be enabled or not!

Oh the simplicity of it!!!! Thanks :)

BTW I put the panel on the tabpages to give it depth. My usual practice is that I section off gropus of controls by putting them inside a and then set it to 3DFixed.

I dont need the Group Control here so use a panel instead.

But .... set the tabpage to enabled or not ..... lost for words as to why I missed that one!!


Oh my journey is but a long one :)
 
Back
Top