Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Windows
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.
Posted 9-May-12 7:32am
Comments
SAKryukov at 9-May-12 14:14pm
   
You need to tag your application type or UI library, such as WPF or Forms.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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,
—SA
  Permalink  
v2
Comments
Sandeep Mewara at 9-May-12 14:31pm
   
Good suggestion and way to learn. 5!
SAKryukov at 9-May-12 14:32pm
   
Thank you, Sandeep.
It's tested on myself when I started to learn some things like that.
--SA
Sandeep Mewara at 9-May-12 14:34pm
   
Add +1 to tested candidates. :)
SAKryukov at 9-May-12 17:05pm
   
:-)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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 />"));
 

UPDATE:
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
this.Controls.Add(tb1);
  Permalink  
v6
Comments
Dave Kreskowiak at 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 at 9-May-12 13:32pm
   
Agreed. My bad.
SAKryukov at 9-May-12 14:14pm
   
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.
--SA
Sandeep Mewara at 9-May-12 14:30pm
   
Updated my answer to keep it on! :)
 
Thanks.
SAKryukov at 9-May-12 14:19pm
   
I added my own very universal approach to such problems -- please see my answer.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
Comments
qwerty 2 at 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 at 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)

  Print Answers RSS
0 OriginalGriff 240
1 Kamal Rocks 184
2 CPallini 155
3 PIEBALDconsult 150
4 BillWoodruff 148
0 OriginalGriff 5,695
1 DamithSL 4,506
2 Maciej Los 4,007
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,180


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 9 May 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100