Create and Fill Grid with Appended Number

Posted on 2009-05-05
Last Modified: 2012-05-06
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.

Question by:Sheritlw
  • 3
  • 2
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 24308295
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


Author Comment

ID: 24308653

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?

LVL 85

Accepted Solution

Mike Tomlinson earned 500 total points
ID: 24308836
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.

Author Comment

ID: 24310697
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

Author Closing Comment

ID: 31578115
Sent the name of the grid to a procedure that returned the grid.  Works great, thanks.

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
DataGridView Events ? 3 45
Allow user to hide and show datagridview columns 4 25
VB.Net - Windows UserName index and length error 4 30
Sql server insert 13 30
This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
In a recent question ( here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question