Solved

Create and Fill Grid with Appended Number

Posted on 2009-05-05
5
329 Views
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
fGrdRear0
fGrdFront0
fGrdLFront0

Tab 1 index controls
fGrdRear1
fGrdFront1
fGrdLFront1

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.  
Like  
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.

Thanks
0
Comment
Question by:Sheritlw
  • 3
  • 2
5 Comments
 
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

0
 

Author Comment

by:Sheritlw
ID: 24308653

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

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

Thanks
0
 
LVL 85

Accepted Solution

by:
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.
0
 

Author Comment

by:Sheritlw
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)

            cr.FillSessionFromDB()

            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

        Next
    End Sub
0
 

Author Closing Comment

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

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

759 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now