使用每行的动态创建的按钮删除TableLayoutPanel的特定行

问题描述 投票:0回答:1

因此,我正在制作一个函数,该函数将从FileDialog结果填充TableLayoutPanel,然后使用循环为每行创建一个删除按钮。这是代码

Private PathtoFile1 As New List(Of String)  'this will contain all the selected file in the dialogwindow
Private rowLineDrawing As Integer = 0
Private selectedfilecountLineDrawing As Integer


Public Function AttachFileLineDrawing(TLP As TableLayoutPanel)
    Dim dr = OpenFileDialog1.ShowDialog

    If (dr = System.Windows.Forms.DialogResult.OK) Then
        selectedfilecountLineDrawing = OpenFileDialog1.FileNames.Count


        For Each FileName In OpenFileDialog1.FileNames
            Try
                Console.WriteLine(FileName.ToString)
                PathtoFile1.Add(FileName.ToString)
            Catch SecEx As Security.SecurityException
                MessageBox.Show("Security error. Please contact your administrator for details.\n\n" &
               "Error message: " & SecEx.Message & "\n\n" &
               "Details (send to Support):\n\n" & SecEx.StackTrace)
            Catch ex As Exception
                'Could Not Load the image - probably permissions-related.
                MessageBox.Show(("Cannot display the image: " & FileName.Substring(FileName.LastIndexOf("\"c)) &
                ". You may not have permission to read the file, or " + "it may be corrupt." _
                & ControlChars.Lf & ControlChars.Lf & "Reported error: " & ex.Message))
            End Try
        Next

        'MAKE SOMETHING HERE TO DISPLAY THE SELECTED ITEMS IN THE TABLELAYOUTPANEL OF THE SUBMIT PROGRESS 
        TLP.Controls.Clear()
        TLP.RowCount = 0
        rowLineDrawing = 0
        For Each Path In PathtoFile1

            Dim filepath As New Label
            filepath.Text = Path
            filepath.Width = Val(360)

            'this button is for previewing the file
            Dim btnPreview As New Button

            AddHandler btnPreview.Click,
                Sub(s As Object, e As EventArgs)
                    Dim btn = CType(s, Button)
                    MsgBox("This is Preview")

                End Sub

            'This button is for removing rows in the tablelayoutpanel
            Dim btnRmv As New Button



            Dim StringToIndex As String = Path  'THIS CATCHES EVERY PATH IN THE LOOP AND STORE IT TO THE VARIABLE WHICH THEN BE USED AS A COMPARABLE PARAMETER FOR THE INDEX SEARCH
            Dim index = PathtoFile1.IndexOf(Path)
            AddHandler btnRmv.Click,
                Sub(s As Object, e As EventArgs)
                    Dim btn = CType(s, Button)
                    MsgBox(index)
                    PathtoFile1.RemoveAt(index) 'THIS LINE OF CODE REMOVE THE SPECIFIC ITEM IN THE LIST USING THE  BTNRMV CLICK

                    'MAKE SOMETHING HERE TO REMOVE THE ROW IN THE TABLELAYOUTAPANEL 

                End Sub



            TLP.SuspendLayout()
            TLP.RowStyles.Add(New RowStyle(SizeType.Absolute, 20))
            TLP.Controls.Add(filepath, 0, rowLineDrawing)
            TLP.Controls.Add(btnPreview, 1, rowLineDrawing)
            TLP.Controls.Add(btnRmv, 2, rowLineDrawing)
            TLP.ResumeLayout()
            rowLineDrawing -= -1

        Next
    End If
End Function

因此,我尝试删除TableLayoutPanel中的行以及动态控件。我的方法是删除列表中的选定项目,并且已正确实现,但无法删除TableLayoutPanel中的行。非常感谢您的帮助!

编辑

我尝试使用上面提供的模块,但出现此错误

enter image description here

并收到此错误

enter image description here

vb.net tablelayoutpanel
1个回答
0
投票

这里是扩展方法,使您可以通过索引从TableLayoutPanel中删除任何行:

Imports System.Runtime.CompilerServices

Public Module TableLayoutPanelExtensions

    <Extension>
    Public Sub RemoveRowAt(source As TableLayoutPanel, index As Integer)
        If index >= source.RowCount Then
            Throw New ArgumentOutOfRangeException(NameOf(index),
                                                  index,
                                                  "The row index must be less than the number of rows in the TableLayoutPanel control.")
        End If

        'Remove the controls in the specified row.
        For columnIndex = 0 To source.ColumnCount - 1
            Dim child = source.GetControlFromPosition(columnIndex, index)

            If child IsNot Nothing Then
                child.Dispose()
            End If
        Next

        'Move controls below the specified row up.
        For rowIndex = index + 1 To source.RowCount - 1
            For columnIndex = 0 To source.ColumnCount - 1
                Dim child = source.GetControlFromPosition(columnIndex, rowIndex)

                If child IsNot Nothing Then
                    source.SetCellPosition(child, New TableLayoutPanelCellPosition(columnIndex, rowIndex - 1))
                End If
            Next
        Next

        'Remove the last row.
        source.RowCount -= 1
    End Sub

End Module

我在每个单元格中执行两次以下代码的3列乘4行TableLayoutPanel进行了测试,其中每个包含Label

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    TableLayoutPanel1.RemoveRowAt(1)
End Sub

结果符合预期,即每次删除第二行。您可能需要根据自己想要的行高进行更多调整。我将行高设置为相等的百分比,以便其余行按比例增长以填充空间。如果您希望有所不同,可以相应地添加代码。请注意,您可以创建几乎相同的方法来删除列。


0
投票

我尝试使用上面提供的模块,但出现此错误

enter image description here

并收到此错误

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.