none
Adding Panels Dynamically RRS feed

  • Question

  • I'm have a listBox of categories that are added through user input via a label and a checkedListBox. I'm trying to get a way to add panels dynamically based on the number of categories in the listBox. I need to use hide/show for each panel since I want the information to switch between each panel. 

    I have this for adding the new panel dynamically based on each new category in the listBox:

    private void button2_Click(object sender, EventArgs e)
    {
      for(int i = 0; i < checkedListBox1.CheckedItems.Count; i++)
      {
       if (!listBox1.Items.Contains(checkedListBox1.CheckedItems[i]))
       {
                        listBox1.Items.Add(checkedListBox1.CheckedItems[i]);
    
                        newPanel.Name = "panel " + (i+2).ToString();
                        newPanel.Location = new Point(405, 10);
                        newPanel.Parent = this;
                        newPanel.Size = new Size(341, 333);
                        newPanel.Visible = false;
                        newPanel.Controls.Add(nameLabel);
                        newPanel.Controls.Add(testButton);
                        listPanel.Add(newPanel);
                        newPanel.Show();
          }
       }
    }

    And then for whichever category is clicked on the sub-category it should open the respective panel using this code: 

    private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
            {
                int index = this.listBox1.IndexFromPoint(e.Location);
    
                if(index != System.Windows.Forms.ListBox.NoMatches)
                {
                    if(index < listPanel.Count - 1)
                    {
                        //nameLabel.Text = "panel " + listPanel[index++].ToString();
                        listPanel[index++].Visible = true;
                        MessageBox.Show("" + listPanel.Count.ToString());
                        listPanel[--index].Show();
    
                    }
                }
            }

    I just either get no respective panel opening or an out-of-range index at listPanel[--index].Show();

    Thanks for your help

    • Moved by CoolDadTx Wednesday, November 13, 2019 2:40 PM Winforms related
    Wednesday, November 13, 2019 12:31 PM

All replies

  • If you need more panels, labels and buttons, then you should create them using new. On each iteration of your loop, inside the if block, you should have something like this:

       var newPanel = new Panel( );

       var nameLabel = new Label( );

       var testButton = new Button( );

    Then set the properties, add controls to collections, etc.

    In order to simplify the design and management of this kind of compound controls, there is a special concept: “User Controls”.

    Wednesday, November 13, 2019 4:00 PM
  • Hi hijack717,

    I checked your code and found that the value of the index has not changed after executing the following two statements.

    listPanel[index++].Visible = true; 
    listPanel[--index].Show();

    So the same panel is always displayed.

    When click the category and open the respective panel, please refer to the following code.

    private void ListBox1_MouseDoubleClick(object sender, MouseEventArgs e)
    {
          int index = this.listBox1.IndexFromPoint(e.Location);
    
          if (index != System.Windows.Forms.ListBox.NoMatches)
          {
              if (index < listPanel.Count)
              {
                  nameLabel.Text = "panel " +index.ToString();
                  listPanel[index].Visible = true;                  
                  MessageBox.Show("" + listPanel.Count.ToString());
                  listPanel[index].Show();
               }
          }
    }

    Best Regards,

    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, November 14, 2019 6:21 AM