且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

向面板添加动态按钮

更新时间:2023-11-30 16:13:40

尝试指定按钮大小:

Try to specify a button size:
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.


您的原始代码对我来说没有任何改变:最可能的假设你观察到的是:



1.你没有将Panel添加到Form的ControlCollection,或者Form中的一些其他有效且可见的ContainerControl。 br />


2.或者,你将Panel的'Visible Property'设置为false。
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.


我使用了FlowLayoutPanel和TableLayoutPanel。它解决了我的问题问题。

无需做任何其他事情......

i used FlowLayoutPanel and TableLayoutPanel .it solved my problem.
no need to do anything else...
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);
   }