Click here to Skip to main content
15,918,742 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a panel in my form , and i am adding dynamic buttons on it at runtime
but the buttons do not appear on the screen
this is my code
C#
int basex = panel1.Location.X;
int basey = panel1.Location.Y;
            for (int i = 0; i < 5; i++)
            {
                Button b = new Button();
                b.Left = basex;
               b.Top = basey;
                basey += 50;
                b.Name = String.Format("btnDriver{0}", i + 1);
                b.Text = String.Format("btnDriver{0}", i + 1);
                b.Click += new EventHandler(b_Click);
                panel1.Controls.Add(b);
            }
Posted
Updated 28-May-20 0:59am
v3
Comments
[no name] 20-Aug-13 10:02am    
Your code works perfectly fine for me. Check the height of your panel. Or try adding the buttons horizontally instead of vertically.
Reza Oruji 25-Aug-13 11:15am    
when i make panel bigger it shows the buttons in the middle of the panel .but why i have to resize the panel ?
[no name] 25-Aug-13 15:50pm    
Because the panel is not big enough to begin with? 50 on your y value is quite a bit. Try a smaller number.
Neema Derakhshan 25-Aug-13 3:31am    
Reza jan, agar ba 1 css be bitton padding bedid kar mikone.

dear Reza, set padding for the button by css, it will work.
[no name] 25-Aug-13 7:02am    
How would you expect CSS to work in a Winforms application?

Try to specify a button size:
C#
int basex = panel1.Location.X;
int basey = panel1.Location.Y;
            for (int i = 0; i < 5; i++)
            {
                Button b = new Button();
                b.Left = basex;
               b.Top = basey;
                b.Size = new Size(100, 50); // <== add this line
                basey += 50;
                
 
                b.Name = String.Format("btnDriver{0}", i + 1);
                b.Text = String.Format("btnDriver{0}", i + 1);
                
                
                
                b.Click += new EventHandler(b_Click);
                panel1.Controls.Add(b);
            }

Hope this helps.
 
Share this answer
 
Comments
Reza Oruji 20-Aug-13 6:17am    
it doesn't work. when i add directly to form, it works but not in panel
Thomas Daniels 20-Aug-13 10:51am    
Is the panel added to the form?
Your original code works fine for me without any change: the most likely hypotheses for what you observe are:

1. you have not added the Panel to the Form's ControlCollection, or some other valid, and visible, ContainerControl, inside the Form.

2. or, you set the Panel's 'Visible Property to 'false.
 
Share this answer
 
i used FlowLayoutPanel and TableLayoutPanel .it solved my problem.
no need to do anything else...
C#
int basex = panel1.Location.X;
   int basey = panel1.Location.Y;
   for (int i = 0; i < 5; i++)
   {
       Button b = new Button();
       b.Left = basex;
       b.Top = basey;
       basey += 50;
       b.Name = String.Format("btnDriver{0}", i + 1);
       b.Text = String.Format("btnDriver{0}", i + 1);
       b.Click += new EventHandler(b_Click);
       flowLayoutPanel1.Controls.Add(b);
   }
 
Share this answer
 
Take Panel control in your .aspx page and after that write code on code behind Page_Load event
as like:

C#
protected void Page_Load(object sender, EventArgs e)
    {
        Button b = new Button();
        //form1.Controls.Add(b);
        Panel1.Controls.Add(b);
        b.Text = "Click";
        b.Click+=new EventHandler(b_Click);
    }


It's working properly......If you getting any problem tell me will solve easily.....:-)
 
Share this answer
 
v2
Comments
[no name] 24-Aug-13 10:38am    
Winform applications do not have aspx pages.
Chintan Desai1988 30-Aug-13 2:22am    
you should to mansion that....k will surely give you a proper answer....:-)
[no name] 30-Aug-13 8:37am    
I don't need a proper answer. I already knew the answer. And the OP clearly tagged his question as being for Winforms.
Because of wrong location button is not visible.
The location of the button you have set according to form (which is not buttons parent). You should add location with respect to Panel. Just to test you can add the location as (5,5).Then your button will be visible on Panel.
 
Share this answer
 
Comments
CHill60 28-May-20 8:04am    
Sort of what was discussed in the comments back on 25-Aug-13

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