Add a TextBox when an item in a CheckedListBox is checked

huangapple go评论72阅读模式
英文:

Add a TextBox when an item in a CheckedListBox is checked

问题

以下是代码的翻译部分:

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);
    }
}

希望这对你有所帮助。如果有其他问题,请随时提问。

英文:

I have a programmatically created checkedListBox that has several options. What I want to be able to do is, when a user 'checks' an item in the checkedListBox, programmatically create a TextBox based on what was checked 'live' (without having to click another button or anything to create the TextBox).
(Example)
The text box should also disappear if the option is unchecked.

So far here is what I have:

        private void rangetypecheckedlistbox_ItemChecked(object sender, ItemCheckEventArgs e)
        {

            List&lt;string&gt; checkedrangetypes = new List&lt;string&gt;();
            foreach (object item in ((CheckedListBox)panel_corebonusbonuses.Controls[&quot;comboBox_corebonusbonuses_range_type_input&quot;]).CheckedItems)
            {
                checkedrangetypes.Add(item.ToString());
            }
            foreach (string item in checkedrangetypes)
            {

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

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

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

                checkedrangetypes.Remove(item);
            }
        }

But it doesn't seem to create the textbox when I select a new item.
I am relatively new to c# and winforms and this is my first app in the language, so any help and explanation would be greatly appreciated!

答案1

得分: 0

你可能知道,在WebForms中,每次点击都被视为一个事件,对吗?你可以在复选框被点击时使用 OnClick 事件,每个事件都包含被点击元素的ID。通过ID,你可以访问元素的其他属性。

示例:(我没有一个活跃的设置来测试这个...你的代码应该类似于下面的样子)

private System.Windows.Forms.CheckedListBox checkedListBox1;

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

public Form1(){

    //初始化
    this.checkedListBox1 = new System.Windows.Forms.CheckedListBox();

    //在CheckedListBox中设置初始对象。
    string[] myFruit = {"Range", "Threat","Blast"};

    //将选择模式从双击更改为单击。
    checkedListBox1.CheckOnClick = true;

    //添加事件监听器
    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(); //创建一个Panel来添加文本框和标签

        //创建标签
        var newLabel = new Label();
        newLabel.Text = item;

        //创建文本框
        var newTextbox = new TextBox();
        newTextbox.ID = item + "_text_box";

        //将标签和文本框添加到面板,然后将面板添加到表单
        mPanel.Controls.Add(newLabel);
        mPanel.Controls.Add(newTextbox);
        form1.Controls.Add(mPanel);
    }
}

请注意,我已经为您翻译了代码中的注释和字符串,以便更容易理解。

英文:

You may know every click is considered as an Event in WebForms right?
You can use the OnClick Event when a checkbox is clicked and every Event contains ID of the the element clicked. Through ID, you can access other properties of the Element.

Example: (I don't have an active setup to test this... Your Code should look like something below)

    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 = {&quot;Range&quot;, &quot;Threat&quot;,&quot;Blast&quot;};

        // 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&lt;string&gt; checkedItems = new List&lt;string&gt;();
        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 + &quot;_text_box&quot;;
	
	        // 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);
        }
    }

huangapple
  • 本文由 发表于 2023年7月23日 23:52:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76749137.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定