繁体   English   中英

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

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

我在TabControl 中有一个checkedListBox

我想要的是动态创建一个标签NumericUpDown ,当用户检查checkedListBox 的项目时,它将显示新标签和NumericUpDown

然后,当它取消选中此项时, numericUpDown 将被清除(空)。

结论:尽可能多的检查项目,尽可能多的创建标签NumericUpDowns

请问,我该怎么做??

对于属性中的 checkedListBox 中的每个复选框项,切换到事件并为事件 CheckStateChanged 创建订阅者 checkBoxName_CheckStateChanged。 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";
        }
    }

您可以根据需要填充字符串。 这些只是示例。 要一次只检查复选框,请查看此处: https : //stackoverflow.com/a/24693858/6650581

您需要做的是手动创建 Label 和 NumericUpDown 并通过添加到 Controls 集合来显示它。 TableLayoutPanel 可以帮助您安排控件,而无需手动设置大小和计算位置。 下面是一个例子:

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