Adding Panels Dynamically

  • Thread starter Thread starter hijack717
  • Start date Start date
H

hijack717

Guest
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))
{
listBox1.Items.Add(checkedListBox1.CheckedItems);

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

Continue reading...
 
Back
Top