Adding controls to a panel dynamically.

B

Big D

Hi all,

I'm having a problem adding controls to a panel dynamically. I have a
usercontrol that I create and try to add 5 times to the panel, but I only
ever see the first control. I know it's the first, because a property of
the userControl is to display it's name. The code I have is:

//Add detatil widgets to the panel

CustControls.DetailWidget x;

for (int i = 0; i<5; i++)

{

x = new CustControls.DetailWidget();

x.Name = "asdf" + i.ToString();

x.Location = new Point(i * 10, i * 10);

this.pnlDetailContainer.Controls.Add(x);

}

How is this supposed to work? Should the panel automatically place each
control under one another or next to one another or must that be done
entirely by hand?

Thanks!

-D
 
V

Vijaye Raji

Your code snippet looks right. Why don't you check programmatically if all
the controls are in the container, say

foreach (Control x in this.pnlDetailContainer.Controls)
{
MessageBox.Show(x.Name);
}

If you see all the added controls, then may be it's a paint issue or
something to do with your control itself (like your control choosing its own
location, etc.)

-vJ
 
J

Jeffrey Tan[MSFT]

Hi

I think the problem should be your user control, because I have tried to
add 5 textbox into the panel, it just works well, code snippet like this:

private void Button1_Click(object sender, System.EventArgs e)
{
for(int i=0;i<5;i++)
{
TextBox tb=new TextBox();
tb.Text="textbox"+ i.ToString();
tb.ID=i.ToString();
Panel1.Controls.Add(tb);
}
}

I think you should check the implement of your user control.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top