close

今天老師教我們如何寫按扭矩陣,方便製造多數按鈕的狀況下使用。

按鈕陣列  

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 WindowsFormsApplication7
{
    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.Click += new EventHandler(Button1_Click);
            //this.Controls.Add(Button1);

        }

        private void Button1_Click(object sender, EventArgs e) 
        { 
             MessageBox.Show("我是台好車,來開我"); 
        }


        private void mouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                MessageBox.Show("hello");
            }
        }


        private void Button_Click(Object sender, EventArgs e)
        {
            //Button button = (Button)sender;
            //button.Text = button.Name;
            MessageBox.Show("hello");
        }

        private void mybuttons()
        {
            int width, height;
            // Button1 = new System.Windows.Forms.Button();
            // Button1.Location = new Point(100, 100);
            // this.Controls.Add(Button1);
            width = 100;
            height = 100;

            Buttons = new System.Windows.Forms.Button[9];


            for (int i = 0; i < 9; ++i)
            {

                Buttons[i] = new Button();
                //:::size:::
                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);
                //Buttons[i].Click += OnClick;
                this.Controls.Add(Buttons[i]);

                if (i <= 2)
                    Buttons[i].Location = new System.Drawing.Point(100 + i * 100, 10);
                else if (i > 2 && i <= 5)
                    Buttons[i].Location = new System.Drawing.Point(100 + (i - 3) * 100, 110);
                else if (i > 5 && i <= 9)
                    Buttons[i].Location = new System.Drawing.Point(100 + (i - 6) * 100, 210);
            }


        }

        private void button2_Click(object sender, EventArgs e)
        {
            MessageBox.Show("我是台好車,來開我"); 
        }
    }
}

arrow
arrow
    全站熱搜

    敏晟 發表在 痞客邦 留言(0) 人氣()