作者nfs02 (danny)
看板C_Sharp
標題[問題] C#如何達到按鈕四選一的方法
時間Fri Apr 16 15:18:36 2010
各位前輩大家好
小弟目前遇到一個困難
就是如何將四個按鈕其中一個按鈕達到隨機為TRUE的功能
目前小弟的寫法是把他寫在一個固定的按鈕
以下為我的程式碼
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public int a = 0, b = 3, c;
int chooes()
{
a++;
c = b - a;
label2.Text = ("您的姓名為:")+textBox1.Text;
if (a < 3)
{
MessageBox.Show("你錯了" + a.ToString() + "次");
}
else
{
MessageBox.Show("三次都用光嚕~掰掰!!沒得玩了");
button1.Enabled = false;
button3.Enabled = false;
button4.Enabled = false;
button5.Enabled = false;
}
return a;
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text.Length == 0)
{
MessageBox.Show("請輸入姓名才給你玩");
button1.Enabled = false;
button3.Enabled = false;
button4.Enabled = false;
button5.Enabled = false;
}
else
{
chooes();
button1.Enabled = false;
}
}
private void button2_Click(object sender, EventArgs e)
{
label2.Text = "";
textBox1.Text = "";
button1.Enabled = Enabled;
button3.Enabled = Enabled;
button4.Enabled = Enabled;
button5.Enabled = Enabled;
a = 0;
}
private void button3_Click(object sender, EventArgs e)
{
if (textBox1.Text.Length == 0)
{
MessageBox.Show("請輸入姓名才給你玩");
button1.Enabled = false;
button3.Enabled = false;
button4.Enabled = false;
button5.Enabled = false;
}
else
{
chooes();
button3.Enabled = false;
}
}
private void button5_Click(object sender, EventArgs e)
{
if (textBox1.Text.Length == 0)
{
MessageBox.Show("請輸入姓名才給你玩");
button1.Enabled = false;
button3.Enabled = false;
button4.Enabled = false;
button5.Enabled = false;
}
else
{
chooes();
button5.Enabled = false;
}
}
private void button4_Click(object sender, EventArgs e)
{
if (textBox1.Text.Length == 0)
{
MessageBox.Show("請輸入姓名才給你玩");
button1.Enabled = false;
button3.Enabled = false;
button4.Enabled = false;
button5.Enabled = false;
}
else
{
MessageBox.Show("恭喜你,猜中嚕 得到娃娃熊一隻 你總共猜錯了"
+ a.ToString() + "次");
}
請各位前輩指導一下 謝謝
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 203.73.162.225
→ james732:忍不住想說你的重覆程式碼好多.....XD 04/16 15:23
→ nfs02:不號意思 我是初學者 我有想過寫成一個函式去做 不過發現 04/16 15:26
→ nfs02:會有問題 04/16 15:26
→ james732:你想做的是讓使用者猜按鈕嗎? 04/16 15:31
→ nfs02:對的 沒有錯 04/16 15:32
→ nfs02:感謝這位大哥 小弟先去研究一下您的寫法 04/16 16:00