Click here to Skip to main content
14,929,225 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to remove a label and a button from a row in a TableLayoutPanel. I want to be able to click a button on a screen and be able to remove a certain button and label from the layout panel, after asking the user if they want to remove this row. Here is the code I have tried and it does not work. When I click yes, it doesn't remove the row I want, it just closes the page entirely. The program doesn't stop running the page with the table just closes.

What I have tried:

private void button2_Click(object sender, EventArgs e)
        {
            DialogResult = MessageBox.Show("Are you sure you want to delete this use?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            if (DialogResult == DialogResult.Yes)
            {
                UserTable.Controls.Remove(label3);
                UserTable.Controls.Remove(button2);
            }
            else
            {
                this.Close();
            }
            

        }
Posted
Updated 4-May-21 19:08pm
v2
Comments
OriginalGriff 4-May-21 3:53am
   
"It doesn't work" is probably the most useless problem report we get - and we get it a lot. It tells us nothing about what is happening, or when it happens.
So tell us what it is doing that you didn't expect, or not doing that you did.
Tell us what you did to get it to happen.
Tell us any error messages.
Use the "Improve question" widget to edit your question and provide better information.
BillWoodruff 5-May-21 1:10am
   
When I click yes, it doesn't remove the row I want, it just closes the page entirely.

in the code shown, this is incorrect: a 'yes would delete the Controls.

I would suspect this line where the MessageBox result is assigned to the Form's DialogResult property. Look in the help to see what happens. Well you already know!
DialogResult = MessageBox.Show("Are you sure you want to de ....

What you need is a local variable of type DialogResult to use in the comparison.
DialogResult result = MessageBox.Show("Are you sure you want to de ....
if (result == DialogResult.Yes) {
   
Comments
BillWoodruff 5-May-21 0:06am
   
+4 The user SHOULD use a local variable, but, in this case ... if there is no other code uses the Form's DialogResult ... it will not potentially cause an error.

Note that the Form.DialogResult property is not exposed in the design-time Property Browser view.
1) take Alan's advice, and vote his solution up

2) if you use 'Close the way shown in your code, it will shut down the WinForm app.

3) if what you want to do is remove a specific Row, that requires a rather complex technique (imho, a design flaw of the TableLayoutPanel): it cannot be done by just removing a specific RowStyle !

3a) this thread on SO shows solutions, and discusses the issues: [^]

3b) if you have removed all its Controls, you can Hide a RowStyle by setting its 'Height to #0, but, then, other rows may resize in a way you don't want/like.

If you want to remove a Row with other Rows below it, things get hairy :)

Because a TableLayout Panel does not support data binding, if your app frequently edits/changes Rows or Columns with Controls, you may want to use a Control like a DataGridView that can be bound to a DataTable.
   
UserTable.Controls.Remove(button2);

Deleting the button2 with it's own handler (button2 click) would probably be considered catastrophic.

Need (at least) a different button / handler.

(If button2_click is not really hooked up to button2 ... pardon my stupidity).
   
Comments
Rema Thomas 4-May-21 4:37am
   
Is there a way to do what I am trying to do at all? If I have a separate button that is not hooked up to the row I am trying to clear, can I remove a certain row. For example, if I want to remove row 2 from the tablelayoutpanel by just pressing a button. Can I do that? I tried something like this(UserTable.Controls.Remove(UserTable.GetControlFromPosition(2));) but it just closed the page again.
BillWoodruff 5-May-21 1:29am
   
Voted #1: random thoughts from someone who has obviously never really worked with a TableLayoutTable.

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