Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Visual Basic > VB 2005 > Visual Basic 2005 Basics
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old September 15th, 2007, 11:02 AM
Registered User
 
Join Date: Sep 2007
Location: Manchester, , United Kingdom.
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?

Reply With Quote
  #2 (permalink)  
Old September 15th, 2007, 12:06 PM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 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
Reply With Quote
  #3 (permalink)  
Old September 16th, 2007, 10:22 AM
Registered User
 
Join Date: Sep 2007
Location: Manchester, , United Kingdom.
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?

Reply With Quote
  #4 (permalink)  
Old September 20th, 2007, 04:20 PM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 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
Reply With Quote
  #5 (permalink)  
Old September 22nd, 2007, 04:09 AM
Registered User
 
Join Date: Sep 2007
Location: Manchester, , United Kingdom.
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

Reply With Quote
  #6 (permalink)  
Old September 22nd, 2007, 04:29 AM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 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
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 10:15 PM.


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