只需要一個button
{
public partial class Form1 : Form
{
Button[,] Buttons = new System.Windows.Forms.Button[5, 5];
Random rand = new Random();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 1; i < 5; i++)
{
for (int j = 1; j < 5; j++)
{
Buttons[i, j] = new Button();
Buttons[i, j].Size = new Size(50, 50);
Buttons[i, j].Location = new Point(i * 50, j * 50);
this.Controls.Add(Buttons[i, j]);//出現在畫面中
}
}
}
private void button1_Click(object sender, EventArgs e)
{
int x, y, a;
int[] myarray = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
for (int i = 15; i > 1; i--)
{
x = rand.Next(0, i);
y = myarray[x];
myarray[x] = myarray[i];
myarray[i] = y;
}
for (int j = 1; j < 5; j++)
{
for (int i = 1; i < 5; i++)
{
a = myarray[(j - 1) * 4 + (i - 1)];
Buttons[i, j].Text = Convert.ToString(a);
this.Controls.Add(Buttons[i, j]);
}
}
}
}
}
2015年10月30日 星期五
2015年10月26日 星期一
推數字
public partial class Form1 : Form
{
int rndmoney1 = 100, rndmoney2 = 100, rndmoney3 = 100, rndmoney4 = 100;
int rndmoney5 = 100, rndmoney6 = 100, rndmoney7 = 100, rndmoney8 = 100;
int rndmoney9 = 100;
public Form1()
{
InitializeComponent();
button1.Enabled = false;
button2.Enabled = false;
button3.Enabled = false;
button4.Enabled = false;
button5.Enabled = false;
button6.Enabled = false;
button7.Enabled = false;
button8.Enabled = false;
button9.Enabled = false;
}
private void button8_Click(object sender, EventArgs e)
{
}
private void button10_Click(object sender, EventArgs e)
{
Random rnd = new Random();
rndmoney1 = rnd.Next(0, 9);
button1.Text = rndmoney1.ToString();
rndmoney2 = rnd.Next(0, 9);
button2.Text = rndmoney2.ToString();
rndmoney3 = rnd.Next(0, 9);
button3.Text = rndmoney3.ToString();
rndmoney4 = rnd.Next(0, 9);
button4.Text = rndmoney4.ToString();
rndmoney5 = rnd.Next(0, 9);
button5.Text = rndmoney5.ToString();
rndmoney6 = rnd.Next(0, 9);
button6.Text = rndmoney6.ToString();
rndmoney7 = rnd.Next(0, 9);
button7.Text = rndmoney7.ToString();
rndmoney8 = rnd.Next(0, 9);
button8.Text = rndmoney8.ToString();
rndmoney9 = rnd.Next(0, 9);
button9.Text = rndmoney9.ToString();
while (rndmoney2 == rndmoney1 )
{
// MessageBox.Show("錯誤");
rndmoney2 = rnd.Next(0, 9);
button2.Text = rndmoney2.ToString();
}
//rndmoney3 = rnd.Next(0, 9);
//button3.Text = rndmoney3.ToString();
while (rndmoney3 == rndmoney1 || rndmoney3 == rndmoney2)
{
// MessageBox.Show("錯誤");
rndmoney3 = rnd.Next(0, 9);
button3.Text = rndmoney3.ToString();
}
// rndmoney4 = rnd.Next(0, 9);
// button4.Text = rndmoney4.ToString();
while (rndmoney4 == rndmoney1 || rndmoney4 == rndmoney2 || rndmoney4 == rndmoney3)
{
// MessageBox.Show("錯誤");
rndmoney4 = rnd.Next(0, 9);
button4.Text = rndmoney4.ToString();
}
// rndmoney5 = rnd.Next(0, 9);
// button5.Text = rndmoney5.ToString();
while (rndmoney5 == rndmoney1 || rndmoney5 == rndmoney2 || rndmoney5 == rndmoney3 || rndmoney5 == rndmoney4)
{
// MessageBox.Show("錯誤");
rndmoney5 = rnd.Next(0, 9);
button5.Text = rndmoney5.ToString();
}
//rndmoney6 = rnd.Next(0, 9);
//button6.Text = rndmoney6.ToString();
while (rndmoney6 == rndmoney1 || rndmoney6 == rndmoney2 || rndmoney6 == rndmoney3 || rndmoney6 == rndmoney4 || rndmoney6 == rndmoney5)
{
// MessageBox.Show("錯誤");
rndmoney6 = rnd.Next(0, 9);
button6.Text = rndmoney6.ToString();
}
// rndmoney7 = rnd.Next(0, 9);
// button7.Text = rndmoney7.ToString();
while (rndmoney7 == rndmoney1 || rndmoney7 == rndmoney2 || rndmoney7 == rndmoney3 || rndmoney7 == rndmoney4 || rndmoney7 == rndmoney5 || rndmoney7 == rndmoney6)
{
// MessageBox.Show("錯誤");
rndmoney7 = rnd.Next(0, 9);
button7.Text = rndmoney7.ToString();
}
// rndmoney8 = rnd.Next(0, 9);
// button8.Text = rndmoney8.ToString();
while (rndmoney8 == rndmoney1 || rndmoney8 == rndmoney2 || rndmoney8 == rndmoney3 || rndmoney8 == rndmoney4 || rndmoney8 == rndmoney5 || rndmoney8 == rndmoney6 || rndmoney8 == rndmoney7)
{
// MessageBox.Show("錯誤");
rndmoney8 = rnd.Next(0, 9);
button8.Text = rndmoney8.ToString();
}
// rndmoney9 = rnd.Next(0, 9);
// button9.Text = rndmoney9.ToString();
while (rndmoney9 == rndmoney1 || rndmoney9 == rndmoney2 || rndmoney9 == rndmoney3 || rndmoney9 == rndmoney4 || rndmoney9 == rndmoney5 || rndmoney9 == rndmoney6 || rndmoney9 == rndmoney7 || rndmoney9 == rndmoney8)
{
// MessageBox.Show("錯誤");
rndmoney9 = rnd.Next(0, 9);
button9.Text = rndmoney9.ToString();
}
{
int rndmoney1 = 100, rndmoney2 = 100, rndmoney3 = 100, rndmoney4 = 100;
int rndmoney5 = 100, rndmoney6 = 100, rndmoney7 = 100, rndmoney8 = 100;
int rndmoney9 = 100;
public Form1()
{
InitializeComponent();
button1.Enabled = false;
button2.Enabled = false;
button3.Enabled = false;
button4.Enabled = false;
button5.Enabled = false;
button6.Enabled = false;
button7.Enabled = false;
button8.Enabled = false;
button9.Enabled = false;
}
private void button8_Click(object sender, EventArgs e)
{
}
private void button10_Click(object sender, EventArgs e)
{
Random rnd = new Random();
rndmoney1 = rnd.Next(0, 9);
button1.Text = rndmoney1.ToString();
rndmoney2 = rnd.Next(0, 9);
button2.Text = rndmoney2.ToString();
rndmoney3 = rnd.Next(0, 9);
button3.Text = rndmoney3.ToString();
rndmoney4 = rnd.Next(0, 9);
button4.Text = rndmoney4.ToString();
rndmoney5 = rnd.Next(0, 9);
button5.Text = rndmoney5.ToString();
rndmoney6 = rnd.Next(0, 9);
button6.Text = rndmoney6.ToString();
rndmoney7 = rnd.Next(0, 9);
button7.Text = rndmoney7.ToString();
rndmoney8 = rnd.Next(0, 9);
button8.Text = rndmoney8.ToString();
rndmoney9 = rnd.Next(0, 9);
button9.Text = rndmoney9.ToString();
while (rndmoney2 == rndmoney1 )
{
// MessageBox.Show("錯誤");
rndmoney2 = rnd.Next(0, 9);
button2.Text = rndmoney2.ToString();
}
//rndmoney3 = rnd.Next(0, 9);
//button3.Text = rndmoney3.ToString();
while (rndmoney3 == rndmoney1 || rndmoney3 == rndmoney2)
{
// MessageBox.Show("錯誤");
rndmoney3 = rnd.Next(0, 9);
button3.Text = rndmoney3.ToString();
}
// rndmoney4 = rnd.Next(0, 9);
// button4.Text = rndmoney4.ToString();
while (rndmoney4 == rndmoney1 || rndmoney4 == rndmoney2 || rndmoney4 == rndmoney3)
{
// MessageBox.Show("錯誤");
rndmoney4 = rnd.Next(0, 9);
button4.Text = rndmoney4.ToString();
}
// rndmoney5 = rnd.Next(0, 9);
// button5.Text = rndmoney5.ToString();
while (rndmoney5 == rndmoney1 || rndmoney5 == rndmoney2 || rndmoney5 == rndmoney3 || rndmoney5 == rndmoney4)
{
// MessageBox.Show("錯誤");
rndmoney5 = rnd.Next(0, 9);
button5.Text = rndmoney5.ToString();
}
//rndmoney6 = rnd.Next(0, 9);
//button6.Text = rndmoney6.ToString();
while (rndmoney6 == rndmoney1 || rndmoney6 == rndmoney2 || rndmoney6 == rndmoney3 || rndmoney6 == rndmoney4 || rndmoney6 == rndmoney5)
{
// MessageBox.Show("錯誤");
rndmoney6 = rnd.Next(0, 9);
button6.Text = rndmoney6.ToString();
}
// rndmoney7 = rnd.Next(0, 9);
// button7.Text = rndmoney7.ToString();
while (rndmoney7 == rndmoney1 || rndmoney7 == rndmoney2 || rndmoney7 == rndmoney3 || rndmoney7 == rndmoney4 || rndmoney7 == rndmoney5 || rndmoney7 == rndmoney6)
{
// MessageBox.Show("錯誤");
rndmoney7 = rnd.Next(0, 9);
button7.Text = rndmoney7.ToString();
}
// rndmoney8 = rnd.Next(0, 9);
// button8.Text = rndmoney8.ToString();
while (rndmoney8 == rndmoney1 || rndmoney8 == rndmoney2 || rndmoney8 == rndmoney3 || rndmoney8 == rndmoney4 || rndmoney8 == rndmoney5 || rndmoney8 == rndmoney6 || rndmoney8 == rndmoney7)
{
// MessageBox.Show("錯誤");
rndmoney8 = rnd.Next(0, 9);
button8.Text = rndmoney8.ToString();
}
// rndmoney9 = rnd.Next(0, 9);
// button9.Text = rndmoney9.ToString();
while (rndmoney9 == rndmoney1 || rndmoney9 == rndmoney2 || rndmoney9 == rndmoney3 || rndmoney9 == rndmoney4 || rndmoney9 == rndmoney5 || rndmoney9 == rndmoney6 || rndmoney9 == rndmoney7 || rndmoney9 == rndmoney8)
{
// MessageBox.Show("錯誤");
rndmoney9 = rnd.Next(0, 9);
button9.Text = rndmoney9.ToString();
}
2015年10月16日 星期五
拉霸
public partial class Form1 : Form
{
int c = 0, d = 0, a = 0, b = 0, f = 0, g = 0;
int rndmoney1 = 100;
int rndmoney2 = 100;
int rndmoney3 = 100;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
c = c + 1;
d = c % 3;
button1.Text = c.ToString();
if (c >= rndmoney1)
{
timer1.Enabled = false;
}
if (d == 0)
{
button1.BackColor = System.Drawing.Color.FromName("RED");
}
else if (d == 1)
{
button1.BackColor = System.Drawing.Color.FromName("Green");
}
else if (d == 2)
{
button1.BackColor = System.Drawing.Color.FromName("Yellow");
}
}
private void button4_Click(object sender, EventArgs e)
{
Random rnd = new Random();
rndmoney1 = rnd.Next(1, 11);
rndmoney2 = rnd.Next(1, 11);
rndmoney3 = rnd.Next(1, 11);
timer1.Enabled = true;
timer2.Enabled = true;
timer3.Enabled = true;
c = 0;
a = 0;
f = 0;
}
private void timer2_Tick(object sender, EventArgs e)
{
a = a + 1;
b = a % 3;
button2.Text = a.ToString();
if (a >= rndmoney2)
{
timer2.Enabled = false;
}
if (b == 0)
{
button2.BackColor = System.Drawing.Color.FromName("Yellow");
}
else if (b == 1)
{
button2.BackColor = System.Drawing.Color.FromName("RED");
}
else if (b == 2)
{
button2.BackColor = System.Drawing.Color.FromName("Green");
}
}
private void timer3_Tick(object sender, EventArgs e)
{
f = f + 1;
g = f % 3;
button3.Text = f.ToString();
if (f >= rndmoney3)
{
timer3.Enabled = false;
}
if (g == 0)
{
button3.BackColor = System.Drawing.Color.FromName("Green");
}
else if (g == 1)
{
button3.BackColor = System.Drawing.Color.FromName("Yellow");
}
else if (g == 2)
{
button3.BackColor = System.Drawing.Color.FromName("Red");
}
}
private void timer4_Tick(object sender, EventArgs e)
{
button4.Text = "Start ";
if (button3.BackColor == button2.BackColor && button3.BackColor == button1.BackColor && button1.BackColor == button2.BackColor)
{
button5.Text = "WIN ";
}
else
{
button5.Text = "Defult ";
}
}
private void button5_Click(object sender, EventArgs e)
{
}
}
}
4個timer
{
int c = 0, d = 0, a = 0, b = 0, f = 0, g = 0;
int rndmoney1 = 100;
int rndmoney2 = 100;
int rndmoney3 = 100;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
c = c + 1;
d = c % 3;
button1.Text = c.ToString();
if (c >= rndmoney1)
{
timer1.Enabled = false;
}
if (d == 0)
{
button1.BackColor = System.Drawing.Color.FromName("RED");
}
else if (d == 1)
{
button1.BackColor = System.Drawing.Color.FromName("Green");
}
else if (d == 2)
{
button1.BackColor = System.Drawing.Color.FromName("Yellow");
}
}
private void button4_Click(object sender, EventArgs e)
{
Random rnd = new Random();
rndmoney1 = rnd.Next(1, 11);
rndmoney2 = rnd.Next(1, 11);
rndmoney3 = rnd.Next(1, 11);
timer1.Enabled = true;
timer2.Enabled = true;
timer3.Enabled = true;
c = 0;
a = 0;
f = 0;
}
private void timer2_Tick(object sender, EventArgs e)
{
a = a + 1;
b = a % 3;
button2.Text = a.ToString();
if (a >= rndmoney2)
{
timer2.Enabled = false;
}
if (b == 0)
{
button2.BackColor = System.Drawing.Color.FromName("Yellow");
}
else if (b == 1)
{
button2.BackColor = System.Drawing.Color.FromName("RED");
}
else if (b == 2)
{
button2.BackColor = System.Drawing.Color.FromName("Green");
}
}
private void timer3_Tick(object sender, EventArgs e)
{
f = f + 1;
g = f % 3;
button3.Text = f.ToString();
if (f >= rndmoney3)
{
timer3.Enabled = false;
}
if (g == 0)
{
button3.BackColor = System.Drawing.Color.FromName("Green");
}
else if (g == 1)
{
button3.BackColor = System.Drawing.Color.FromName("Yellow");
}
else if (g == 2)
{
button3.BackColor = System.Drawing.Color.FromName("Red");
}
}
private void timer4_Tick(object sender, EventArgs e)
{
button4.Text = "Start ";
if (button3.BackColor == button2.BackColor && button3.BackColor == button1.BackColor && button1.BackColor == button2.BackColor)
{
button5.Text = "WIN ";
}
else
{
button5.Text = "Defult ";
}
}
private void button5_Click(object sender, EventArgs e)
{
}
}
}
4個timer
2015年10月2日 星期五
第三節課 (紅綠燈)
程式設計工藝大師
需要三個button和一個timer
需要三個button和一個timer
public partial class Form1 : Form
{
int c = 2, d = 0;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
}
private void button1_Click_1(object sender, EventArgs e)
{
c = c + 1;
button1.Text = c.ToString();
}
private void timer1_Tick(object sender, EventArgs e)
{
c = c + 1;
d = c % 3;
button1.Text = d.ToString();
button2.Text = d.ToString();
button3.Text = d.ToString();
if (d == 0)
{
button1.BackColor = System.Drawing.Color.FromName("RED");
}
else
{
button1.BackColor = System.Drawing.Color.FromName("White");
}
if (d == 1)
{
button2.BackColor = System.Drawing.Color.FromName("Yellow");
}
else
{
button2.BackColor = System.Drawing.Color.FromName("White");
}
if (d == 2)
{
button3.BackColor = System.Drawing.Color.FromName("Green");
}
else
{
button3.BackColor = System.Drawing.Color.FromName("White");
}
}
}
訂閱:
文章 (Atom)