GFDSRFV
GFDSRFV

Reputation: 9

C# winforms dynimcally created Labels position

I have a bunch of dynamically added controls which add row by row when the user clicks the add user button. I want there to be a label when the page loads and i want the same label to move down every time the add user button is clicked (under each row of textboxes). Right now it is there on load and it moves down when the user clicks the button the first time but after that it just stays. Here is my code:

Global variables:

Label Savelbl = new Label();    
int LabelX = 15;
int LabelY = 110;       
int spacelbl = 15;

Page load:

Savelbl.Location = new Point(LabelX, LabelY);
Savelbl.Name = "Savelbl";
Savelbl.Text = "Please click 'save' to save your changes";
CaeUsersPanel.Controls.Add(Savelbl);

Add user button:

    private void CAEAddUserbtn_Click(object sender, EventArgs e)
    {

        var i = UsernameTextBoxes.Count + 1; // this is a list of the added textboxes
        ADDUserInfo(i); //method which adds the dynamically created textboxes

      Savelbl.Location = new Point(LabelX, LabelY + spacelbl);

    }

Remove user button (the label should move back up when this is clicked):

private void Remove_Click(object sender, EventArgs e)
{
DialogResult result = MessageBox.Show("Are you sure you want delete this user?  \n Deleting users may break workflows", "Delete", MessageBoxButtons.YesNo);
            if (result == DialogResult.Yes)
            {

                int idx = RemoveButtons.IndexOf((Button)sender);

                // Remove button
                RemoveButtons[idx].Dispose();
                RemoveButtons.RemoveAt(idx);

                // Remove textbox
                UsernameTextBoxes[idx + 1].Dispose();
                UsernameTextBoxes.RemoveAt(idx + 1);

                //Shift controls up
                for (int i = idx; i < RemoveButtons.Count; i++)
                {
                    UsernameTextBoxes[i + 1].Top -= SpaceDelta;
                }

                space -= SpaceDelta;

                Savelbl.Location = new Point(LabelX, LabelY - spacelbl);

            }
        }

Upvotes: 0

Views: 746

Answers (1)

Derek
Derek

Reputation: 8753

You never update LabelX and LabelY.

LabelX = Savelbl.Location.X
LabelY = Savelbl.Location.Y
Savelbl.Location = new Point(LabelX, LabelY - spacelbl);

You could also get rid of these variables probably...

Upvotes: 1

Related Questions