Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 2005 > Visual Basic 2005 Basics
|
Visual Basic 2005 Basics If you are new to Visual Basic programming with version 2005, this is the place to start your questions. For questions about the book: Beginning Visual Basic 2005 by Thearon Willis and Bryan Newsome, ISBN: 0-7645-7401-9 please, use this forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual Basic 2005 Basics 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 September 15th, 2007, 11:02 AM
Registered User
 
Join Date: Sep 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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?

 
Old September 15th, 2007, 12:06 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

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
 
Old September 16th, 2007, 10:22 AM
Registered User
 
Join Date: Sep 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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?

 
Old September 20th, 2007, 04:20 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

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
 
Old September 22nd, 2007, 04:09 AM
Registered User
 
Join Date: Sep 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

 
Old September 22nd, 2007, 04:29 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

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





Similar Threads
Thread Thread Starter Forum Replies Last Post
can any one tell me how i can add ball in run time humera Beginning VB 6 1 May 1st, 2008 11:15 AM
Labels on form sweet4511 VB How-To 2 July 20th, 2005 11:46 AM
Add static controls in run time & change font/bkg firstreflex Visual C++ 0 December 17th, 2004 12:24 PM
How to retrieve multiple values at run time using anshul21 Crystal Reports 1 May 31st, 2004 10:36 PM
Add multiple records with one form submission quinn Classic ASP Basics 3 December 13th, 2003 09:27 AM





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