在窗口应用程序中在运行时添加控件 [英] Adding Controls at Run Time in window application

查看:61
本文介绍了在窗口应用程序中在运行时添加控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿专业



有问题,这就是我不知道的问题

问题是我正在添加复选框控件动态地使用此代码



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

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





但是在执行此程序后只添加了一个控件我添加了5个控件根据我的循环.......



有任何答案....... ???



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.......???

推荐答案

对我来说它添加了5个控件。



检查下面稍微修改过的代码(而不是我已经输入ID的名字):



For me its adding 5 controls.

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

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


也许它只是一个视觉效果而且它们都是在同一个地方绘制的。尝试添加
Maybe it''s just a visual thing and they are drawn all in the same place. Try adding
objcheckbox.Location = new Point(0, 10 * i);

到你的循环。

这应该将复选框放在另一个之下。

to your loop.
That should place the checkboxes one below the other.


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


这篇关于在窗口应用程序中在运行时添加控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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