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

2016-01-29 12:06 7 1 收藏

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

【 tulaoshi.com - vb 】

下面是实现上面结果的程序源代码:

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

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/vb/)

//清除在程序中使用到的资源
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 ) ;

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/vb/)

//为产生的新的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.R

来源:https://www.tulaoshi.com/n/20160129/1484652.html

延伸阅读
作为软件设计和开发人员大都有过使用DLL(动态连接库)的经历,DLL的产生使得我们的应用程序在可维护性、代码的重复使用等方面都有了很大的提高。以前用的DLL一般都是用Visual C++、Delphi或者VB等编程语言来编写的,这种DLL的编写和使用,我们大都已经比较习惯了。作为新一代的程序开发语言--Visual C#,到底是如何编写和使用DLL的呢!本...
所谓托盘程序顾名思义就是象托起的盘子一样的程序。而所谓的托起的盘子就是程序运行中显示出的图标,而托起的位置就是视窗系统的的工具栏了。托盘程序具有直观、占用屏幕空间较小并且可以为它定义多个功能菜单,这就给操作者带来了方便,所以越来越多的程序设计者都把程序设计成托盘这种方式。我们已经看过了用其他语言设计托盘程序的例子...
一.前言 Visual C#作为一门新兴的编程语言,具有许多其它语言无法比拟的优点。它既有VB的快速简洁,同时又不失C++的高效性能,而且作为一门基于组件编程的语言,它在组件编程方面有着相当强大和完善的功能。本文笔者就通过运用Visual C#编写一个Pop3邮件接收组件向大家介绍如何用Visual C#进行组件编程以及编程过程中的一些方法和技...
Adobe PDF格式已经变成很多机构和公司进行跨平台制表的通用媒体格式。尽管我不是这个产品的狂热痴迷者,却不得不接受这样一个事实:用这个格式产生一个协定可能会比用Word还要好。 PDF的全称为Portable document.nbspFormat,即可移植文档格式,由广泛应用于专业打印领域的Postscript解释语言演化而来。为了使PDF迅速得到推广,Adobe...
Visual C#是微软公司推出的新一代程序开发语言,是微软.Net框架中的一个重要组成部分。屏幕保护程序是以scr为扩展名的标准Windows可执行程序。屏幕保护程序不仅可以延长显示器的使用寿命,还可以保护私人信息。本文向大家介绍一个.Net平台上用C#编写的一个动态文本及图形的屏幕保护程序。 一、具体实现步骤: (1)在Visual Stud...

经验教程

506

收藏

88
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部