473,507 Members | 2,375 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5160

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

Similar topics

9
2121
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
2359
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
2490
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
1820
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
1650
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
3761
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
2297
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
1530
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
2410
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
3875
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...
0
7223
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7376
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7031
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7485
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5623
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1542
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
760
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.