简体   繁体   中英

Create labels dynamically depending of CheckedListBox c# winforms

I have a checkedListBox in a TabControl

What I want is to create a label and NumericUpDown dynamically , when User check an item of checkedListBox it will show the new label and NumericUpDown

Then , when it Unchecked this item ,The numericUpDown will be clear (empty).

Conclusion: As many checked items , as many w've create labels and NumericUpDowns .

Please, how will I do that ??

For each checkbox item in your checkedListBox in properties switch to events and create subscriber checkBoxName_CheckStateChanged for event CheckStateChanged. The code in the sucriber can be like this:

 private void checkBox1_CheckStateChanged(object sender, EventArgs e)
    {
        var source = sender as CheckBox;
        if (source.Checked == true)
        {
            this.numericUpDown1.Text = "TextWhenChecked";
            this.labelAtTheNumericUpDown.Text = "TextWhenChecked";
        }
        else
        {
            this.numericUpDown1.Text = "TextWhenUnchecked";
            this.label1.Text = "TextWhenUnchecked";
        }
    }

You fill the strings as you want. These are only examples. To have only checkBox checked at a time look at here: https://stackoverflow.com/a/24693858/6650581 .

What you need to do is creating Label and NumericUpDown manually and show it by adding to Controls collection. A TableLayoutPanel can help you arranging controls without setting Size and calculate Location manually. Here is an example:

public class MainForm : Form
{
    private CheckedListBox checkedListBox;
    private TableLayoutPanel tableLayoutPanel;

    public MainForm()
    {
        InitializeComponent();

        //Fill checkedListBox and create controls
        for( int i = 0; i <= 5; i++ )
        {
            checkedListBox.Items.Add( i.ToString() );
            Label lbl = new Label()
            {
                Name = "lbl" + i,
                Text = "Label " + i,
                Visible = false
            };

            NumericUpDown num = new NumericUpDown()
            {
                Name = "num" + i,
                Value = i,
                Visible = false
            };

            tableLayoutPanel.Controls.Add( lbl, 0, i );
            tableLayoutPanel.Controls.Add( num, 1, i );
        }
    }

    private void checkedListBox_ItemCheck( object sender, ItemCheckEventArgs e )
    {
        if( e.NewValue == CheckState.Checked )
        {
            tableLayoutPanel.Controls["lbl" + e.Index].Visible = true;
            tableLayoutPanel.Controls["num" + e.Index].Visible = true;
        }
        else
        {
            tableLayoutPanel.Controls["lbl" + e.Index].Visible = false;
            ((NumericUpDown)tableLayoutPanel.Controls["num" + e.Index]).Value = 0M;
        }
    }

    private void InitializeComponent()
    {
        this.checkedListBox = new System.Windows.Forms.CheckedListBox();
        this.tableLayoutPanel = new System.Windows.Forms.TableLayoutPanel();
        this.SuspendLayout();
        // 
        // checkedListBox
        // 
        this.checkedListBox.Location = new System.Drawing.Point(8, 8);
        this.checkedListBox.Name = "checkedListBox";
        this.checkedListBox.Size = new System.Drawing.Size(200, 100);
        this.checkedListBox.TabIndex = 1;
        this.checkedListBox.ItemCheck += new System.Windows.Forms.ItemCheckEventHandler(this.checkedListBox_ItemCheck);
        // 
        // tableLayoutPanel
        // 
        this.tableLayoutPanel.AutoScroll = true;
        this.tableLayoutPanel.ColumnCount = 2;
        this.tableLayoutPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
        this.tableLayoutPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
        this.tableLayoutPanel.Location = new System.Drawing.Point(8, 112);
        this.tableLayoutPanel.Name = "tableLayoutPanel";
        this.tableLayoutPanel.Size = new System.Drawing.Size(200, 100);
        this.tableLayoutPanel.TabIndex = 2;
        // 
        // MainForm
        // 
        this.ClientSize = new System.Drawing.Size(223, 227);
        this.Controls.Add(this.tableLayoutPanel);
        this.Controls.Add(this.checkedListBox);
        this.Name = "MainForm";
        this.ResumeLayout(false);
    }
}

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