1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
Imports System.Windows.Forms
Public Class Form1
Friend WithEvents pnl As TableLayoutPanel
Friend buttons As List(Of Button)
Private nbColumn As Integer = 2
Private nbRow As Integer = 3
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
pnl = New TableLayoutPanel
pnl.Location = New Point(100, 100)
pnl.ColumnCount = 2
pnl.RowCount = 3
pnl.ColumnStyles.Add(New ColumnStyle(SizeType.Percent, 50.0))
pnl.ColumnStyles.Add(New ColumnStyle(SizeType.Percent, 50.0))
AddButtons()
Me.Controls.Add(pnl)
End Sub
Private Sub AddButtons()
For i As Integer = 0 To nbRow - 1
For j As Integer = 0 To nbColumn - 1
Dim btn As New Button
btn.Name = "btn1"
btn.Text = "button" + (i + 1).ToString() + (j + 1).ToString()
btn.Location = New Point
btn.Tag = New Position(j + 1, i + 1)
AddHandler btn.Click, AddressOf btnClick
pnl.Controls.Add(btn, j, i)
Next
Next
End Sub
Private Sub btnClick(sender As Object, e As EventArgs)
Dim btn As Button = CType(sender, Button)
Dim pos As Position = btn.Tag
MessageBox.Show(pos.Col.ToString() + "," + pos.Row.ToString())
End Sub
End Class
Public Class Position
Public Property Col As Integer
Public Property Row As Integer
Public Sub New(xcol As Integer, yrow As Integer)
Col = xcol
Row = yrow
End Sub
End Class |
Partager