VB.NET Label Arrays With Option Strict On Made Easy

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • David Rathbone

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

    Code:
    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
    Last edited by NeoPa; Nov 4 '10, 03:17 PM. Reason: Please use the [code] tags provided.
Working...