2014年12月7日 星期日

12/5 excel VBA 亂數產生
VB excel 亂數程式碼
Private Sub CommandButton1_Click()
For i = 1 To 9
Cells(1, i) = i
Cells(1, i).Font.ColorIndex = 0
Next
For i = 1 To 9
j = 10 - i
myrandom = (Fix(Rnd() * j + 1))
Cells(3, 3) = myrandom
tem = Cells(1, j)
Cells(1, j) = Cells(1, myrandom)
Cells(1, myrandom) = tem
Next
End Sub


11/28產生亂數(亂數重複)
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 WindowsFormsApplication8
{
public partial class Form1 : Form
{
int i;
Button[,] buttons = new Button[5, 5];
Random ran = new Random();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 1; i <=4; i++)
{
for (int j = 1; j <=4 ; j++)
{
int c = ran.Next(1, 15);
buttons[i, j] = new Button();
buttons[i, j].Location = new Point(i * 50, j * 50);
buttons[i, j].Size = new Size(50, 50);
buttons[i, j].BackColor = Color.White;
this.Controls.Add(buttons[i, j]);
buttons[i, j].Text = Convert.ToString(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 WindowsFormsApplication10
{
public partial class Form1 : Form
{
Button[,] buttons = new Button[10, 10];
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 1; i <= 9; i++)
{
for (int j = 1; j <= 9; j++)
{
buttons[i, j] = new Button();
buttons[i, j].Location = new Point(i * 50, j * 50);
buttons[i, j].Size = new Size(50, 50);
buttons[i, j].BackColor = Color.White;
buttons[i, j].Text = (i*j).ToString();
this.Controls.Add(buttons[i, j]);
}
}
}
}
}

11/14 九九乘法表
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 WindowsFormsApplication10
{
public partial class Form1 : Form
{
Button[,] buttons = new Button[10, 10];
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 1; i <= 9; i++)
{
for (int j = 1; j <= 9; j++)
{
buttons[i, j] = new Button();
buttons[i, j].Location = new Point(i * 50, j * 50);
buttons[i, j].Size = new Size(50, 50);
buttons[i, j].BackColor = Color.White;
buttons[i, j].Text = (i*j).ToString();
this.Controls.Add(buttons[i, j]);
}
}
}
}
}

11/7 產生方塊
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 WindowsFormsApplication4
{
public partial class Form1 : Form
{
System.Windows.Forms.Button Button1;//產生一個按鈕
System.Windows.Forms.Button[] Buttons;//產生數個按鈕
public Form1()
{
InitializeComponent();
}
int aa;
private void Form1_Load(object sender, EventArgs e)
{
// Button1 = new System.Windows.Forms.Button();
//Button1.Click += new System.EventHandler(Button1_Click);
// this.Controls.Add(Button1);
/*測試單一按鈕*/
mybuttons( aa );
}

private void Button_Click(Object sender, EventArgs e)
{
Button number = (Button)sender;
MessageBox.Show(number.Text);
}

private void mybuttons(int i)
{
Buttons = new System.Windows.Forms.Button[9];

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

Buttons[i] = new Button();
Buttons[i].Text = i.ToString();
Buttons[i].BackColor = Color.White;
this.Controls.Add(Buttons[i]);

Buttons[i].Click += new System.EventHandler(Button_Click);

if (i <= 2)
{
Buttons[i].Location = new System.Drawing.Point(100 + i * 200, 50);
Buttons[i].Size = new Size(100, 100);

}

else if (i > 2 && i <= 5)
{
Buttons[i].Location = new System.Drawing.Point(100 + (i - 3) * 200, 200);
Buttons[i].Size = new Size(100, 100);

}
else if (i > 5 && i <= 9)
{
Buttons[i].Location = new System.Drawing.Point(100 + (i - 6) * 200, 350);
Buttons[i].Size = new Size(100, 100);

}

}
}
}
}


10/31圈叉遊戲 比較輸贏
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 WindowsFormsApplication6
{
public partial class Form1 : Form
{
int count = 1;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
button1.Text = "";
button2.Text = "";
button3.Text = "";
button4.Text = "";
button5.Text = "";
button6.Text = "";
button7.Text = "";
button8.Text = "";
button9.Text = "";
}
private void button1_Click(object sender, EventArgs e)
{
int r;
r = count % 2;
if (r == 1)
{
button1.Text = "o";
}
else if (r == 0)
{
button1.Text = "x";
}
textBox1.Text = Convert.ToString(r);
count++;
button1.Enabled = false;
judge();
}
private void button2_Click(object sender, EventArgs e)
{
int r;
r = count % 2;
if (r == 1)
{
button2.Text = "o";
}
else if (r == 0)
{
button2.Text = "x";
}
textBox1.Text = Convert.ToString(r);
count++;
button2.Enabled = false;
judge();
}
private void button3_Click(object sender, EventArgs e)
{
int r;
r = count % 2;
if (r == 1)
{
button3.Text = "o";
}
else if (r == 0)
{
button3.Text = "x";
}
textBox1.Text = Convert.ToString(r);
count++;
button3.Enabled = false;
judge();
}
private void button4_Click(object sender, EventArgs e)
{
int r;
r = count % 2;
if (r == 1)
{
button4.Text = "o";
}
else if (r == 0)
{
button4.Text = "x";
}
textBox1.Text = Convert.ToString(r);
count++;
button4.Enabled = false;
judge();
}
private void button5_Click(object sender, EventArgs e)
{
int r;
r = count % 2;
if (r == 1)
{
button5.Text = "o";
}
else if (r == 0)
{
button5.Text = "x";
}
textBox1.Text = Convert.ToString(r);
count++;
button5.Enabled = false;
judge();
}
private void button6_Click(object sender, EventArgs e)
{
int r;
r = count % 2;
if (r == 1)
{
button6.Text = "o";
}
else if (r == 0)
{
button6.Text = "x";
}
textBox1.Text = Convert.ToString(r);
count++;
button6.Enabled = false;
judge();
}
private void button7_Click(object sender, EventArgs e)
{
int r;
r = count % 2;
if (r == 1)
{
button7.Text = "o";
}
else if (r == 0)
{
button7.Text = "x";
}
textBox1.Text = Convert.ToString(r);
count++;
button7.Enabled = false;
judge();
}
private void button8_Click(object sender, EventArgs e)
{
int r;
r = count % 2;
if (r == 1)
{
button8.Text = "o";
}
else if (r == 0)
{
button8.Text = "x";
}
textBox1.Text = Convert.ToString(r);
count++;
button8.Enabled = false;
judge();
}
private void button9_Click(object sender, EventArgs e)
{
int r;
r = count % 2;
if (r == 1)
{
button9.Text = "o";
}
else if (r == 0)
{
button9.Text = "x";
}
textBox1.Text = Convert.ToString(r);
count++;
button9.Enabled = false;
judge();
}
private void judge()
{
if (button1.Text == "x" && button2.Text == "x" && button3.Text == "x")
MessageBox.Show("x贏了");
if (button1.Text == "x" && button4.Text == "x" && button7.Text == "x")
MessageBox.Show("x贏了");
if (button3.Text == "x" && button6.Text == "x" && button9.Text == "x")
MessageBox.Show("x贏了");
if (button7.Text == "x" && button8.Text == "x" && button9.Text == "x")
MessageBox.Show("x贏了");
if (button4.Text == "x" && button5.Text == "x" && button6.Text == "x")
MessageBox.Show("x贏了");
if (button2.Text == "x" && button5.Text == "x" && button8.Text == "x")
MessageBox.Show("x贏了");
if (button1.Text == "x" && button5.Text == "x" && button9.Text == "x")
MessageBox.Show("x贏了");
if (button3.Text == "x" && button5.Text == "x" && button7.Text == "x")
MessageBox.Show("x贏了");
if (button1.Text == "o" && button2.Text == "o" && button3.Text == "o")
MessageBox.Show("o贏了");
if (button1.Text == "o" && button4.Text == "o" && button7.Text == "o")
MessageBox.Show("o贏了");
if (button3.Text == "o" && button6.Text == "o" && button9.Text == "o")
MessageBox.Show("o贏了");
if (button7.Text == "o" && button8.Text == "o" && button9.Text == "o")
MessageBox.Show("o贏了");
if (button4.Text == "o" && button5.Text == "o" && button6.Text == "o")
MessageBox.Show("o贏了");
if (button2.Text == "o" && button5.Text == "o" && button8.Text == "o")
MessageBox.Show("o贏了");
if (button1.Text == "o" && button5.Text == "o" && button9.Text == "o")
MessageBox.Show("o贏了");
if (button3.Text == "o" && button5.Text == "o" && button7.Text == "o")
MessageBox.Show("o贏了");
}
}
}

10/24圈叉遊戲
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 WindowsFormsApplication4
{
public partial class Form1 : Form
{
int count = 1;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
button1.Text = "";
button2.Text = "";
button3.Text = "";
button4.Text = "";
button5.Text = "";
button6.Text = "";
button7.Text = "";
button8.Text = "";
button9.Text = "";
}
private void button1_Click(object sender, EventArgs e)
{
int r;
r = count % 2;
if (r == 1)
{
button1.Text = "o";
}
else if (r == 0)
{
button1.Text = "x";
}
textBox1.Text = Convert.ToString(r);
count++;
button1.Enabled = false;
}
private void button2_Click(object sender, EventArgs e)
{
int r;
r = count % 2;
if (r == 1)
{
button2.Text = "o";
}
else if (r == 0){
button2.Text = "x";
}
textBox1.Text = Convert.ToString(r);
count++;
button2.Enabled = false;
}
private void button3_Click(object sender, EventArgs e)
{
int r;
r = count % 2;
if (r == 1){
button3.Text = "o";
}
else if (r == 0)
{
button3.Text = "x";
}
textBox1.Text = Convert.ToString(r);
count++;
button3.Enabled = false;
}
private void button4_Click(object sender, EventArgs e)
{
int r;
r = count % 2;
if (r == 1)
{
button4.Text = "o";
}
else if (r == 0){
button4.Text = "x";
}
textBox1.Text = Convert.ToString(r);
count++;
button4.Enabled = false;
}
private void button5_Click(object sender, EventArgs e)
{
int r;
r = count % 2;
if (r == 1)
{
button5.Text = "o";
}
else if (r == 0)
{
button5.Text = "x";
}
textBox1.Text = Convert.ToString(r);
count++;
button5.Enabled = false;
}
private void button6_Click(object sender, EventArgs e)
{
int r;
r = count % 2;
if (r == 1){
button6.Text = "o";
}
else if (r == 0)
{
button6.Text = "x";
}
textBox1.Text = Convert.ToString(r);
count++;
button6.Enabled = false;
}
private void button7_Click(object sender, EventArgs e)
{
int r;
r = count % 2;
if (r == 1)
{
button7.Text = "o";
}
else if (r == 0)
{
button7.Text = "x";
}
textBox1.Text = Convert.ToString(r);
count++;
button7.Enabled = false;
}
private void button8_Click(object sender, EventArgs e)
{
int r;
r = count % 2;
if (r == 1)
{
button8.Text = "o";
}
else if (r == 0)
{
button8.Text = "x";
}
textBox1.Text = Convert.ToString(r);
count++;
button8.Enabled = false;
}
private void button9_Click(object sender, EventArgs e)
{
int r;
r = count % 2;
if (r == 1)
{
button9.Text = "o";
}
else if (r == 0)
{
button9.Text = "x";
}
textBox1.Text = Convert.ToString(r);
count++;
button9.Enabled = false;
}
}
}

10/24方塊賽跑

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;
using System.Threading;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
int totalSum1 = 0;
int totalSum2 = 0;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int d1, d2,sum;//第一個方塊
int D1, D2, SUM;//第二個方塊
Random ran = new Random();
d1 = ran.Next(1, 7);
d2 = ran.Next(1, 7);
D1 = ran.Next(1, 7);
D2 = ran.Next(1, 7);
label1.Text = Convert.ToString(d1);
label2.Text = Convert.ToString(d2);
label3.Text = Convert.ToString(D1);
label4.Text = Convert.ToString(D2);
sum = d1 + d2;
SUM = D1 + D2;
for (int i = 1; i <= sum; i++)
{
button2.Left = (i+totalSum1)*5;
Thread.Sleep(50);//delay 1 秒
Application.DoEvents();
}//第一個方快走的
totalSum1 += sum;//總步數
textBox1.Text = Convert.ToString(totalSum1);
for (int i = 1; i <= SUM; i++)
{
button3.Left = (i+totalSum2)*5;
Thread.Sleep(50);//delay 1 秒
Application.DoEvents();
}//第二個方快走的
totalSum2 += SUM;//總步數
textBox2.Text = Convert.ToString(totalSum2);
if (totalSum1 > 50 && totalSum2 > 50)
{
if (totalSum1 > totalSum2)
{
MessageBox.Show("第一個贏了");
}
else if (totalSum1 < totalSum2)
MessageBox.Show("第二個贏了");
else
MessageBox.Show("平手");
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
}
}

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
{
int c,d=1;
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
if (button1.Left > 300 )
d = -1;
if (button1.Left < 50)
d = 1;
c = c + d;
button1.Left = 100*c;
}
private void timer1_Tick(object sender, EventArgs e)
{
if (button1.Left > 300)
d = -1;
if (button1.Left < 50)
d = 1;
c = c + d;
button1.Left = 100 * c;
}
}
}

9/26 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 WindowsFormsApplication2
{
public partial class Form1 : Form
{
int c = 0;
int r;
public Form1()
{
InitializeComponent();
}
private void button4_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
c++;
r = c % 3;
label1.Text = "r = " + r;
if (r == 1)
{
button1.BackColor = Color.Red;
button2.BackColor = Color.Black;
button3.BackColor = Color.Black;
}
else if (r == 2)
{
button1.BackColor = Color.Black;
button2.BackColor = Color.Yellow;
button3.BackColor = Color.Black;
}
else if (r == 0)
{
button1.BackColor = Color.Black;
button2.BackColor = Color.Black;
button3.BackColor = Color.Green;
}
}
}
}