自動產生按鈕
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 WindowsFormsApplication11
{
public partial class Form1 : Form
{
//System.Windows.Forms.Button Button1;
System.Windows.Forms.Button[] Buttons;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
mybutton();
//Button1 = new System.Windows.Forms.Button();(產生一個按鈕)
//Button1.Location=new Point(100,100);(按鈕座標)
//Button1.Size=new Size(50,50);(按鈕大小)
//Button1.Click += new EventHandler(Button1_Click);
//this.Controls.Add(Button1);
}
private void Button1_Click(object sender, EventArgs e)
{
//MessageBox.Show("Hello");
}
private void mouseClick(object sender, EventArgs e)
{
//if (e.Button == MouseButtons.Left)
{
//MessageBox.Show("hello");
}
}
private void Button_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
//btn.Text = btn.Name;
MessageBox.Show(btn.Text);
}
private void mybutton()
{
Buttons = new System.Windows.Forms.Button[9];
for (int i = 0; i < 9; ++i)
{
int width, height;
width = this.Size.Width;
height = this.Size.Height;
Buttons[i] = new Button();
width = 60;
height = 60;
Buttons[i].Size = new Size(width, height);
Buttons[i].Text = i.ToString();
Buttons[i].BackColor = Color.Red;
Buttons[i].Click += new System.EventHandler(this.Button_Click);
this.Controls.Add(Buttons[i]);
if (i <= 2)
Buttons[i].Location = new System.Drawing.Point(150 + i * 60, 100);
else if (i > 2 && i <= 5)
Buttons[i].Location = new System.Drawing.Point(150 + (i - 3) * 60, 160);
else if (i > 5 && i <= 9)
Buttons[i].Location = new System.Drawing.Point(150 + (i - 6) * 60, 220);
}
}
}
}
沒有留言:
張貼留言