简体   繁体   English

根据 CheckedListBox c# winforms 动态创建标签

[英]Create labels dynamically depending of CheckedListBox c# winforms

I have a checkedListBox in a TabControl我在TabControl 中有一个checkedListBox

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我想要的是动态创建一个标签NumericUpDown ,当用户检查checkedListBox 的项目时,它将显示新标签和NumericUpDown

Then , when it Unchecked this item ,The numericUpDown will be clear (empty).然后,当它取消选中此项时, numericUpDown 将被清除(空)。

Conclusion: As many checked items , as many w've create labels and NumericUpDowns .结论:尽可能多的检查项目,尽可能多的创建标签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.对于属性中的 checkedListBox 中的每个复选框项,切换到事件并为事件 CheckStateChanged 创建订阅者 checkBoxName_CheckStateChanged。 The code in the sucriber can be like this: sucriber 中的代码可以是这样的:

 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 .要一次只检查复选框,请查看此处: 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.您需要做的是手动创建 Label 和 NumericUpDown 并通过添加到 Controls 集合来显示它。 A TableLayoutPanel can help you arranging controls without setting Size and calculate Location manually. TableLayoutPanel 可以帮助您安排控件,而无需手动设置大小和计算位置。 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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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