简体   繁体   中英

Add combobox dynamically with checkbox

I am trying to add a combobox when a checkbox is clicked.

The code I am using is below. As it stands, I can get it to move around a combobox that already exists, but it won't create a new one.

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
    {
        // Determine the CheckState of the check box.  
        if (checkBox1.CheckState == CheckState.Checked)
        {  

            combo.Items.AddRange(new object[] {
                "Item 1",
                "Item 2",
                "Item 3",
                "Item 4",
                "item 5",
                "Item 6"});
            combo.Location = new System.Drawing.Point(19, 123);
            combo.Name = "combo";
            combo.Size = new System.Drawing.Size(121, 21);
            combo.TabIndex = 0;
            combo.SelectedIndexChanged += new System.EventHandler(this.combo_SelectedIndexChanged);
            combo.BringToFront();

            this.AllowDrop = false;
        }
    }

    private void checkBox1_Click(object sender, System.EventArgs e)
    {
        switch (checkBox1.CheckState)
        {
            case CheckState.Checked:
                ComboBox combo = new ComboBox();
                Controls.Add(combo);
                break;
            case CheckState.Unchecked:

                break;
            case CheckState.Indeterminate:

                break;
        }
    }

The way to solve is to be conscious of which function you are placing your statements into. The combobox's settings (the range, location, etc.) will be in the CheckChanged function under the if checked condition. These will go along with the groupbox.controls.add(); and combo.BringToFront(); , just in case it ends up behind something.

private void checkBox1_CheckedChanged(object sender, EventArgs e)
    {
        // Determine the CheckState of the check box.  
        if (checkBox1.CheckState == CheckState.Checked)
        {
            groupBox1.Controls.Add(combo);
            combo.Items.AddRange(new object[] {
                "Item 1",
                "Item 2",
                "Item 3",
                "Item 4",
                "Item 5",
                "Item 6"});
            combo.Location = new System.Drawing.Point(19, 123);
            combo.Name = "combo";
            combo.Size = new System.Drawing.Size(121, 21);
            combo.TabIndex = 0;
            combo.SelectedIndexChanged += new System.EventHandler(this.combo_SelectedIndexChanged);
            combo.BringToFront();

            this.AllowDrop = false;
}

Meanwhile, in the Click function, you must instantiate the combobox and add it.

    private void checkBox1_Click(object sender, System.EventArgs e)
    {
        switch (checkBox1.CheckState)
        {
            case CheckState.Checked:
                ComboBox combo = new ComboBox();
                Controls.Add(combo);
                break;
            case CheckState.Unchecked:

                break;
            case CheckState.Indeterminate:

                break;
        }
    }

If you then want to get rid of the combobox once the checkbox is unchecked, just add

        if (checkBox1.CheckState == CheckState.Unchecked)
        {
            groupBox1.Controls.Remove(combo);
            Controls.Remove(combo);
            combo.Items.Clear();
        }

into the private void checkBox1_CheckedChanged(object sender, EventArgs e) function. Make sure to put combo.items.Clear() or it will add the range of the new instance of the combobox to the old one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM