用程式碼向窗體新增控制元件步驟如下
(1)例項化一個控制元件;
(2)設定控制元件例項屬性;
(3)將控制元件例項新增到窗體的Controls集合中
(1)在Visual Studio中新建一個“Windos 窗體應用程式”
(2)窗體程式碼Form1.cs如下:
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Drawing;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
public Form1()
InitializeComponent();
Button btn = new Button();
btn.Location = new Point(50, 50);
btn.Size = new Size(80, 25);
btn.Text = "退出";
btn.Click += btn_Click;
//新增到視窗的Controls集合中
this.Controls.Add(btn);
}
void btn_Click(object sender, EventArgs e)
this.Close();
用程式碼向窗體新增控制元件步驟如下
(1)例項化一個控制元件;
(2)設定控制元件例項屬性;
(3)將控制元件例項新增到窗體的Controls集合中
(1)在Visual Studio中新建一個“Windos 窗體應用程式”
(2)窗體程式碼Form1.cs如下:
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Drawing;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Button btn = new Button();
btn.Location = new Point(50, 50);
btn.Size = new Size(80, 25);
btn.Text = "退出";
btn.Click += btn_Click;
//新增到視窗的Controls集合中
this.Controls.Add(btn);
}
void btn_Click(object sender, EventArgs e)
{
this.Close();
}
}
}