开发者

How do I remove combo box dynamically in C# by pressing a button in runtime?

I am currently trying to make a form which works dynamically. I created a flow layout panel, add button, and remove button on the form. When I click on the add button, combo box appears inside the panel and keeps adding if I click on add button from top to bottom.

Problem is I don't know how to remove one by one. For example, if I added three combo box by clicking ad开发者_运维问答d button 3 times(say combo box 1, 2, 3), I want to remove them in order 3, 2, 1 as I click remove button.

How do I do this in C#?

Thank you very much.


Try this:

flowLayoutPanel.Controls.RemoveAt(flowLayoutPanel.Controls.Count - 1);

Refer to Control.ControlCollection.RemoveAt Method


I think you can use a combo box control array to keep track of the combo boxes created, have a look o this article.


Try This

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Drawing;

namespace WindowsFormsApplication2 { public partial class Form1 : Form { public Form1() { InitializeComponent(); }

    private void button1_Click(object sender, EventArgs e)
    {
        ComboBox b1 = new ComboBox();
        b1.BackColor = Color.Blue;

        flp.Controls.Add(b1);

        b1.Text = b1.TabIndex.ToString();

    }

    private void button2_Click(object sender, EventArgs e)
    {
        if (flp.Controls.Count > 0)
        {

            flp.Controls.RemoveAt(flp.Controls.Count - 1);
        }
    }



}

}

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜