首 页 ┆ 源码下载 ┆ IT学院 ┆ 字体下载 ┆ 模板下载 ┆ 源码发布 ┆ 广告合作 ┆ 网站地图 ┆ 虚拟主机 ┆ 中文域名
► 设为首页
► 加入收藏
► 联系我们
源码下载 >> ASP源码 | PHP源码 | ASP.net源码 | JSP源码 | CGI源码 | VC/C++源码 | VB源码 | Delphi源码 | Flash源码
文章学院 >> 网络编程 | 网页设计 | 图形图象 | 数据库 | 服务器 | 网络媒体 | 网络安全 | 操作系统 | 办公软件 | 软件开发 | 黑客知识
字体下载 >> 精制字体 | 非英字体 | 艺术字体 | 著名字体 | 哥特式 | 简单字体 | 手写体 | 节假日 | 图案字体 | 精度像素 | 中文字体
模板下载 >> 企业门户 | 数码网络 | 休闲娱乐 | 影视音乐 | 旅游名胜 | 文化艺术 | 电子商务 | 个性展示 | 登陆导航 | Flash模板
►►您当前的位置:源码园 → IT学院 → 软件开发 → VB编程 → 文章内容

用Visual C#动态生成组件,请看!(三)

作者:佚名  来源:网上收集  发布时间:2005-12-8 1:31:43
下面是实现上面结果的程序源代码:

using System ;
using System.Drawing ;
using System.Collections ;
using System.ComponentModel ;
using System.Windows.Forms ;
using System.Data ;
namespace DynamicControls
{
public class Form1 : Form
{
private Button btnAdd ;
private System.ComponentModel.Container components = null ;
private Button txtAdd ;
//给产生的按钮定义一个数量计算器
private int counter ;
//给产生的按钮定义相对位置的纵坐标
private int locY ;
//给产生的文本框定义一个数量计算器
private int counter01 ;
//给产生的文本框定义相对位置的纵坐标
private int locY1 ;
public Form1 ( )
{
InitializeComponent ( ) ;
//初始化产生的按钮何文本框位置的纵坐标
locY = this.btnAdd.Location.Y ;
locY1 = this.txtAdd.Location.Y ;
}

//清除在程序中使用到的资源
protected override void Dispose ( bool disposing )
{
if ( disposing )
{
if ( components != null )
{
components.Dispose ( ) ;
}
}
base.Dispose ( disposing ) ;
}

private void InitializeComponent ( )
{
this.btnAdd = new Button ( ) ;
this.txtAdd = new Button ( ) ;
this.SuspendLayout ( ) ;

this.btnAdd.FlatStyle = FlatStyle.Popup ;
this.btnAdd.Location = new System.Drawing.Point ( 8 , 16 ) ;
this.btnAdd.Name = "btnAdd" ;
this.btnAdd.TabIndex = 0 ;
this.btnAdd.Text = "生成按钮!" ;
this.btnAdd.Click += new System.EventHandler ( this.btnAdd_Click ) ;

this.txtAdd.FlatStyle = FlatStyle.Popup ;
this.txtAdd.Location = new System.Drawing.Point ( 108 , 16 ) ;
this.txtAdd.Name = "txtAdd" ;
this.txtAdd.TabIndex = 1 ;
this.txtAdd.Text = "生成文本框!" ;
this.txtAdd.Click += new System.EventHandler ( this.txtAdd_Click ) ;

this.AutoScaleBaseSize = new System.Drawing.Size ( 5 , 13 ) ;
this.ClientSize = new System.Drawing.Size ( 292 , 273 ) ;
this.Controls.Add ( btnAdd ) ;
this.Controls.Add ( txtAdd ) ;
this.Name = "Form1" ;
this.Text = "在Visual C#中如何动态产生组件!" ;
this.ResumeLayout ( false ) ;

}
static void Main ( )
{
Application.Run ( new Form1 ( ) ) ;
}
private void btnAdd_Click ( object sender , System.EventArgs e )
{
//按钮数量计算器在每次按钮按动后加"1"
counter += 1 ;
//对要产生的按钮的纵坐标的相对位置是前一个产生按钮的相对位置的纵坐标加"3"
locY += this.btnAdd.Height + 3 ;
//创建一个新的Button组件
Button myButton = new Button ( ) ;
//设定他的名称和Text属性,以及产生的位置
myButton.Name = "Button " + counter ;
myButton.Text = "按钮 " + counter ;
myButton.Location = new Point ( btnAdd.Location.X , locY ) ;

//为产生的新的Button组件设定事件,本文中为产生的按钮设定了三个事件
myButton.MouseEnter += new System.EventHandler ( this.btn_MouseEnter ) ;
myButton.MouseLeave += new System.EventHandler ( this.btn_MouseLeave ) ;
myButton.Click += new System.EventHandler ( this.btn_Click ) ;
//在窗体中显示此按钮
this.Controls.Add ( myButton ) ;
}

private void txtAdd_Click ( object sender , System.EventArgs e )
{
//文本框数量计算器在每次按钮按动后加"1"
counter01 += 1 ;
//对要产生的文本框的纵坐标的相对位置是前一个产生按钮的相对位置的纵坐标加"3
locY1 += this.txtAdd.Height + 3 ;
//创建一个新的TextBox组件
TextBox myBox = new TextBox ( ) ;
//设定他的名称和Text属性,以及产生的位置
myBox.Name = "TextBox " + counter01 ;
myBox.Text = "文本框 " + counter01 ;
myBox.Location = new Point ( txtAdd.Location.X , locY1 ) ;
//为产生的新的TextBox组件设定事件,本文中为产生的文本框设定了一个事件
myBox.Click += new System.EventHandler ( this.btn_Click ) ;
//在窗体中显示此文本框
this.Controls.Add ( myBox ) ;
}
private void btn_MouseEnter ( object sender , System.EventArgs e )
{
//出箱
Button currentButton = ( Button ) sender ;
//设定按钮的背景色
currentButton.BackColor = Color.Red ;
}

private void btn_MouseLeave ( object sender , System.EventArgs e )
{
//出箱
Button currentButton = ( Button ) sender ;
currentButton.BackColor = Control.DefaultBackColor ;
}

private void btn_Click ( object sender , System.EventArgs e )
{
if ( sender.GetType ( ) == typeof ( Button ) )
{
Button control = ( Button ) sender ;
MessageBox.Show ( control.Text + "被按动了!");
}
else
{
TextBox control = ( TextBox ) sender ;
MessageBox.Show ( control.Text + "被按动了!" ) ;
}
}

}
}

五. 总结:

通过上面介绍,不难看出,动态创建组件并不是一件很难的事情,难就难在为这个组件创建事件上面,因为这涉及到实值类型变量和参考类型变量的转换,这就是所谓的装箱和出箱的问题。当然在程序设计的时候,你不仅可以创建那些可见的组件,也可以创建那些不可见的组件,具体的实现方法和本文中的方法类似。

[] [返回上一页] [打 印]
  • 上一篇文章:用Visual C#动态生成组件,请看!(二)
  • 下一篇文章:用Visual C#动态生成组件,请看!(一)

  • 相关文章:
  • 在XP中使用Vista屏保
  • 利用Vista新命令将系统操作变得更轻松
  • [图文]制作和使用Vista恢复盘的新方法
  • [图文]禁用Vista移动中心
  • 分析统计使用Vista的体验
  • 使用Visual C++开发SOAP客户端应用
  • 如何用Visual C#做组件
  • 用Visual C#动态生成组件,请看!(一)
  • 用Visual C#动态生成组件,请看!(三)
  • 用Visual C#动态生成组件,请看!(二)
  • 用Visual C++实现PDF文件的显示
  • 利用Visual C#打造一个平滑的进度条
关于本站 - 网站帮助 - 广告合作 - 下载声明 - 友情连接 - 网站地图 - 源码发布
Copyright © 2003-2009 Ymyasp.Com. All Rights Reserved .
备案序号:粤ICP备07029071号