0

我有一个关于 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
4

2 回答 2

0

在 zetNieuw 中创建按钮时,您可以将按钮的 Name 属性设置为包含行和列的值,例如

nieuweKnop.Name = "btn_" & rij & "_" & kolom

如果遵循相同的约定,您以后可以使用按钮的名称按行和列查找特定按钮。此外,如果您有一个特定的按钮,您可以在 Name 属性上确定行和列。

Dim parts = btn.Name.Split("_"C)
Dim row = Integer.Parse(parts(1))
Dim col = Integer.Parse(parts(2))

如果只是关于后者(根据按钮对象检索行和列),请分析您已在 zetNieuw 中设置的 Tag 属性。

于 2013-11-12T12:05:50.520 回答
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
于 2013-11-12T12:35:46.263 回答