Create and Fill Grid with Appended Number

Posted on 2009-05-05
Medium Priority
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 86

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 86

Accepted Solution

Mike Tomlinson earned 2000 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


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses
Course of the Month14 days, 19 hours left to enroll

771 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