471,311 Members | 1,783 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 471,311 developers and data experts.

VB.NET Label Arrays With Option Strict On Made Easy

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..

Regards Dave


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

Expand|Select|Wrap|Line Numbers
  1. Option Strict On
  2. Imports System.Drawing
  3.  
  4. Public Class Form1
  5. Public Lbl_Array As Label() = New Label(64) {} ' Set the number you labels need
  6.  
  7. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  8. ' Demo places 8 x 8 (64) lables on form
  9. Dim CNT As Integer
  10. Dim X As Integer
  11. Dim Y As Integer
  12. Dim H As Integer
  13. Dim W As Integer
  14. Dim r, c As Integer
  15.  
  16.  
  17. Y = Lbl_Master.Location.Y
  18. H = Lbl_Master.Height
  19. W = Lbl_Master.Width
  20.  
  21. CNT = 1
  22.  
  23. For c = 1 To 8
  24.   X = Lbl_Master.Location.X
  25.   For r = 1 To 8
  26.  
  27.     Lbl_Array(CNT) = New Label
  28.     Lbl_Array(CNT).Location = New System.Drawing.Point(X, Y)
  29.     Lbl_Array(CNT).Size = New System.Drawing.Size(W, H)
  30.     Lbl_Array(CNT).AutoSize = Lbl_Master.AutoSize
  31.     Lbl_Array(CNT).BackColor = Lbl_Master.BackColor
  32.     Lbl_Array(CNT).BorderStyle = Lbl_Master.BorderStyle
  33.     Lbl_Array(CNT).TextAlign = Lbl_Master.TextAlign
  34.     Lbl_Array(CNT).ForeColor = Lbl_Master.ForeColor
  35.     Lbl_Array(CNT).Font = Lbl_Master.Font
  36.     Lbl_Array(CNT).Text = CStr(CNT)
  37.     Lbl_Array(CNT).Tag = CNT ' This is the clever bit so that we can find the index!
  38.     Lbl_Array(CNT).Visible = True
  39.     Lbl_Array(CNT).BringToFront()
  40.     AddHandler Lbl_Array(CNT).Click, AddressOf lblArray_click
  41.     Panel1.Controls.Add(Lbl_Array(CNT))
  42.     X = (X + W) + 8 ' Note 8 is your offset in X
  43.     CNT += 1
  44.   Next
  45.   Y = (Y + H) + 8 ' Note 8 is your offset in Y
  46.  
  47. Next
  48. End Sub
  49.  
  50. 'This is the Event handler for a click on the Array Label
  51. Private Sub lblArray_click(ByVal sender As Object, ByVal e As EventArgs)
  52. ' fixed late binding with option strict on!! 
  53. ' Just trick the compiler with these two lines
  54. Dim Lbl_tmp As Label = CType(sender, Label)
  55. Dim i As Integer = CInt(Lbl_tmp.Tag)
  56.   Lbl_Array(i).BackColor = Color.Aqua ' change back color to show we clicked on it 
  57. End Sub:
  58. End Class
Nov 4 '10 #1
0 4663

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

9 posts views Thread by Microsoft News | last post: by
13 posts views Thread by Shannon Richards | last post: by
30 posts views Thread by Microsoft News | last post: by
3 posts views Thread by carlin smith via .NET 247 | last post: by
9 posts views Thread by YYZ | last post: by
15 posts views Thread by guy | last post: by
8 posts views Thread by Rory Becker | last post: by
8 posts views Thread by =?Utf-8?B?R3JlZw==?= | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.