需要一個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);
}
}
}
}