简体   繁体   English

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

[英]VB.Net: Using and addressing buttons in TableLayoutPanel

Hi I have this TablelayoutPanel setup currently in my program to create a grid of buttons that will later correspond to a specific column and row: 嗨,我目前在程序中使用此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()

And this all works, creating a grid of buttons, much like the layout of an excel spreadsheet. 所有这些都有效,创建了一个按钮网格,非常类似于excel电子表格的布局。

The buttons are named according to their XY position (eg the button in (1,1) would be called "B11") but the problem is I can't seem to work out how I can address these buttons ie 这些按钮是根据其XY位置命名的(例如(1,1)中的按钮将被称为“ B11”),但问题是我似乎无法弄清楚如何处理这些按钮,即

*If B(XY) is clicked then save boolean value that button at X,Y is pressed. *如果单击B(XY),则保存按X,Y按钮的布尔值。

It would be great to have one algorithm to scan and check if any buttons have been pressed instead of using "Select Case" for each button. 最好有一种算法来扫描和检查是否已按下任何按钮,而不是对每个按钮使用“选择大小写”。 I would just create the buttons in the designer but for my full code i'm going to need 1000+ buttons and that seems an inefficient way to do so. 我只是在设计器中创建按钮,但是对于我的完整代码,我将需要1000个以上的按钮,但这似乎是一种低效的方法。

Your buttonname_Click should have a Sender object which is the Button that you Clicked just cast it to a Button and check the name then. 您的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

Based on your last statement see if this works you may need to build an Array or a List if you need to reference the Text elsewhere in your Program 根据您的最后一条语句,看看是否可行,如果您需要在程序的其他位置引用Text,则可能需要构建一个数组或一个List

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

You have add clicked event and handle each of them by using their row and column number. 您已添加clicked事件并通过使用其行号和列号来处理每个事件。 Make yourself a new button first so that you can access row and column numbers with spending no effort to parse column and row numbers from the controls name: 首先为您自己设置一个新按钮,以便您可以访问行号和列号,而无需花费精力分析控件名称中的列号和行号:

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

Now create and handle: 现在创建并处理:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM