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

时间:2016-09-19 16:02:05

标签: c# winforms checkboxlist

我在 TabControl

中有 checkedListBox

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

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

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

拜托,我该怎么做?

2 个答案:

答案 0 :(得分:0)

对于属性中checkedListBox中的每个复选框项,切换到事件并为事件CheckStateChanged创建订阅者checkBoxName_CheckStateChanged。 sucriber中的代码可以是这样的:

import UIKit
import AVFoundation

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        do {
            try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient)
        } catch _ {
        }
        return true
    }
}

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

答案 1 :(得分:0)

您需要做的是手动创建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);
    }
}
相关问题