ขั้นตอน
1. เปิด windows form จะได้ form1.cs
2. double click บน form เข้าสู่การ coding
3. ใช้ code ดังนี้
using System;
//using System.Collections.Generic;
//using System.ComponentModel;
//using System.Data;
using System.Drawing;
//using System.Linq;
//using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1 {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) {
Button myButton = new Button();
myButton.Height = 40;
myButton.Width = 200;
myButton.BackColor = Color.Yellow;
myButton.ForeColor = Color.Blue;
myButton.Location = new Point(5, 100);
myButton.Text = "I am Dynamic Button";
myButton.Name = "myButton";
myButton.Font = new Font("arial", 12);
myButton.Click += new EventHandler(myButton_Click);
Controls.Add(myButton);
}
private void myButton_Click(object sender, EventArgs e) {
MessageBox.Show("hello");
}
}
}
// http://www.c-sharpcorner.com/uploadfile/mahesh/creating-a-button-at-run-time-in-C-Sharp/
|