c#winform怎样实现c 动态生成控件件

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
您的访问请求被拒绝 403 Forbidden - ITeye技术社区
您的访问请求被拒绝
亲爱的会员,您的IP地址所在网段被ITeye拒绝服务,这可能是以下两种情况导致:
一、您所在的网段内有网络爬虫大量抓取ITeye网页,为保证其他人流畅的访问ITeye,该网段被ITeye拒绝
二、您通过某个代理服务器访问ITeye网站,该代理服务器被网络爬虫利用,大量抓取ITeye网页
请您点击按钮解除封锁&C#_动态生成控件以及添加事件处理 - 摩根船长 - 博客园
  有时候需要在程序运行到一定时候或者某个事件之后需要动态生成控件 在C#下一般方式是: private B ...
  有时候需要在程序运行到一定时候或者某个事件之后需要动态生成控件
  在C#下一般方式是:
    private Button Db=new Button() ;     Db.Name="Dy_Button"           //设定名称     Db.Location=new Point(100,200);//设定位置     。。。。//其他属性设置   //这里添加消息处理     。。     this.Controls.Add (Db);//添加到控件组中
  这样就完成了动态生成但是需要给控件添加消息处理事件
  那么现在就需要在生成控件添加到控件组前增加如下语句:
  DPB.MouseClick += new EventHandler(this.pictureBox_MouseClick);
  现在编写这个消息处理的函数pictureBox_Click()
        private void pictureBox_MouseClick(object sender,EventArgs e)        {            MessageBox.Show("click");        }
  这样完成了事件处理的添加
  首先,创建一个全局变量"i "用来区分各个新的按钮:
private int i=0;
  然后在已有的按钮中添加方法如下:
   private void button1_Click(object sender, System.EventArgs e)   {    i++;    Button b = new Button();//创建一个新的按钮    b.Name="b"+i;//这是我用来区别各个按钮的办法    System.Drawing.Point p = new Point(12,13+i*30);//创建一个坐标,用来给新的按钮定位    b.Location =//把按钮的位置与刚创建的坐标绑定在一起       panel1.Controls.Add(b);//向panel中添加此按钮    b.Click += new System.EventHandler(btn_click);//将按钮的方法绑定到按钮的单击事件中b.Click是按钮的单击事件   }
完成以上步骤就已经可以进行动态按钮的创建 下面我们来讲如何对新建的按钮添加对应的事件方法:
btn_click(): private void btn_click(object sender, System.EventArgs e) { Button b1 = (Button)//将触发此事
完成以上步骤就已经可以进行动态按钮的创建
下面我们来讲如何对新建的按钮添加对应的事件方法btn_click():
   private void btn_click(object sender, System.EventArgs e)   {    Button b1 = (Button)//将触发此事件的对象转换为该Button对象        MessageBox.Show(""+b1.Name);   }
  至此就已经完成了动态创建按钮和事件
  ASP.net为控件动态添加事件
  实现的功能是在网页上的Panel中动态添加一个Button,并为这个Button写一个单击事件。
  动态添加控件的事件,语句:
<mand += new CommandEventHandler(this.EventFun);
  具体的代码请看下面:
  需要特别注意的是:
  添加控件和给控件加事件时绝对不能放到 if(!IsPostback){}里面,那样的话,点击一次后控件会消失,而且事件也不
  会执行。
protected void Page_Load(object sender, EventArgs e)    {       //将输入字符串分析为System.Web.UI.Control对象,b为传进去的值        Control c = ParseControl("&asp:Button Text = '按我' ID = 'myButton' commandargument = 'b' runat = 'server' /&");        //将控件添加大Panel中        this.Panel1.Controls.Add(c);         //查找页面名为myButton的控件        Button Button = (Button)Page.FindControl("myButton");         //添加事件On_Button        mand += new CommandEventHandler(this.On_Button);            }    //CommandEventArgs为command事件提供数据    protected void On_Button(Object sender,CommandEventArgs e)    {        Response.Write("&script language = 'JavaScript' type = 'text/Javascript'&alert('" + e.CommandArgument.ToString() + "');&/script&");    }
动态添加控件并添加事件
private void Page_Load(object sender, System.EventArgs e) { Button Button1 = new Button(); mandArgument = b1; Button1.Text = Btn1; mand += new CommandEvent
  动态添加控件并添加事件
private void Page_Load(object sender, System.EventArgs e){   Button Button1 = new Button();   mandArgument = "b1";   Button1.Text = "Btn1";   mand += new CommandEventHandler(this.OnButton);    PlaceHolder1.Controls.Add(Button1);    Button Button2 = new Button();   mandArgument = "b2";   Button2.Text = "Btn2";   mand += new CommandEventHandler(this.OnButton);    PlaceHolder1.Controls.Add(Button2);    Control c3 = ParseControl("&ASP:Button id='Button3' text='Btn3' commandname='Btn' commandargument='b3' runat='server' /&"); //将字符串转换成Web控件   Control c4 = ParseControl("&asp:Button id='Button4' text='Btn4' commandname='Btn' commandargument='b4' runat='server' /&");    PlaceHolder1.Controls.Add(c3);    PlaceHolder1.Controls.Add(c4);    Button myBut = (Button)Page.FindControl("Button3");    mand += new CommandEventHandler(this.OnButton);    Button myBut2 = (Button)Page.FindControl("Button4");    mand += new CommandEventHandler(this.OnButton); }public void OnButton(Object Sender, CommandEventArgs e) {    switch (e.CommandArgument.ToString().ToLower())    {     case "b1":      Label1.Text = "Button 1";      break;     case "b2":      Label1.Text = "Button 2";      break;     case "b3":      Label1.Text = "Button 3";      break;     case "b4":      Label1.Text = "Button 4";      break;    }; } 
这几天忙,好久没上来了,今天项目基本完了,可以轻松下了。明天周末,呵呵。 我项目中遇到的是动态创建Tab选项卡和Gridview,按下面的方法可以,创建它们当然在page_load里了,有时,它会引起你页面上其它的按钮用
  这几天忙,好久没上来了,今天项目基本完了,可以轻松下了。明天周末,呵呵。
  我项目中遇到的是动态创建Tab选项卡和Gridview,按下面的方法可以,创建它们当然在page_load里了,有时,它会引起你页面上其它的按钮用不了,这时,你可以把它们不放在page_load里,而放在page_init里,这样就不会有问题了,我不知其它的Ajax控件会不会有这样的问题,我创建TabPanel时,就出现异常。我把它们放在Page_Init里就不会这样了。
  动态添加AjaxControl Toolkit的Tab控件及设置模板
  有朋友在使用AjaxControl Toolkit里的Tab控件,他刚刚问怎么实现在cs里动态添加TabPanel项。我建了一个项目试了一下。主要问题是TabPanel里如何创建ContentTemplate。
   &ajaxToolkit:TabContainer&控件使用方法比较简单,直接拖到页面上,添加TabPanel,然后直接输入TabPanel的内容就可以了,控件完全支持设计时的WYSIWYG,非常方便。TabPanel里的内容是通过&contenttemplate&&/contenttemplate&来指定的。这个对应于TabPanel的ContentTemplate属性,VS里intellisense显示它的类型是ITemplate,这是一个接口,我试着在intellisense里找找有没有类实现了这个接口,但一无所获。找不到这个类,就没有办法给ContentTemplate赋值。于是查看了一下Tab控件的源码,又查阅了MSDN,终于把问题解决了。
  问题关键就在于这个实现了ITemplate接口的类,我找不到,只好自己写了一个。也不知道有没有别的方法,希望有知道的朋友告诉我。全部代码如下:
using Susing System.Dusing System.Cusing System.Wusing System.Web.Susing System.Web.UI;using System.Web.UI.WebCusing System.Web.UI.WebControls.WebPusing System.Web.UI.HtmlCusing AjaxControlT //记得加上这个啊public partial class _Default : System.Web.UI.Page {    protected void Page_Load(object sender, EventArgs e)    {        TabContainer tabContainer1 = new TabContainer();        TabPanel tab1 = new TabPanel();        tab1.HeaderText = "标签1";        tabContainer1.Tabs.Add(tab1);        TabPanel tab2 = new TabPanel();        tab2.HeaderText = "标签2";        tabContainer1.Tabs.Add(tab2);        //建立第一个Tab里的内容        Panel panel1 = new Panel();                Label label1 = new Label();        label1.Text = "这是第一个Tab";        panel1.Controls.Add(label1);        //创建一个Template        TabContentTemplate temp1 = new TabContentTemplate();        //添加子控件        temp1.SetTemplate(panel1);        tab1.ContentTemplate = temp1;        this.form1.Controls.Add(tabContainer1);    }}public class TabContentTemplate : ITemplate{    private Control _    public void SetTemplate(Control templateControl)    {        _template = templateC    }    ITemplate Members#region ITemplate Members    public void InstantiateIn(Control container)    {        container.Controls.Add(_template);    }    #endregion}
  很多控件,如GridView、Repeat等都使用了Template,应该也都可以按这种方法来实现动态设置模板了。
  BTW,另外还有一种方法动态设置模板,就是使用Page.LoadTemplate("template.ascx"),如tab1.ContentTemplate=Page.LoadTemplate("template.ascx") 
随笔 - 114
最大化所有窗口
最小化所有窗口
停靠所有窗口转自:/lj1020/articles/2568885.html
一、添加GroupBox控件
1.实例化并显示
1 //实例化GroupBox控件
GroupBox groubox = new GroupBox();
groubox.Name = &gbDemo&;
groubox.Text = &实例&;
//设置上和左位置
//groubox.Top = 50;
//groubox.Left = 50;
//通过坐标设置位置
groubox.Location = new Point(12, 12);
//将groubox添加到页面上
this.Controls.Add(groubox);
二、在GroupBox控件中添加TextBox控件
1.实例化并显示
//实例化TextBox控件
TextBox txt = new TextBox();
txt.Name = &txtDemo&;
txt.Text = &txt实例&;
//将TextBox在GroupBox容器中显示
//txt.Parent =
//将TextBox在GroupBox容器中显示
groubox.Controls.Add(txt);
2.置于顶层和置于底层
//置于顶层
txt.BringToFront();
//置于底层
txt.SendToBack();
3.添加事件
1 //添加Click单击事件
txt.Click &#43;= new EventHandler(btn_Click);
//定义Click单击事件
private void btn_Click(object sender, EventArgs e)
MessageBox.Show(&事件添加成功&);
三、添加多个
1.动态添加多个
1 //添加控件
public void AddGroupBox()
string name = &gbox&;
for (int i = 0; i & 3; i&#43;&#43;)
GroupBox gbox = new GroupBox();
gbox.Name = name &#43;
gbox.Text=name&#43;i;
gbox.Width = 300;
gbox.Height = 100;
gbox.Location = new Point(32, 20 &#43; i * 150);
this.Controls.Add(gbox);
//调用添加文本控件的方法
AddTxt(gbox);
//添加文本控件
public void AddTxt(GroupBox gb)
string name = &txt&;
for (int i = 0; i & 3; i&#43;&#43;)
TextBox txt = new TextBox();
txt.Name =gb.Name&#43; name &#43;
txt.Text =gb.Name&#43;&|&&#43; name &#43;
txt.Location = new Point(12, 15 &#43; i * 30);
gb.Controls.Add(txt);
2 using System.Collections.G
3 using ponentM
4 using System.D
5 using System.D
6 using System.L
7 using System.T
8 using System.Windows.F
10 namespace Select_ListBox
public partial class Form2 : Form
{TextBox txt = new TextBox();
public Form2()
InitializeComponent();
private void Form2_Load(object sender, EventArgs e)
AddGroupBox();
////实例化GroupBox控件
//GroupBox groubox = new GroupBox();
//groubox.Name = &gbDemo&;
//groubox.Text = &实例&;
////设置上和左位置
////groubox.Top = 50;
////groubox.Left = 50;
////通过坐标设置位置
//groubox.Location = new Point(12, 12);
////将groubox添加到页面上
//this.Controls.Add(groubox);
////实例化TextBox控件
//TextBox txt = new TextBox();
//txt.Name = &txtDemo&;
//txt.Text = &txt实例&;
////将TextBox在GroupBox容器中显示
////txt.Parent =
////将TextBox在GroupBox容器中显示
//groubox.Controls.Add(txt);
////置于顶层
//txt.BringToFront();
////置于底层
//txt.SendToBack();
////添加Click单击事件
//txt.Click &#43;= new EventHandler(btn_Click);
////定义Click单击事件
//private void btn_Click(object sender, EventArgs e)
MessageBox.Show(&ss&);
//添加控件
public void AddGroupBox()
string name = &gbox&;
for (int i = 0; i & 3; i&#43;&#43;)
GroupBox gbox = new GroupBox();
gbox.Name = name &#43;
gbox.Text=name&#43;i;
gbox.Width = 300;
gbox.Height = 100;
gbox.Location = new Point(32, 20 &#43; i * 150);
this.Controls.Add(gbox);
//调用添加文本控件的方法
AddTxt(gbox);
//添加文本控件
public void AddTxt(GroupBox gb)
string name = &txt&;
for (int i = 0; i & 3; i&#43;&#43;)
TextBox txt = new TextBox();
txt.Name =gb.Name&#43; name &#43;
txt.Text =gb.Name&#43;&|&&#43; name &#43;
txt.Location = new Point(12, 15 &#43; i * 30);
gb.Controls.Add(txt);
第一次在博客园里写学习记录,新手上路,请多指教
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:33759次
排名:千里之外
转载:273篇
(15)(11)(12)(2)(24)(11)(54)(28)(22)(22)(56)(25)posts - 1,&
comments - 0,&
trackbacks - 0
一、添加GroupBox控件
1.实例化并显示
1 //实例化GroupBox控件
GroupBox groubox = new GroupBox();
groubox.Name = "gbDemo";
groubox.Text = "实例";
//设置上和左位置
//groubox.Top = 50;
//groubox.Left = 50;
//通过坐标设置位置
groubox.Location = new Point(12, 12);
//将groubox添加到页面上
this.Controls.Add(groubox);
二、在GroupBox控件中添加TextBox控件
1.实例化并显示
//实例化TextBox控件
TextBox txt = new TextBox();
txt.Name = "txtDemo";
txt.Text = "txt实例";
//将TextBox在GroupBox容器中显示
//txt.Parent =
//将TextBox在GroupBox容器中显示
groubox.Controls.Add(txt);
2.置于顶层和置于底层
//置于顶层
txt.BringToFront();
//置于底层
txt.SendToBack();
3.添加事件
1 //添加Click单击事件
txt.Click += new EventHandler(btn_Click);
//定义Click单击事件
private void btn_Click(object sender, EventArgs e)
MessageBox.Show("事件添加成功");
三、添加多个
1.动态添加多个
1 //添加控件
public void AddGroupBox()
string name = "gbox";
for (int i = 0; i & 3; i++)
GroupBox gbox = new GroupBox();
gbox.Name = name +
gbox.Text=name+i;
gbox.Width = 300;
gbox.Height = 100;
gbox.Location = new Point(32, 20 + i * 150);
this.Controls.Add(gbox);
//调用添加文本控件的方法
AddTxt(gbox);
//添加文本控件
public void AddTxt(GroupBox gb)
string name = "txt";
for (int i = 0; i & 3; i++)
TextBox txt = new TextBox();
txt.Name =gb.Name+ name +
txt.Text =gb.Name+"|"+ name +
txt.Location = new Point(12, 15 + i * 30);
gb.Controls.Add(txt);
2 using System.Collections.G
3 using ponentM
4 using System.D
5 using System.D
6 using System.L
7 using System.T
8 using System.Windows.F
10 namespace Select_ListBox
public partial class Form2 : Form
{TextBox txt = new TextBox();
public Form2()
InitializeComponent();
private void Form2_Load(object sender, EventArgs e)
AddGroupBox();
////实例化GroupBox控件
//GroupBox groubox = new GroupBox();
//groubox.Name = "gbDemo";
//groubox.Text = "实例";
////设置上和左位置
////groubox.Top = 50;
////groubox.Left = 50;
////通过坐标设置位置
//groubox.Location = new Point(12, 12);
////将groubox添加到页面上
//this.Controls.Add(groubox);
////实例化TextBox控件
//TextBox txt = new TextBox();
//txt.Name = "txtDemo";
//txt.Text = "txt实例";
////将TextBox在GroupBox容器中显示
////txt.Parent =
////将TextBox在GroupBox容器中显示
//groubox.Controls.Add(txt);
////置于顶层
//txt.BringToFront();
////置于底层
//txt.SendToBack();
////添加Click单击事件
//txt.Click += new EventHandler(btn_Click);
////定义Click单击事件
//private void btn_Click(object sender, EventArgs e)
MessageBox.Show("ss");
//添加控件
public void AddGroupBox()
string name = "gbox";
for (int i = 0; i & 3; i++)
GroupBox gbox = new GroupBox();
gbox.Name = name +
gbox.Text=name+i;
gbox.Width = 300;
gbox.Height = 100;
gbox.Location = new Point(32, 20 + i * 150);
this.Controls.Add(gbox);
//调用添加文本控件的方法
AddTxt(gbox);
//添加文本控件
public void AddTxt(GroupBox gb)
string name = "txt";
for (int i = 0; i & 3; i++)
TextBox txt = new TextBox();
txt.Name =gb.Name+ name +
txt.Text =gb.Name+"|"+ name +
txt.Location = new Point(12, 15 + i * 30);
gb.Controls.Add(txt);
第一次在博客园里写学习记录,新手上路,请多指教
阅读(...) 评论() &}

我要回帖

更多关于 winform 生成控件 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信