winforms 选中CheckedListBox中的项时添加TextBox

eeq64g8w  于 2023-08-07  发布在  其他
关注(0)|答案(1)|浏览(120)

我有一个以编程方式创建的checkedListBox,它有几个选项。我希望能够做的是,当用户“选中”checkedListBox中的一个项目时,以编程方式根据“实时”选中的内容创建一个TextBox(而不必单击其他按钮或任何东西来创建TextBox)。(Example)如果未选中该选项,则文本框也应消失。
到目前为止,这是我所拥有的:

private void rangetypecheckedlistbox_ItemChecked(object sender, ItemCheckEventArgs e)
        {

            List<string> checkedrangetypes = new List<string>();
            foreach (object item in ((CheckedListBox)panel_corebonusbonuses.Controls["comboBox_corebonusbonuses_range_type_input"]).CheckedItems)
            {
                checkedrangetypes.Add(item.ToString());
            }
            foreach (string item in checkedrangetypes)
            {

                Label newdynamicvallabel = new Label();
                newdynamicvallabel.Size = new Size(201, 20);
                newdynamicvallabel.Name = "dynamic_label" + item;
                newdynamicvallabel.Location = new Point(100, 130);
                newdynamicvallabel.Text = item.ToUpper() + " VALUE";

                //Create range_type textbox.
                TextBox newdynamicvaltext = new TextBox();
                newdynamicvaltext.Size = new Size(272, 28);
                newdynamicvaltext.Name = "dynamic_textbox_" + item + "_input";
                newdynamicvaltext.Location = new Point(381, 127);
                newdynamicvaltext.BorderStyle = BorderStyle.FixedSingle;

                panel_corebonusbonuses.Controls.Add(newdynamicvallabel);
                panel_corebonusbonuses.Controls.Add(newdynamicvaltext);

                checkedrangetypes.Remove(item);
            }
        }

字符串
但是当我选择一个新项目时,它似乎并没有创建文本框。我对c#和winforms还比较陌生,这是我第一个使用这种语言的应用程序,所以如果有任何帮助和解释,我将不胜感激!

bqf10yzr

bqf10yzr1#

你可能知道每次点击都被认为是WebForms中的一个事件,对吗?您可以在单击复选框时使用 OnClick 事件,并且每个事件都包含所单击元素的ID。通过ID,您可以访问元素的其他属性。
示例:(我没有活动设置来测试此...你的代码应该看起来像下面的东西)

private System.Windows.Forms.CheckedListBox checkedListBox1;

    public static void Main(string[] args) 
    {
      Application.Run(new Form1());
    }

    public Form1(){

        //Initialize
        this.checkedListBox1 = new System.Windows.Forms.CheckedListBox();

        // Sets up the initial objects in the CheckedListBox.
        string[] myFruit = {"Range", "Threat","Blast"};

        // Changes the selection mode from double-click to single click.
        checkedListBox1.CheckOnClick = true;

       //Adding Event Listener
       this.checkedListBox1.ItemCheck += new System.Windows.Forms.ItemCheckEventHandler(this.checkedListBox1_ItemCheck);

    }

    private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
    {                     
        List<string> checkedItems = new List<string>();
        foreach (var item in checkedListBox1.CheckedItems)
    checkedItems.Add(item.ToString());

        if (e.NewValue == CheckState.Checked)
            checkedItems.Add(checkedListBox1.Items[e.Index].ToString());
        else
            checkedItems.Remove(checkedListBox1.Items[e.Index].ToString());

        foreach (string item in checkedItems)
        {
            var mPanel = new Panel(); //Cretae a Panel to add Textbox and Label
    
            //Create Label
            var newLabel = new Label();
            newLabel.Text = item;
    
            //Create text Box
            var newTextbox = new TextBox();
            newTextbox.ID = item + "_text_box";
    
            // add the label and textbox to the panel, then add the panel to the form
            mPanel.Controls.Add(newLabel);
            mPanel.Controls.Add(newTextbox);
            form1.Controls.Add(mPanel);
        }
    }

字符串

相关问题