- ·上一篇内容:C语言中fgetc函数的使用方法
- ·下一篇内容:在程序中动态地修改按钮的图片和尺寸
在C#代码中添加按钮的方法
有时我们需要在程序中动态地添加按钮完成一定的工作,下面的代码就演示了具体的方法。
注:本程序源自国外的一个网站(http://www.java2s.com/)在转载时经过了适当修改,以更适合懂汉语的人阅读。
/*
C# Program命 Tips & Techniques
by Charles Wright, Kris Jamsa
Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/
using System;
using System.Windows.Forms;
namespace Form
{
public class ButtonclsForm : System.Windows.Forms.Form
{
private Button button1;
public ButtonclsForm()
{
Text = "在程序中动态添加按钮的方法";
button1 = new Button ();
// SuspendLayout();
button1.Text = "退出";
button1.Name = "button1";
button1.Size = new System.Drawing.Size (72, 30);
button1.Location = new System.Drawing.Point ((ClientRectangle.Width - button1.Size.Width) / 2, ClientRectangle.Height - 35);
Controls.AddRange(new System.Windows.Forms.Control[] {this.button1});
button1.Click += new System.EventHandler(OnClickButton1);
// ResumeLayout (false);
}
static public void Main()
{
Application.Run(new ButtonclsForm());
}
void OnClickButton1 (object sender, System.EventArgs e)
{
Application.Exit ();
}
}
}
本文源自:翔宇亭——IT乐园(http://www.biye5u.com),转载请保留此信息!
微信搜索“优雅的代码”关注本站的公众号,或直接使用微信扫描下面二维码关注本站公众号,以获取最新内容。
个人成长离不开各位的关注,你的关注就是我继续前行的动力。