Create and Fill Grid with Appended Number

I have a tabcontrol with 6 tabs and lots of grids on each tab.
Being from the VB 6 control index era, I named each control the same except appended a number to the end to correspond with the index of the tab.  In VB 6 they would all been named the same, but with different indexes. It would have been very easy to fill the info.

Tab 0 Index controls

Tab 1 index controls

I originally thought I could just send the index of the tab selected and append it to the name of the grid to be able to fill them.  
Dim fGrdRear as FarPoint.Win.Spread.FpSpread = fGrdRear & tabindex  
but that doesn't return the grid.
I don't want to create the same procedure a bunch of times in order to fill each tabs controls.
What would be the best way to fill the grids?  There are about 12 grids on each tab.

Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

Mike TomlinsonConnect With a Mentor Middle School Assistant TeacherCommented:
Lol...that's not alot of code!

To answer your question, though, yes...and no.

If you know the CONTAINER, you can get a control directly by passing the name to Controls() collection.  For instance if "Label3" was contained DIRECTLY by "Panel1" you could do:

    Dim i As Integer = 3
    Dim lbl As Label = Panel1.Controls("Label" & i)

But here we are hard-coding "Panel1".  If you have common code in an Event handler where the "sender" parameter will give you the correct TabPage then use code similar to the above by casting "sender" to the correct control and replacing "Panel1" with that reference.
Mike TomlinsonMiddle School Assistant TeacherCommented:
Use a function like this:

   Private Function GetControlByName(ByVal ctrlName As String) As Control
        Dim retCtrl As Control = Nothing
        Dim ctls() As Control = Me.Controls.Find(ctrlName, True)
        If ctls.Length > 0 Then
            retCtrl = ctls(0)
        End If
        Return retCtrl
    End Function

Sample Usage:
(finding "fGrdRear1" thru "fGrdRear5")

    Dim ctl As Control
    Dim ctlName As String
    Dim fGrdRear as FarPoint.Win.Spread.FpSpread
    For i As Integer = 1 to 5
        ctlName = "fGrdRear" & i
        ctl = GetControlByName(ctlName)
        If Not IsNothing(ctl) AndAlso TypeOf ctl Is FarPoint.Win.Spread.FpSpread Then
            fGrdRead = CType(ctl, FarPoint.Win.Spread.FpSpread)
            ' ... now do something with "fGrdRead" ...

        End if
    Next i

SheritlwAuthor Commented:

I only fill each tab's grids once... so if they picked tab index 3 I would only fill
fpGrdLRear3   etc.

So is there any other way to do this, without having to write so much code?

SheritlwAuthor Commented:
I guess it probably isn't, but when you are learning... it can be a pain:-)
So I tried to implement what you said, but instead of a panel I used the tabpage as the container.
Anyway, this is what I've got.   It gives me a "New" error when I get to  "With fpGrdLFront.ActiveSheet"

Thanks so much...

 Public Sub FillAllSessions(ByVal iRaceID As Integer, ByVal iMaxTabs As Integer)

        For i As Integer = 0 To iMaxTabs

            Dim tab As TabPage = Me.TabRacing.TabPages(i)

            Dim fpGrdLRear As FarPoint.Win.Spread.FpSpread = tab.Controls("fpGrdLRear" & i)
            Dim fpGrdRRear As FarPoint.Win.Spread.FpSpread = tab.Controls("fpGrdRRear" & i)
            Dim fpGrdLFront As FarPoint.Win.Spread.FpSpread = tab.Controls("fpGrdLFront" & i)
            Dim fpGrdRFront As FarPoint.Win.Spread.FpSpread = tab.Controls("fpGrdRFront" & i)
            Dim fpGrdTiresF As FarPoint.Win.Spread.FpSpread = tab.Controls("fpGrdTiresF" & i)
            Dim fpGrdTiresR As FarPoint.Win.Spread.FpSpread = tab.Controls("fpGrdTiresR" & i)

            Dim fpGrdRear As FarPoint.Win.Spread.FpSpread = tab.Controls("fpGrdRear" & i)
            Dim fpGrdFront As FarPoint.Win.Spread.FpSpread = tab.Controls("fpGrdFront" & i)

            Dim fpGrdWings As FarPoint.Win.Spread.FpSpread = tab.Controls("fpGrdWings" & i)
            Dim fpGrdEngines As FarPoint.Win.Spread.FpSpread = tab.Controls("fpGrdEngines" & i)
            Dim fpGrdGearing As FarPoint.Win.Spread.FpSpread = tab.Controls("fpGrdGearing" & i)

            Dim cr As New cRaceSessions(ConnectionString)

            cr.RaceID = iRaceID
            cr.SessionID = Field2Integer(tab.Tag)


            With fpGrdLFront.ActiveSheet

                .SetValue(0, 2, cr.LFOffsetNumb)
                .SetValue(1, 2, cr.LFShockTuningID)
                .SetValue(2, 2, cr.LFTorsionBarTuningID)
                .SetValue(3, 2, cr.LFCOSpringTuningID)
                .SetValue(4, 2, cr.LFHeight)
                .SetValue(5, 2, cr.LFWeight)

            End With

            With fpGrdLRear.ActiveSheet

                .SetValue(0, 2, cr.LBOffsetNumb)
                .SetValue(1, 2, cr.LBShockTuningID)
                .SetValue(2, 2, cr.LBTorsionBarTuningID)
                .SetValue(3, 2, cr.LBCOSpringTuningID)
                .SetValue(4, 2, cr.LBHeight)
                .SetValue(5, 2, cr.LBWeight)

            End With

            With fpGrdRFront.ActiveSheet

                .SetValue(0, 2, cr.RFOffsetNumb)
                .SetValue(1, 2, cr.RFShockTuningID)
                .SetValue(2, 2, cr.RFTorsionBarTuningID)
                .SetValue(3, 2, cr.RFCOSpringTuningID)
                .SetValue(4, 2, cr.RFHeight)
                .SetValue(5, 2, cr.RFWeight)
            End With

            With fpGrdRRear.ActiveSheet

                .SetValue(0, 2, cr.RBOffsetNumb)
                .SetValue(1, 2, cr.RBShockTuningID)
                .SetValue(2, 2, cr.RBTorsionBarTuningID)
                .SetValue(3, 2, cr.RBCOSpringTuningID)
                .SetValue(4, 2, cr.RBHeight)
                .SetValue(5, 2, cr.RBWeight)

            End With

            'Fill Engine Grid
            With fpGrdEngines.ActiveSheet

                .SetValue(0, 2, cr.EngineID)
                .SetValue(1, 2, cr.HPValve)
                .SetValue(2, 2, cr.LPValve)
                .SetValue(3, 2, cr.ByPass)
                .SetValue(4, 2, cr.FuelGallons)

            End With

            'fill gearing grid
            With fpGrdGearing.ActiveSheet

                .SetValue(0, 2, cr.GearRatio)
                .SetValue(1, 2, cr.RnBRatio)
                .SetValue(2, 2, cr.FrontSpr)
                .SetValue(3, 2, cr.RearSpr)
                .SetValue(4, 2, cr.NumbTeeth)

            End With

            'Fill Front Stagger

            With fpGrdFront.ActiveSheet

                .SetValue(0, 2, cr.FStagger)
                .SetValue(1, 2, cr.FSwayBarTuningID)

            End With

            'Fill Back Stagger

            With fpGrdRear.ActiveSheet

                .SetValue(0, 2, cr.BStagger)
                .SetValue(1, 2, cr.BSwayBarTuningID)

            End With

            'Fill wings

            With fpGrdWings.ActiveSheet

                .SetValue(0, 2, cr.UpperWingAngle)
                .SetValue(1, 2, cr.NoseWingAngle)

            End With

            With fpGrdTiresF.ActiveSheet
                .SetValue(0, 2, cr.RFTireComp)
                .SetValue(0, 3, cr.LFTireComp)

                .SetValue(1, 2, cr.RFTirePress)
                .SetValue(1, 3, cr.LFTirePress)

                .SetValue(3, 2, cr.RFTireCirc)
                .SetValue(3, 3, cr.LFTireCirc)

            End With

            With fpGrdTiresR.ActiveSheet

                .SetValue(0, 2, cr.RBTireComp)
                .SetValue(0, 3, cr.LBTireComp)

                .SetValue(1, 2, cr.RBTirePress)
                .SetValue(1, 3, cr.LBTirePress)

                .SetValue(3, 2, cr.RBTireCirc)
                .SetValue(3, 3, cr.LBTireCirc)

            End With

    End Sub
SheritlwAuthor Commented:
Sent the name of the grid to a procedure that returned the grid.  Works great, thanks.
All Courses

From novice to tech pro — start learning today.