Click here to Skip to main content
15,918,976 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
See more:
I am currently working on

I want delete rows from tablelayoutpanel while i select the checkbox.

When i am checked checkbox at a time rows should be deleted from tablelayoutpanel by using

What I have tried:

Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click

Dim row As Integer
row = TableLayoutPanel1.RowCount - 1
For col As Int32 = 0 To 6
Dim c As Control
c = TableLayoutPanel1.GetControlFromPosition(col, row)
TableLayoutPanel1.RowCount -= 1

End Sub

In this code, row will delete but from last.

Actually i want to delete row which is selected by user.
Updated 10-Apr-17 7:22am

1 solution

It appears you have set the row to be deleted as the last row in your code above

This line is the problem
row = TableLayoutPanel1.RowCount - 1

Steps you should follow:

a. Loop through all Controls in TableLayoutPanel1
b. Find checkbox and see if it is checked
c. Get its position (row and Column) using GetPositionFromControl
d. Get the row
e. Delete the control
f. Delete the row

DIM RowToDelete as Integer
For Each CNTRL in TableLayoutPane1.Controls
If (TypeOf CNTRL Is CheckBox) AndAlso DirectCast(CNTRL, CheckBox).Checked Then
		RowToDelete = TableLayoutPanel1.GetPositionFromControl(CNTRL).Row
		Exit For
End If

TableLayoutPanel1.RowCount -= 1

Hope this helps.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900