0

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

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!

datacat
  • 17
  • 3

1 Answers1

0

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 = {"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);
        }
    }
GaB
  • 96
  • 3