Reputation: 9
Here is the code. Help will be much obliged. I want the panels to be added to tabePage1, but it is being added to the form instead.
private void tabPage1_Click(object sender, EventArgs e)
{
int i, j;
for (i = 1; i <= 3; i++)
{
for (j = 1; j <= 4; j++)
{
Panel a = new Panel();
a.Location = new Point(i * 200, j * 50);
a.Width = 180;
a.Height = 40;
a.Name = "Rom " + (((i * 4) - 3) + (j - 1));
a.BackColor = Color.Yellow;
a.AllowDrop = true;
a.DragDrop += new System.Windows.Forms.DragEventHandler(this.panel1_DragDrop);
a.DragOver += new System.Windows.Forms.DragEventHandler(this.panel1_DragOver);
a.Visible = true;
Label l = new Label();
l.Location = new Point(10, 10);
l.Width = 180;
l.Text = a.Name;
a.Controls.Add(l);
l.AllowDrop = true;
this.Controls.Add(a);
Upvotes: 0
Views: 2110
Reputation: 8551
This code:
this.Controls.Add(a);
adds the control to the Form
, because your tabPage1_Click
method is in a subclass of Form
(making this
refer to the Form
). To add the panels to tabPage1
, do this instead:
tabPage1.Controls.Add(a);
Incidentally, do you really want to add all the panels to a single TabPage
, or do you want to create a TabPage
for each panel? If the latter, the code will obviously look different.
Edit: in answer to your comment, you can add to a different TabPage
by referring to it by name as above (e.g. tabPage2.Controls.Add(a);
) or, if you want to add a set of panels to each TabPage
in your TabControl
, you could do something like this:
foreach (TabPage tp in yourTabControl.TabPages)
{
// create panel...
tp.Controls.Add(a);
// ...
}
Upvotes: 1