需要一個Access資料表
共有 5個button
2個 textbox
2個 label
1個 bindingSource
1個 dataGridView
public partial class Form1 : Form
{
int a = 0, rnd1 = 0;
public Form1()
{
InitializeComponent();
textBox1.DataBindings.Add("Text", bindingSource1, "秒數", true);
textBox2.DataBindings.Add("Text", bindingSource1, "輸贏", true);
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
a = a + 1;
textBox1.Text = a.ToString();
if (bindingSource1.Count.Equals(6))
{
timer1.Enabled = false;
MessageBox.Show("遊戲結束");
}
}
private void button1_Click(object sender, EventArgs e)
{
Random rnd = new Random();
rnd1 = rnd.Next(1, 4);
if (rnd1 == 1)
{
button4.Text = "剪刀";
textBox2.Text = "平手";
}
if (rnd1 == 2)
{
button4.Text = "石頭";
textBox2.Text = "你輸了";
}
if (rnd1 == 3)
{
button4.Text = "布";
textBox2.Text = "你贏了";
}
try
{
bindingSource1.EndEdit();
猜拳TableAdapter.Update(資料庫1DataSet);
MessageBox.Show("下一回合");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
try
{
bindingSource1.AddNew();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void button2_Click(object sender, EventArgs e)
{
Random rnd = new Random();
rnd1 = rnd.Next(1, 4);
if (rnd1 == 1)
{
button4.Text = "剪刀";
textBox2.Text = "你贏了";
}
if (rnd1 == 2)
{
button4.Text = "石頭";
textBox2.Text = "平手";
}
if (rnd1 == 3)
{
button4.Text = "布";
textBox2.Text = "你輸了";
}
try
{
bindingSource1.EndEdit();
猜拳TableAdapter.Update(資料庫1DataSet);
MessageBox.Show("下一回合");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
try
{
bindingSource1.AddNew();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void button3_Click(object sender, EventArgs e)
{
Random rnd = new Random();
rnd1 = rnd.Next(1, 4);
if (rnd1 == 1)
{
button4.Text = "剪刀";
textBox2.Text = "你輸了";
}
if (rnd1 == 2)
{
button4.Text = "石頭";
textBox2.Text = "你贏了";
}
if (rnd1 == 3)
{
button4.Text = "布";
textBox2.Text = "平手";
}
try
{
bindingSource1.EndEdit();
猜拳TableAdapter.Update(資料庫1DataSet);
MessageBox.Show("下一回合");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
try
{
bindingSource1.AddNew();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void Form1_Load(object sender, EventArgs e)
{
// TODO: 這行程式碼會將資料載入 '資料庫1DataSet.猜拳' 資料表。您可以視需要進行移動或移除。
this.猜拳TableAdapter.Fill(this.資料庫1DataSet.猜拳);
}
private void button5_Click(object sender, EventArgs e)
{
try
{
bindingSource1.AddNew();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void button6_Click(object sender, EventArgs e)
{
try
{
bindingSource1.EndEdit();
猜拳TableAdapter.Update(資料庫1DataSet);
MessageBox.Show("資料更新成功");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
物件導向回家練習
2015年12月19日 星期六
2015年10月3日 星期六
C# 紅綠燈
C#
紅綠燈
紅燈五秒
綠燈三秒
黃燈兩秒
public partial class Form1 : Form
{
int a = 0, b = 5, c = 3, d = 0, g = 0;
public Form1()
{
InitializeComponent();
}
private void timer1_Tick(object sender, EventArgs e)
{
g = g + 1;
a = g % 10;
if (a <= 4)
{
button1.BackColor = System.Drawing.Color.Red;
button2.BackColor = System.Drawing.Color.Red;
b = b - 1;
button2.Text = b.ToString();
}
else
{
button1.BackColor = System.Drawing.Color.Black;
button2.BackColor = System.Drawing.Color.Black;
b = 5;
}
if (a >4 && a <=7)
{
button4.BackColor = System.Drawing.Color.Green;
button2.BackColor = System.Drawing.Color.Green;
c = c - 1;
button2.Text = c.ToString();
}
else
{
button4.BackColor = System.Drawing.Color.Black;
c = 3;
}
if (a >= 8 && a<=9 )
{
button3.BackColor = System.Drawing.Color.Yellow;
}
else
{
button3.BackColor = System.Drawing.Color.Black;
}
}
紅綠燈
紅燈五秒
綠燈三秒
黃燈兩秒
public partial class Form1 : Form
{
int a = 0, b = 5, c = 3, d = 0, g = 0;
public Form1()
{
InitializeComponent();
}
private void timer1_Tick(object sender, EventArgs e)
{
g = g + 1;
a = g % 10;
if (a <= 4)
{
button1.BackColor = System.Drawing.Color.Red;
button2.BackColor = System.Drawing.Color.Red;
b = b - 1;
button2.Text = b.ToString();
}
else
{
button1.BackColor = System.Drawing.Color.Black;
button2.BackColor = System.Drawing.Color.Black;
b = 5;
}
if (a >4 && a <=7)
{
button4.BackColor = System.Drawing.Color.Green;
button2.BackColor = System.Drawing.Color.Green;
c = c - 1;
button2.Text = c.ToString();
}
else
{
button4.BackColor = System.Drawing.Color.Black;
c = 3;
}
if (a >= 8 && a<=9 )
{
button3.BackColor = System.Drawing.Color.Yellow;
}
else
{
button3.BackColor = System.Drawing.Color.Black;
}
}
訂閱:
文章 (Atom)