473,225 Members | 1,582 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,225 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 5098

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

Similar topics

9
by: Microsoft News | last post by:
I have a project that was created all with Option Strict OFF. Works great, not a problem with it. But if I turn Option Strict ON then I get a LOT of errors. My question, should I even care...
9
by: Codemonkey | last post by:
Hi, Sorry for a stupid question, but is it possible to do a narrowing conversion with an object array with Option Strict On in VB? E.g: ------------------ Dim aBase as Base() = {New...
13
by: Shannon Richards | last post by:
Hello: I have a problem using ByRef arguments with Option Strict ON. I have built a generic sub procedure "ChangeValue()" to change the value of an argument if the new value is not the same as the...
30
by: Microsoft News | last post by:
I have a project that was created all with Option Strict OFF. Works great, not a problem with it. But if I turn Option Strict ON then I get a LOT of errors. My question, should I even care...
3
by: carlin smith via .NET 247 | last post by:
I have an option strict on problem. i am using the event for 1 button to be the event for several others, kind of like indexes in vb6. it looks something like this: ***** Private Sub...
1
by: m830266 | last post by:
I'm trying to use the APAX serial I/O control (www.turbocontrol.com/AProZilla.htm) in a VB.NET project and I'm having trouble with its 'data received' events. The data is supplied by APAX as a...
9
by: YYZ | last post by:
After reading many messages in this group, it seems that the preferred setting for this is ON. Okay, I did that in my project (first with ..Net -- long time VB6 developer) and now a bunch of...
15
by: guy | last post by:
when i first started using .net (beta 1) i came across option strict and thought hey this could be really good, and since then have always turned it on, most people here seem to agree that this is...
8
by: Rory Becker | last post by:
A wise man once said: "Never put off until runtime what you can fix at compile time." Actually I think he said it about 10 minutes before I started this post. I am a firm believer, like the...
8
by: =?Utf-8?B?R3JlZw==?= | last post by:
We have an application in our office that has the Option Strict option set to off right now. I do understand it should be set to ON, but right now, I'm just going to continue with it this way since...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

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.