向面板添加动态按钮 [英] adding dynamic buttons to panel

查看:90
本文介绍了向面板添加动态按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的表格中有一个面板,我在运行时添加了动态按钮

但按钮没有出现在屏幕上

这是我的代码

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

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);
            }

推荐答案

尝试指定按钮大小:

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);
   }


这篇关于向面板添加动态按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆