Vb.net 如何获取发件人列和行号?

Vb.net 如何获取发件人列和行号?,vb.net,tablelayoutpanel,Vb.net,Tablelayoutpanel,我有一个TableLayoutPanel(tlp_printing_content_layer2),有1列和2行,每个单元格还包含1个按钮,单击按钮时如何获取列和单元格编号?因为我想用另一个用户控件替换按钮 Private Sub btn_printer_Click(sender As Object, e As EventArgs) Handles btn_printer2.Click, btn_printer3.Click, btn_printer4.Click, btn_printer5.C

我有一个TableLayoutPanel(tlp_printing_content_layer2),有1列和2行,每个单元格还包含1个按钮,单击按钮时如何获取列和单元格编号?因为我想用另一个用户控件替换按钮

Private Sub btn_printer_Click(sender As Object, e As EventArgs) Handles btn_printer2.Click, btn_printer3.Click, btn_printer4.Click, btn_printer5.Click
    Dim currButton As Button = sender
    Dim prtp As New ctrl_PrinterPanel
    currButton.Dispose()   
    tlp_printing_content_layer2.Controls.Add(prtp, sender.column, sender.row)
End Sub

sender.column和sender.row无效。。。或者,这是用其他用户控件替换按钮的另一种方法?

您可以遍历列和行,直到找到与所单击按钮名称匹配的
按钮:

     Private Sub PrintButton_Click(sender As Object, e As EventArgs) Handles PrintButtonOne.Click, PrintButtonTwo.Click

        'find which row was clicked
        Dim rowPos As Integer = whichRow(sender, e)

        'did we get a valid result?
        If rowPos > -1 Then

            'this is a dummy red control so that we can see it working
            Dim myNewControl As Panel = New Panel With {.BackColor = Color.Red}

            'remove the old
            TableLayoutPanel1.Controls.Remove(DirectCast(sender, Button))

            'add the new
            TableLayoutPanel1.Controls.Add(myNewControl, 0, rowPos)

        End If

    End Sub

    Private Function whichRow(sender As Object, e As EventArgs) As Integer

        'cast the button that was clicked
        Dim clickButton As Button = DirectCast(sender, Button)

        'look through all the cols and rows
        For colNum As Integer = 0 To TableLayoutPanel1.ColumnCount
            For rowNum As Integer = 0 To TableLayoutPanel1.RowCount
                'try cast the control we find at position colnum:rownum
                Dim testcontrol As Button = TryCast(TableLayoutPanel1.GetControlFromPosition(colNum, rowNum), Button)
                If Not testcontrol Is Nothing Then
                    'is this testcontrol the same as the click control?
                    If clickButton.Name = testcontrol.Name Then
                        Return rowNum
                    End If
                End If
            Next
        Next

        'not found or some other issue
        Return -1

    End Function

您总是只有两行吗?
dim pos as TableLayoutPanelCellPosition=tlp.GetCellPosition(按钮)