2014年11月13日 星期四

C# 自動產生2*2按鈕+99乘法表

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 WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        int a;
        Button[,] buttons = new Button[10, 10];
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            for (int i = 1; i < 10; i++)
            {
                for (int j = 1; j < 10; j++)
                {
                    buttons[i, j] = new Button();
                    this.Controls.Add(buttons[i,j]);
                    buttons[i, j].Size = new Size(50, 50);
                    a = i * j;
                    buttons[i, j].Text = Convert.ToString(a);                              
                    buttons[i, j].Location = new Point(i * 50, j * 50);
                 
                }
            }
        }
    }
}

VB 99乘法表

Private Sub CommandButton1_Click()
For a = 1 To 9 Step 1
 For b = 1 To 9 Step 1
 Cells(a, b) = a * b
 Next
Next

End Sub

2014年11月7日 星期五

C#自動產生按鈕

自動產生按鈕

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);
              }
          }

         
     

   }
         
}