• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 985
  • Last Modified:

Using For Each VB 2008

I am trying to loop through a form that has multiple splitcontainers, comboboxes and grids (FPSpread).

I only want to loop through the grids.

How do  I do this?

Dim cnt As FarPoint.Win.Spread.FpSpread

        For Each cnt In Me.Controls

Thanks
0
Sheritlw
Asked:
Sheritlw
  • 3
  • 2
2 Solutions
 
djon2003Commented:
In the For each loop.. check for the type of control you have..

So :
Dim cnt As Control

For Each cnt In Me.Controls
If TypeOf cnt Is FarPoint.Win.Spread.FpSpread Then
'' DO SOMETHING
End If
Next Each
0
 
SheritlwAuthor Commented:
I have probably 30  grids on  the form.  I need to get  the controls name, activesheet, etc.. while looping through so I need to reference the control type as a spread so I don't get errors when trying  to loop through them.
The grid has different properties etc. than regular controls.
thanks
0
 
djon2003Commented:
Just cast the control found to your class type..

Dim cnt As Control

For Each cnt In Me.Controls
If TypeOf cnt Is FarPoint.Win.Spread.FpSpread Then
   Dim curGrid As FarPoint.Win.Spread.FpSpread = cnt
'' DO SOMETHING
End If
Next Each
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
SheritlwAuthor Commented:
Unfortunetly it's  not finding  the grids.  I think it's because my form has a tabcontrol  with multiple tabs and a SplitContainer.  All the grids are on the  splitcontainers.
How do  I reference all the splitcontainers  and grids?
Thanks
0
 
djon2003Commented:
Ehm.. so you are saying that your grid are inside containers ? If so, use a recursive method to find them...

Call this method with DoThat(Me) --> Where Me should be the form.
Public Sub DoThat(cnt as Control)
 
For Each cnt In Me.Controls
DoThat(cnt) ' Recursive way
If TypeOf cnt Is FarPoint.Win.Spread.FpSpread Then
   Dim curGrid As FarPoint.Win.Spread.FpSpread = cnt
'' DO SOMETHING
End If
Next Each
End Sub

Open in new window

0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Right...recursion is necessary but it should look more like:
Public Class Form1
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        RecurseControls(Me)
    End Sub
 
    Private Sub RecurseControls(ByVal cnt As Control)
        For Each ctl As Control In cnt.Controls
            If TypeOf ctl Is FarPoint.Win.Spread.FpSpread Then
                Dim fps As FarPoint.Win.Spread.FpSpread = CType(ctl, FarPoint.Win.Spread.FpSpread)
                ' ...do something with "fps"...
            ElseIf ctl.HasChildren Then
                RecurseControls(ctl)
            End If
        Next
    End Sub
 
End Class

Open in new window

0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now