在网上浏览时,很少有人能够使用对象数组来运行VB.net 2008/2010。
我觉得是时候把我从网络上获取的一切回馈给其他用户了。
VB6有一个简单的对象数组系统,该系统已在VB.net中丢失
作为C#/ C ++程序员,我喜欢dot.net,但认为“基本”应该是基本的!
我使用VB.net进行快速的Visual开发,并喜欢它的简便性,您可以回到代码中重新阅读它。
确定,您需要一个标签作为数组,且选项严格打开。
此演示在面板上放置了64个8x8标签,并在您单击时更改了颜色。
打开一个新窗体,然后将Panel1放置在600x400的窗体上
将标签放在面板的左上角。 (位置15,12)
我称之为“ Lbl_Master”,设置为false,Autosize false,TextAlign MiddleCenter,
BackColor Lime,Text 01和Size 40,30
这可以简化阵列开始位置和其他视觉属性。
在窗体上放置一个按钮(Button1)。
希望这对您的VB编码人员有所帮助。
问候戴夫
输入Form1的此代码为................。
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
From: https://bytes.com/topic/visual-basic/insights/900984-vb-net-label-arrays-option-strict-made-easy