2015年1月8日 星期四

數字陣列

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
    {
        System.Windows.Forms.Button Button1;
        System.Windows.Forms.Button[] Buttons;
        public Form1()
        {
            InitializeComponent();
           
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            mybuttons();
            //Button1 = new System.Windows.Forms.Button();
            //Button1.Location = new Point(100, 100);
            //Button1.Size = new Size(100, 100);
            //Button1.Click += new System.EventHandler(Button1_Click);
            for (int i = 0; i < Buttons.Length; i++)
            {
                int index = i;
                Buttons[i].Click += (sender1, ex) => this.Display(index + 1);
                this.Controls.Add(Button1);
            }
        }
        public void Display(int i)
        {
            MessageBox.Show("Button No " + i);
        }
        private void Button1_Click(object sender, EventArgs e)
        {
           
            MessageBox.Show("zzzzzzzzzzz");
        }
        private void mybuttons()
        {

            int width, height;
            //Button1 = new System.Windows.Forms.Button();
            //Button1.Location = new Point(200, 200);
            //Button1.Size = new Size(200, 200);
            //this.Controls.Add(Button1);
            width = 50;
            height = 50;
            Buttons = new System.Windows.Forms.Button[9];

            for (int i = 0; i < 9; ++i)
            {
                Buttons[i] = new Button();
                this.Controls.Add(Buttons[i]);
                Buttons[i].Size = new Size(width, height);
                Buttons[i].Text = i.ToString();
                Buttons[i].BackColor = Color.Red;
                if (i <= 2)
                    Buttons[i].Location = new System.Drawing.Point(100 + i * 50, 10);
                else if (i > 2 && i <= 5)
                    Buttons[i].Location = new System.Drawing.Point(100 + (i - 3) * 50, 60);
                else if (i > 5 && i <= 9)
                    Buttons[i].Location = new System.Drawing.Point(100 + (i - 6) * 50, 110);

            }
        }
    }
}

沒有留言:

張貼留言