我对VB.net有疑问。 在此代码中,创建了64个按钮(通过for循环)。如何通过坐标访问Klik方法和zetnieuw中的按钮?因为,基本上zetnieuw功能只是制作按钮。
Public Class Form1
Dim AANTAL_KOLOMMEN As Integer = 8
Dim AANTAL_RIJEN As Integer = 8
Dim Button1 As Button
Dim tel As Boolean
Dim value1 As Integer
Dim value2 As Integer
Public Function zetNieuw(rij As Integer, kolom As Integer) As Button
Dim nieuweKnop As New Button()
nieuweKnop.Width = 40
Me.Controls.Add(nieuweKnop)
nieuweKnop.Left = kolom * nieuweKnop.Width
nieuweKnop.Top = rij * nieuweKnop.Width
nieuweKnop.Height = nieuweKnop.Width
nieuweKnop.Text = ""
AddHandler nieuweKnop.MouseDown, AddressOf Klik
...
nieuweKnop.Tag = (kolom + (rij * AANTAL_KOLOMMEN))
Return nieuweKnop
End Function
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Me.value1 = 1 To AANTAL_KOLOMMEN Step 1
For Me.value2 = 1 To AANTAL_RIJEN Step 1
Me.zetNieuw(value2, value1)
Next
Next
End Sub
Private Sub Klik(sender As Object, e As MouseEventArgs)
Dim knop As Button = sender
...
End Sub
答案 0 :(得分:0)
在zetNieuw中创建按钮时,可以将按钮的Name属性设置为包含行和列的值,例如:
nieuweKnop.Name = "btn_" & rij & "_" & kolom
如果遵循相同的约定,您可以稍后按行和列使用按钮的名称find特定按钮。此外,如果您有特定按钮,则可以在Name属性上确定行和列。
Dim parts = btn.Name.Split("_"C)
Dim row = Integer.Parse(parts(1))
Dim col = Integer.Parse(parts(2))
如果只是后者(根据按钮对象检索行和列),请分析您在zetNieuw中设置的Tag属性。
答案 1 :(得分:0)
像这样的东西应该可以工作,像这样声明你的数组
Dim myButtons(AANTAL_KOLOMMEN - 1, AANTAL_RIJEN - 1) As Button
像这样填充它,请注意.Net中的数组为零:
For Me.value1 = 0 To AANTAL_KOLOMMEN - 1 Step 1
For Me.value2 = 0 To AANTAL_RIJEN - 1 Step 1
myButtons(value2, value1) = Me.zetNieuw(value2, value1)
Next
Next