Click here to Skip to main content
14,936,036 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I am adding a control dynamically to a panel depending on the requirement i.e
the same process can repeat and add the same controls again depending on the count(no of times to be added).
But the thing is am able to see only the last added control instead the space is taken when created for the previous controls. Am not able to see the other controls.
So how can i make each added control visible?
Thanks in advance.
You need to tag your application type or UI library, such as WPF or Forms.

I usually advise a very general method of solving such problems. If you don't know how to put some control, create some layout, etc., try to do it using the designer first. Test the application to see the results. If this is what you want, locate the auto-generated code and learn how it works. You will need to use exact same techniques. Only don't use ugly naming of the variables and method designers usually use — such names violate (good) Microsoft naming conventions. Give everything semantically sensible names, without numeric characters and other "automated" dirt.

Good luck,
Sandeep Mewara 9-May-12 14:31pm
Good suggestion and way to learn. 5!
Thank you, Sandeep.
It's tested on myself when I started to learn some things like that.
Sandeep Mewara 9-May-12 14:34pm
Add +1 to tested candidates. :)
If you were working on ASP.NET then the following answer would have applied. As such, it's of no use.

am able to see only the last added control instead the space is taken when created for the previous controls. Am not able to see the other controls.
So how can i make each added control visible

Add a line break after every panel to push the next one in a new line.

One of the ways to add it:
myPanel.Controls.Add(new LiteralControl("<br />"));

As Dave has already pointed out, for Winforms, you need to provide the Left & Top property of the control. Something like:
TextBox tb1 = new TextBox();

// set up the left and top position (the same as changing the location in the IDE)
tb1.Left = 100;
tb1.Top = 100;

// put any default text if you want
tb1.Text = "Hello World!"

// add the newly created control to the form
Dave Kreskowiak 9-May-12 12:55pm
You did see the tags at the top of his post that said "C# Windows". Where in there did you see "ASP.NET"? In other words, you're reply won't work at all in a Windows Forms app.
Sandeep Mewara 9-May-12 13:32pm
Agreed. My bad.
Will you fix your random mistake? I voted 5 -- but in advance. The idea is correct in the sense that the failure to add a control as a child (to establish child-parent relationship) is a most typical mistake. With other application type and UI libraries it looks very similar.
Sandeep Mewara 9-May-12 14:30pm
Updated my answer to keep it on! :)

I added my own very universal approach to such problems -- please see my answer.
Without seeing the code you're using for creating these controls in a loop, we can only guess.

Chances are good you're not setting the Top and Left (alternatively the Location) properties of the controls, so they are all sitting on top of each other at 0,0, or, you are setting every control to the same location. In either case, you will only see the last control added.

You have to add an appropriate offset to the Top and/or Left properties of each control in order to move them off the last control that was added. Exactly what you do here is dependent on your requirements.
qwerty 2 10-May-12 2:05am
But the space for each control is created but not visible. Only the last control added is visible.
There is no problem with the space i.e they are not sitting on top of each other as you thought.The space is created but not visible.
Dave Kreskowiak 10-May-12 8:18am
What did I say?? I said "Without seeing the code you're using for creating these controls", we have to GUESS at what you're problem is. I told you what the MOST COMMON MISTAKE is given the description you provided.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900