当前位置:K88软件开发文章中心编程语言.NET.NET01 → 文章内容

在C#代码中添加按钮的方法

减小字体 增大字体 作者:佚名  来源:翔宇亭IT乐园  发布时间:2018-12-31 11:36:51

:2010-09-06 18:44:00

有时我们需要在程序中动态地添加按钮完成一定的工作,下面的代码就演示了具体的方法。

注:本程序源自国外的一个网站(http://www.java2s.com/)在转载时经过了适当修改,以更适合懂汉语的人阅读。

/*
C# Programming 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 (7230);
            button1.Location = new System.Drawing.Point ((ClientRectangle.Width - button1.Size.Width2, 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 ();
        }
    }
}



在C#代码中添加按钮的方法