Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hey professional

there is a problem and that is what i don't know
problem is i am adding checkbox control dynamicly with this code

C#
public void AddDynamicControl()
       {
           int j=5;
       for(int i=1; i<=j; i++)
           {
               CheckBox objcheckbox = new CheckBox();
               objcheckbox.Name = i.ToString();
               objcheckbox.Text = i.ToString() + "checkbox";

           panel1.Controls.Add(objcheckbox);
           }
       }


but its adding only one control after execution of this program therefor i am adding 5 controls according to my for loop.......

have any answer.......???
Posted
Comments
Sergey Alexandrovich Kryukov 13-May-13 4:10am    
All controls are always added during run time; you simply may not noticed that.
To get a valid answer, you always need to tag the UI library or application type your use.
—SA
Shubh Agrahari 13-May-13 4:21am    
no its wrapping problem SA ma all control taking same place in container thats why only one control looks at runtime..i got ma answer but thanks to all.

For me its adding 5 controls.

Check the slightly modified code below(Instead of Name I have put ID):

C#
protected void Button1_Click(object sender, EventArgs e)
        {
            int j = 5;
            for (int i = 1; i <= j; i++)
            {
                CheckBox objcheckbox = new CheckBox();
                objcheckbox.ID = i.ToString();
                objcheckbox.Text = i.ToString() + "checkbox";

                Panel1.Controls.Add(objcheckbox);
            }
        }
 
Share this answer
 
v2
Comments
Shubh Agrahari 13-May-13 4:02am    
but there is did not showing property like ID in Intellisence in win form i think ID property stand in ASP.net but here Windows application there is id becomes Name property......
Maybe it's just a visual thing and they are drawn all in the same place. Try adding
C#
objcheckbox.Location = new Point(0, 10 * i);
to your loop.
That should place the checkboxes one below the other.
 
Share this answer
 
Comments
Shubh Agrahari 13-May-13 4:18am    
thanks lukeer for giving sudden spark, i also think that my all control taking same place....
C#
public void AddDynamicControl()
       {
           int j=5;
       for(int i=1; i<=j; i++)
           {
               CheckBox objcheckbox = new CheckBox("CheckBox"+i);
               objcheckbox.Name = i.ToString();
               objcheckbox.Text = i.ToString() + "checkbox";

           panel1.Controls.Add(objcheckbox);
           }
       }


just do like this
 
Share this answer
 
Comments
Mohammed Hameed 13-May-13 3:38am    
What have been changed??

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