Using Labels in Arrays

Updated DavidRathbone 1 Tallied Votes 2K Views Share

Looking on the web very few have got VB.net 2008/2010 using object arrays working.
I felt it was time to give back what I have taken from the web for any other users.

VB6 had a simple object array system which has been lost in VB.net
As a C#/ C++ programer I like dot.net but think "Basic" should be Basic!

I use VB.net for rapid Visual development and enjoy its ease in which you can come back to the code to re-read it.

OK you need a Label as an Array with Option Strict On..........
This demo places 64 labels 8x8 on a panel and changes back color when you click on it.

Open a new Form and place a Panel1 on the form size 600x400
Place a Lable on the top left corner of the panel. (Location 15,12)

I called it "Lbl_Master" set visible to false , Autosize false, TextAlign MiddleCenter,
BackColor Lime , Text 01 and Size 40,30
This acts as a Master for ease of your array start postion and other visual properties.

place a button (Button1) on the form.


Hope this helps all you VB coders..

Reagrds Dave


Enter This code for the Form1 as .................

Option Strict On
Imports System.Drawing

Public Class Form1
Public Lbl_Array As Label() = New Label(64) {} ' Set the number you labels need

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' Demo places 8 x 8 (64) lables on form
Dim CNT As Integer
Dim X As Integer
Dim Y As Integer
Dim H As Integer
Dim W As Integer
Dim r, c As Integer


Y = Lbl_Master.Location.Y
H = Lbl_Master.Height
W = Lbl_Master.Width

CNT = 1

For c = 1 To 8
X = Lbl_Master.Location.X
For r = 1 To 8

Lbl_Array(CNT) = New Label
Lbl_Array(CNT).Location = New System.Drawing.Point(X, Y)
Lbl_Array(CNT).Size = New System.Drawing.Size(W, H)
Lbl_Array(CNT).AutoSize = Lbl_Master.AutoSize
Lbl_Array(CNT).BackColor = Lbl_Master.BackColor
Lbl_Array(CNT).BorderStyle = Lbl_Master.BorderStyle
Lbl_Array(CNT).TextAlign = Lbl_Master.TextAlign
Lbl_Array(CNT).ForeColor = Lbl_Master.ForeColor
Lbl_Array(CNT).Font = Lbl_Master.Font
Lbl_Array(CNT).Text = CStr(CNT)
Lbl_Array(CNT).Tag = CNT ' This is the clever bit so that we can find the index!
Lbl_Array(CNT).Visible = True
Lbl_Array(CNT).BringToFront()
AddHandler Lbl_Array(CNT).Click, AddressOf lblArray_click
Panel1.Controls.Add(Lbl_Array(CNT))
X = (X + W) + 8 ' Note 8 is your offset in X
CNT += 1
Next
Y = (Y + H) + 8 ' Note 8 is your offset in Y

Next
End Sub
'This is the Event handler for a click on the Array Label
Private Sub lblArray_click(ByVal sender As Object, ByVal e As EventArgs)
' fixed late binding with option strict on!! 
' Just trick the compiler with these two lines
Dim Lbl_tmp As Label = CType(sender, Label)
Dim i As Integer = CInt(Lbl_tmp.Tag)
Lbl_Array(i).BackColor = Color.Aqua ' change back color to show we clicked on it 
End Sub:
End Class
AndreRet commented: Nice layout, no code encasing though. +4
AndreRet 526 Senior Poster

Thank you for the snippet David. Here is some points for your effort.

If you however can enclose your code in the future in code brackets as in - "(CODE)(/CODE)" all viewers will find your post more resourceful. All you have to do is to click on the "(CODE)" label in the post reply box to encase all code in its place.

Thanks:)

AndreRet 526 Senior Poster

You have also selected a new thread as a question and not as a code snippet. Please mark this as solved otherwise it will stay open for infinity of time.

peter_budo 2,532 Code tags enforcer Team Colleague Featured Poster

OK, switched it to code snipped and added code tags

AndreRet 526 Senior Poster

Thanks Peter.:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.