p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   Visual Basic 2005 Basics (http://p2p.wrox.com/forumdisplay.php?f=137)
-   -   Add multiple labels to a form at run time (http://p2p.wrox.com/showthread.php?t=62090)

peter-h September 15th, 2007 11:02 AM

Add multiple labels to a form at run time
 
I'm trying to populate a form with 256 labels arranged as a 16 X 16 block with the labels tight to each other. In the following code 'labelsize', 'toplabeltop' and 'labelleft' are set by code (not shown) that uses 'Screen.PrimaryScreen.Bounds' to establish the screen size and then sets the form size.

The relevant code for populating the form is:

        Dim lbldisplay As New Label
Dim labelsize As Integer, toplabeltop As Integer, labelleft As Integer, row As Integer, column As Integer

        For row = 0 To 15
            For column = 0 To 15
                lbldisplay.Name = row & "," & column
                lbldisplay.Size = New Size(labelsize, labelsize)
                lbldisplay.Top = toplabeltop + (row * labelsize)
                lbldisplay.Left = labelleft + (column * labelsize)
                lbldisplay.BackColor = Color.Black
                frmDisplay.Controls.Add(lbldisplay)

            Next
        Next

The problem is that only the last label is visible. What am I missing?


Imar September 15th, 2007 12:06 PM

You forgot to assign a *new* label to the lblDisplay name variable. With the current code, you overwrite the existing label on each iteration. Try this instead:
Code:

For row = 0 To 15
  For column = 0 To 15
    lbldisplay = New Label()
    lbldisplay.Name = row & "," & column
    lbldisplay.Size = New Size(labelsize, labelsize)
    lbldisplay.Top = toplabeltop + (row * labelsize)
    lbldisplay.Left = labelleft + (column * labelsize)
    lbldisplay.BackColor = Color.Black
    frmDisplay.Controls.Add(lbldisplay)
  Next
Next

HtH,

Imar

---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004

peter-h September 16th, 2007 10:22 AM

Thanks for the reply. That has successfully populated the form with my 256 labels.
I found that trying to alter the backcolor of an indivdual label with

frmDisplay.Controls.Item(lbldisplay.Name("3,5")).B ackColor = Color.Green

produced the error "Index was outside the bounds of the array."

I can successfully alter any label properties by using, for instance ~

frmDisplay.Controls(153).BackColor = Color.Green

where the 153 is the index position of the label. If it's not too much trouble would you tell me what's wrong with the code that produces the error?


Imar September 20th, 2007 04:20 PM

Hi there,

SOrry for the late reply. Somehow I overlooked this post. Anyway, what exactly is this:

frmDisplay.Controls.Item(lbldisplay.Name("3,5")).B ackColor = Color.Green

What should lbldisplay.Name("3,5") represent / contain?

Imar

---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004

peter-h September 22nd, 2007 04:09 AM

Hi Imar

Thanks for coming back on this. I've modified the program to do things a different way now by using an array in which the required pattern of label colors is stored. The display (of labels) is then updated by stepping through each element of the array and modifying the corresponding label color accordingly.
To answer your question:
Originally I used
lbldisplay.Name = row & "," & column
when setting up the original set of labels on the form, where the row and column variables were stepped on to achieve the 16 X 16 pattern of labels. I'd assumed I could then reference any label by it's name. Thus the
lbldisplay.Name("3,5")
My new solution seems to be OK although there's still a lot to code for setting the required pattern in the array. The whole project is involved with showing how a matrix of lights would appear with different patterns.

Peter


Imar September 22nd, 2007 04:29 AM

Right, an array does indeed seem like a good solution here.

Thanks for the update.

Imar

---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004


All times are GMT -4. The time now is 12:04 PM.

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