开发者

Removing a control from Windows Form and adjusting other controls to fit

I have a Windows Form App that I can add user controls to by clicking a button. I can also delete one control at a time by selecting it and clicking a delete button. All that works fine but if I add three controls and delete the second there is an gap between 开发者_开发问答the two remaining controls on the form. I would like to have all the remaining controls move up to fill in the gap.

Like this:

//Add three controls and we have this:

Control1

Control2

Control3

//Delete Control2 and we now have this:

Control1

Control3

//What I would like is this:

Control1

Control3

Here is what I have tried but it isn't doing the job

private void btQuestionDelete_Click(object sender, EventArgs e)
{
    bool adjustMode = false;
    int height = 0;

    foreach (Control c in this.tpQuestions.Controls)
    {
        if (c is QuestionControl)
        {
            if (adjustMode)
            {
                int moveUpBy = c.Top - height;
                c.Top = moveUpBy;
                c.Parent.Refresh();
            }
            else
            {
                QuestionControl q = (QuestionControl)c;
                if (q.IsSelected)
                {
                    this.tpQuestions.Controls.Remove(c);
                    adjustMode = true;
                    height = q.Height;
                }
            }
        }
    }
}


maybe you want to have a look at flowLayoutPanels ?


To add a control just do this:

public void AddControl(ControlToAdd item)
{
    item.Dock = DockStyle.Top;

    this.Controls.Add(item);
}

To remove a control just Dispose it! All remaining controls will slide up!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜