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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.NET 1 27
How to open form using item in Listbox. 8 23
Visual Studios 1 76
VB.Net. Reading xml value 6 29
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

791 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