Wrox Programmer Forums
|
VB How-To Ask your "How do I do this with VB?" questions in this forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 13th, 2007, 06:07 AM
Registered User
 
Join Date: Feb 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default dynamically adding objects

I am interested in knowing the quickest way to dynamically add objects. I currently have an example where i need to dynamically create a grid using panels and textboxes. It is comprised of,
one large panel, which is added at design time.
At runtime I add up to 256 panels (16x16) with each panel containing 3 textboxes. To do this I do,

Code:
sub LoadGrid()
For c As Integer = 1 To 17
            For r As Integer = 1 To 17
                Dim pnlUnicodeCell As New Panel
                With pnlUnicodeCell
                    .Name = "pnlUnicodeCell_" & c & "_" & r
                    Dim columnX3 As String = "ColumnX:" & c
                    Dim rowY3 As String = "RowY:" & r
                    Dim x As Integer = cols3.Item(columnX3) '//cols3 is a hashtable containing x,y points for each cell.
                    Dim y As Integer = cols3.Item(rowY3)

                    .Location = New Point(x, y)
                    If c = 1 And r = 1 Then
                        .Width = 20
                        .Height = 20
                        .BackColor = Color.SeaGreen
                    ElseIf c = 1 And r <> 1 Then
                        .Width = 20
                        .Height = 60
                        .BackColor = Color.SeaGreen
                        'Dim cellTxt As New Label 'TextBox
                    ElseIf r = 1 And c <> 1 Then
                        .Width = 60
                        .Height = 20
                        .BackColor = Color.SeaGreen
                    Else
                        .Width = 60
                        .Height = 60
                        .BackColor = Color.LightSeaGreen
                    End If

                    .BorderStyle = BorderStyle.FixedSingle
                End With

                pnlUnicode.Controls.Add(pnlUnicodeCell)
            Next
        Next

        AddCells()
 End Sub

 Public Sub AddCells()

        For Each pnlUnicodeCell As Panel In pnlUnicode.Controls
            Dim pnlUnicodeCellName As String = pnlUnicodeCell.Name

            For c As Integer = 1 To 17
                For r As Integer = 1 To 17
                    Dim curPnlName As String = "pnlUnicodeCell_" & c & "_" & r
                    If curPnlName = pnlUnicodeCellName Then
                        If c <> 1 And r <> 1 Then
                            For g As Integer = 1 To 3
                                Dim x As Integer = 0
                                Dim y As Integer
                                Dim cellTxt As New Label ' TextBox

                                With cellTxt
                                    Select Case g
                                        Case 1
                                            y = 0
                                            .Height = 30
                                            .Font = New System.Drawing.Font("Microsoft Sans Serif", 20.5!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
                                        Case 2
                                            y = 30
                                            .Height = 15
                                            .Font = New System.Drawing.Font("Microsoft Sans Serif", 8.0!, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
                                        Case 3
                                            y = 45
                                            .Height = 15
                                            .Font = New System.Drawing.Font("Microsoft Sans Serif", 8.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
                                    End Select

                                    .Location = New Point(x, y)
                                    .Height = 20
                                    .Width = 60
                                    .BorderStyle = BorderStyle.None
                                    .Name = pnlUnicodeCell.Name & "_cell_" & g
                                    .TabIndex = tabNo
                                    .TextAlign = HorizontalAlignment.Center
                                    tabNo += 1
                                    .BackColor = pnlUnicodeCell.BackColor
                                End With

                                pnlUnicodeCell.Controls.Add(cellTxt)
                                cellTxt.BringToFront()

                                AddHandler cellTxt.Click, AddressOf ClickUnicodeCell
                            Next
                        End If
                    End If
                Next
            Next
        Next

    End Sub


This loads really slowly and I would be very grateful if someone could show me a quicker way to do this. Thanks

 
Old February 13th, 2007, 03:52 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 627
Thanks: 0
Thanked 0 Times in 0 Posts
Default

shoudn't this go in a .NET section?

anyway, did you try to enclose your code between a Me.SuspendLayout and Me.ResumeLayout?

Marco

"There are two ways to write error-free programs. Only the third one works."
Unknown
 
Old February 14th, 2007, 03:56 AM
Registered User
 
Join Date: Feb 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Apologies, I was a bit confused as to which section it should go in, being new to this forum. I thought the "vb-how to?" related to all flavours of vb, silly me. Maybe there should be a "VB.NET - How to?"
I have tried enclosing code between a Me.SuspendLayout and Me.ResumeLayout and it seems to have no affect at all

 
Old February 14th, 2007, 03:12 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 627
Thanks: 0
Thanked 0 Times in 0 Posts
Default

well, you are adding 256*(1+3) (three text boxes for each panel) controls, that makes one thousand! it will always need some time to do that.
are you really sure you want to do that?
I can't see how a user will be able to understand how to enter data in a thousand of textboxes.
Besides, how can be able to use three text boxes inside a pabel that uis only 16 pixels wide? In a large screen resolution (like 1280x1024) they will be barebly visible at all

Personally, it looks the job for a grid (only one control)

Marco

"There are two ways to write error-free programs. Only the third one works."
Unknown





Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamically add objects based on Word Bookmarks a4d2a0m VB How-To 2 February 27th, 2007 03:57 AM
Dynamically Creating Objects of Array of Classes dinkarsinha General .NET 0 January 28th, 2005 06:22 AM
how delete objects created dynamically? renee_santoscoi Javascript 1 October 7th, 2004 08:08 PM
adding more objects to an already defined form. adeptz Javascript 3 May 19th, 2004 09:50 AM
How to change report's objects dynamically. Vadim Crystal Reports 2 May 17th, 2004 09:40 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.