2015年10月23日 星期五

n*n個不重複的亂數


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        const int buttonSize = 3;
        const int buttonStartX = 10;
        const int buttonStartY = 10;
        const int buttonWidth = 50;
        const int buttonHeight = 50;

        Button[] newBtn = new Button[buttonSize * buttonSize];
        Button startButton;
        Random rnd = new Random();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //b1.Location = new Point(10, 10);

            int count = 0;

            for (int i = 0; i < buttonSize; i++)
            {
                for (int j = 0; j < buttonSize; j++)
                {
                    newBtn[i + j * buttonSize] = new Button();
                    newBtn[i + j * buttonSize].Location = new Point(buttonStartX + buttonWidth * i, buttonStartY + buttonHeight * j);
                    newBtn[i + j * buttonSize].Text = (count + 1).ToString();
                    newBtn[i + j * buttonSize].Name = newBtn[i + j * buttonSize].Text;
                    newBtn[i + j * buttonSize].Size = new Size(buttonWidth, buttonHeight);
                    newBtn[i + j * buttonSize].TabIndex = count + 1;
                    this.Controls.Add(newBtn[i + j * buttonSize]);
                    newBtn[i + j * buttonSize].Click += new System.EventHandler(button1_Click);
                    newBtn[i + j * buttonSize].Enabled = false;
                    count++;
                }
            }

            startButton = new Button();
            startButton.Location = new Point(buttonStartX + buttonWidth * (buttonSize + 2), buttonStartY + buttonHeight * buttonSize);
            startButton.Text = (count + 1).ToString();
            startButton.Name = startButton.Text;
            startButton.Size = new Size(buttonWidth*2, buttonHeight*2);
            startButton.TabIndex = count + 1;
            this.Controls.Add(startButton);
            startButton.Click += new System.EventHandler(startbutton_Click);
            startButton.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
            startButton.BackgroundImage = Image.FromFile("C:\\Users\\student\\Downloads\\IMAG0003.jpg");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //MessageBox.Show(sender.ToString());
        }

        private void startbutton_Click(object sender, EventArgs e)
        {
            int[] arr = new int[buttonSize * buttonSize + 1];
            for (int i = 0; i < buttonSize * buttonSize + 1; i++)
                arr[i] = i;

            for (int i = 0; i < buttonSize * buttonSize; i++)
            {
                int rand = rnd.Next(1, buttonSize * buttonSize);
                if (rand == i)
                    continue;
                int tem = arr[rand];
                arr[rand] = arr[i];
                arr[i] = tem;
                //newBtn[i].Text = rnd.Next(1, 10).ToString();
            }

            for (int i = 0; i < buttonSize * buttonSize; i++)
                newBtn[i].Text = arr[i].ToString();
        }
    }
}

2015年10月2日 星期五

紅綠燈

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        int c = 0;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            c++;
            button1.Text = c.ToString();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            c++;
            button1.Text = c.ToString();
            button2.Text = c.ToString();
            button3.Text = c.ToString();

            if (c % 3 == 0)
            {
                button2.BackColor = System.Drawing.Color.White;
                button1.BackColor = System.Drawing.Color.Red;
                button3.BackColor = System.Drawing.Color.White;
            }
            else if (c % 3 == 1)
            {
                button2.BackColor = System.Drawing.Color.Yellow;
                button1.BackColor = System.Drawing.Color.White;
                button3.BackColor = System.Drawing.Color.White;
            }
            else if (c % 3 == 2)
            {
                button2.BackColor = System.Drawing.Color.White;
                button1.BackColor = System.Drawing.Color.White;
                button3.BackColor = System.Drawing.Color.Green;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}

程式設計工藝大師

程式設計工藝大師
D0250179 曾敬允