VB.Net:TableLayoutPanel中的使用和寻址按钮

时间:2013-01-03 18:17:13

标签: vb.net controls tablelayoutpanel

您好我的程序中目前有这个TablelayoutPanel设置来创建一个按钮网格,该网格稍后将对应于特定的列和行:

'****INITIALISES TABLE LAYOUT INTO FORM******
    Dim ColCount, RowCount As Integer

    'Later change so that values are automatically calculated
    ColCount = 5
    RowCount = 5

    '*********Copy and pasted from site as example, CHANGE LATER*******
    Haztable = New TableLayoutPanel
    Haztable.AutoScroll = True
    Haztable.Dock = DockStyle.Fill
    Haztable.ColumnCount = ColCount
    Haztable.RowCount = RowCount
    For rowNo As Integer = 0 To Haztable.RowCount - 1
        For columnNo As Integer = 0 To Haztable.ColumnCount - 1
            'Dim ctrl As Control = New Button
            'ctrl.Text = String.Format("{0} {1},{2}", ctrl.GetType().Name, columnNo, rowNo)
            'ctrl.Size = New Size(20, 20)
            'Haztable.Controls.Add(ctrl, columnNo, rowNo)
            Dim buttonname As String
            buttonname = "B" & columnNo & rowNo
            Dim button As Control = New Button
            button.Size = New Size(70, 20)
            button.Name = buttonname
            button.Text = buttonname
            Haztable.Controls.Add(button, columnNo, rowNo)
            AddHandler button.Click, AddressOf buttonname_Click
        Next
    Next
    Me.Controls.Add(Haztable)
    Call buttonfind()

这一切都有效,创建了一个按钮网格,就像excel电子表格的布局一样。

按钮根据其XY位置命名(例如,(1,1)中的按钮将被称为“B11”)但问题是我似乎无法解决如何解决这些按钮,即

*如果单击B(X.Y),则保存按下X,Y的按钮的布尔值。

如果有一个算法扫描并检查是否按下了任何按钮而不是每个按钮使用“选择案例”,那将是很棒的。 我只是在设计器中创建按钮,但对于我的完整代码,我将需要1000多个按钮,这似乎是一种低效的方式。

2 个答案:

答案 0 :(得分:0)

你的buttonname_Click应该有一个Sender对象,你点击的Button只是将它转换为Button然后检查名称。

Private Sub buttonname_Click(sender As System.Object, e As System.EventArgs) 
    Dim btn As Button = CType(sender, Button)

    Select Case btn.Name
        Case "B11"
            'Do something
        Case "B12"
            'Do Something esle

            '...........
    End Select

End Sub

根据您的上一个声明,看看是否有效,如果您需要在程序中的其他地方引用文本,则可能需要构建数组或列表

Private Sub buttonname_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Dim btn As Button = CType(sender, Button)

    If btn.Text = "H" Then
        btn.Text = "M"
    ElseIf btn.Text = "M" Then
        btn.Text = "L"
    ElseIf btn.Text = "L" Then
        btn.Text = ""
    Else
        btn.Text = "H"
    End If


End Sub

答案 1 :(得分:0)

您已添加点击事件并使用其行号和列号处理每个事件。首先让自己成为一个新按钮,以便您可以通过花费不费力来解析行号和列号来解析控件名称中的列号和行号:

Public Class NewButton
    Inherits Button
    Public Row, Column, ClickCount as Integer
End Class

现在创建并处理:

Public Class Form1
    Sub addbuttons()
        Dim newbut As New NewButton
        newbut.Name = "B12"
        newbut.Row = "1"
        newbut.Column = "2"
        'and other properties...
        AddHandler newbut.Click, AddressOf clicked
        Me.Controls.Add(newbut)
    End Sub
    Sub clicked(sender As System.Object, e As System.EventArgs)
        Dim x As NewButton = DirectCast(sender, NewButton)
        If x.Column = 2 And x.Row = 1 Then
            x.ClickCount += 1
        End If
    End Sub
End Class