CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
山寨机中的战斗机! 程序优化工程师到底对IT界有没有贡献
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  .NET技术 >  VB.NET

如何动态加载控件,加载好后又如何响应事件呢?

楼主horsefly()2003-11-01 18:17:51 在 .NET技术 / VB.NET 提问

最好可以给出例子。  
  小弟我不胜感激。:) 问题点数:100、回复次数:5Top

1 楼whl9234(葫芦)回复于 2003-11-01 20:17:15 得分 20

如  
  private   Table   addt(string   str,string   cssc)  
  {  
  Table   t=new   Table();  
  t.Rows.Add(new   TableRow());  
  t.Rows.Add(addtr(str,cssc));  
  t.Rows.Add(new   TableRow());  
  t.Width=Unit.Parse("100%");  
  return   t;  
  }  
  addtr是一个动态添加数据行的方法  
  动态添加事件,可以直接在动态生成控件的方法中加入:this.Button1.Click   +=   new   System.EventHandler(this.Button1_Click);(第二个this.Button1_Click是事件的名字)Top

2 楼visualbasic2000(齐谐)回复于 2003-11-01 20:18:54 得分 20

Public   Class   Form1  
          Inherits   System.Windows.Forms.Form  
  #Region   "   Windows   窗体设计器生成的代码   "  
          Public   Sub   New()  
                  MyBase.New()  
                  '该调用是   Windows   窗体设计器所必需的。  
                  InitializeComponent()  
                  '在   InitializeComponent()   调用之后添加任何初始化  
          End   Sub  
          '窗体重写   dispose   以清理组件列表。  
          Protected   Overloads   Overrides   Sub   Dispose(ByVal   disposing   As   Boolean)  
                  If   disposing   Then  
                          If   Not   (components   Is   Nothing)   Then  
                                  components.Dispose()  
                          End   If  
                  End   If  
                  MyBase.Dispose(disposing)  
          End   Sub  
          'Windows   窗体设计器所必需的  
          Private   components   As   System.ComponentModel.IContainer  
          '注意:   以下过程是   Windows   窗体设计器所必需的  
          '可以使用   Windows   窗体设计器修改此过程。  
          '不要使用代码编辑器修改它。  
          Friend   WithEvents   Button1   As   System.Windows.Forms.Button  
          <System.Diagnostics.DebuggerStepThrough()>   Private   Sub   InitializeComponent()  
                  Me.Button1   =   New   System.Windows.Forms.Button  
                  Me.SuspendLayout()  
                  '  
                  'Button1  
                  '  
                  Me.Button1.Location   =   New   System.Drawing.Point(264,   72)  
                  Me.Button1.Name   =   "Button1"  
                  Me.Button1.Size   =   New   System.Drawing.Size(136,   40)  
                  Me.Button1.TabIndex   =   0  
                  Me.Button1.Text   =   "Button1"  
                  '  
                  'Form1  
                  '  
                  Me.AutoScaleBaseSize   =   New   System.Drawing.Size(6,   14)  
                  Me.AutoScroll   =   True  
                  Me.ClientSize   =   New   System.Drawing.Size(424,   269)  
                  Me.Controls.Add(Me.Button1)  
                  Me.Name   =   "Form1"  
                  Me.Text   =   "动态加载试验"  
                  Me.ResumeLayout(False)  
          End   Sub  
  #End   Region  
          Dim   Txt(10)   As   TextBox  
          Private   Sub   Form1_Load(ByVal   sender   As   System.Object,   ByVal   e   As   System.EventArgs)   Handles   MyBase.Load  
                  Dim   i   As   Int16  
                  For   i   =   1   To   10  
                          Txt(i)   =   New   TextBox  
                          Txt(i).Top   =   i   *   100  
                          Txt(i).Tag   =   i  
                          Me.Controls.Add(Txt(i))  
                          AddHandler   Txt(i).TextChanged,   AddressOf   textchange  
                  Next  
          End   Sub  
          Sub   textchange(ByVal   sender   As   System.Object,   ByVal   e   As   System.EventArgs)  
                  Me.Text   =   "文本改变为:"   &   sender.Text   &   "         接受到第"   &   sender.Tag   &   "个动态加载的文本框发出的消息!"  
          End   Sub  
          Private   Sub   Button1_Click(ByVal   sender   As   System.Object,   ByVal   e   As   System.EventArgs)   Handles   Button1.Click  
                  Txt(4).BackColor   =   Color.Red  
                  MsgBox("动态加载的指定文本框的颜色改变了!",   MsgBoxStyle.OKOnly,   "发送消息!")  
          End   Sub  
  End   Class  
     
   
     
   
  Made   by   e-Stack   Room  
  http://www.mycnknow.comTop

3 楼menuvb(戏子,白日做梦)回复于 2003-11-02 09:26:20 得分 20

Private   Sub   btnAddButton_Click(ByVal   sender   As   System.Object,   ByVal   e   As   System.EventArgs)   Handles   btnAddButton.Click  
   
                  '   Increment   the   control   count  
                  m_ControlCount   +=   1  
   
                  '   Only   allow   5   buttons,   just   to   simplify   drawing   of   the   user   interface  
                  If   m_ControlCount   <=   5   Then  
                          '   Create   a   new   Button  
                          Dim   x   As   New   Button()  
   
                          '   Add   properties   to   the   form  
                          x.Name   =   "btn"   +   m_ControlCount.ToString()  
                          x.Text   =   "btn"   +   m_ControlCount.ToString()  
                          x.Location   =   New   Point(Me.m_Location.X   +   250,   Me.m_Location.Y)  
                          m_Location.Y   +=   x.Height   +   5  
   
                          '   Add   the   two   event   handlers    
                          AddHandler   x.Click,   AddressOf   myButtonHandler_Click  
  end   sub    
  Private   Sub   myButtonHandler_Click(ByVal   sender   As   Object,   ByVal   e   As   EventArgs)  
                  '   Verify   that   the   type   of   control   triggering   this   event   is   indeed  
                  '       a   Button.   This   is   necessary   since   this   handler   can   be   attached  
                  '       to   any   event.  
                  If   TypeOf   sender   Is   Button   Then  
                          '   Let   the   user   know   what   Button   was   pressed.  
                          MsgBox(CType(sender,   Button).Text   +   "   was   pressed!",   _  
                                          MsgBoxStyle.OKOnly,   Me.Text)  
                  End   If  
          End   SubTop

4 楼NoReady(亦正亦偏)回复于 2003-11-02 11:30:11 得分 20

例:  
  在formload时  
  dim   btn   as   new   button  
  btn.text="ok"  
  me.controls.add(btn)  
   
  '这是另一个过程,不用放到formlaod中  
  addhandler   btn.click,AddressOf(sayHello)  
   
  Private   Sub   sayHello(ByVal   sender   As   Object,   ByVal   e   As   EventArgs)  
          msgbox("Hello!   You   have   click   me   now")  
  End   Sub  
  Top

5 楼itleon(良朋)回复于 2003-11-02 17:22:29 得分 20

Dim   p   As   New   PictureBox  
                                  With   p  
                                          .Height   =    
                                          .Width   =    
                                          .Image   =    
                                          .BorderStyle   =   BorderStyle.FixedSingle  
                                          .Location   =   New   Point(x,   y)  
                                          .SizeMode   =   PictureBoxSizeMode.StretchImage  
                                          .Cursor   =   Cursors.Hand  
                                          .Name   =    
                                  End   With  
                                  ImagePanel.Controls.Add(p)  
                                  AddHandler   p.Click,   AddressOf   Double_ClickTop

相关问题

  • 怎么能让动态加载的Label控件能响应Click事件?
  • 动态加载控件?
  • 动态加载控件
  • 赠100分,动态加载用户控件时的事件响应问题,参与者有分。
  • 如何动态加载控件
  • 如何动态加载控件?
  • 动态加载控件问题???
  • winfrom动态加载控件的问题
  • 如何动态加载用户控件
  • 动态加载VB控件的问题?

关键词

  • me
  • 加载
  • button1
  • 动态
  • disposing
  • byval
  • textchange
  • sub
  • 事件
  • 必需

得分解答快速导航

  • 帖主:horsefly
  • whl9234
  • visualbasic2000
  • menuvb
  • NoReady
  • itleon

相关链接

  • CSDN .NET频道
  • .NET类图书
  • C#类图书
  • .NET类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo