我想在Windows窗体中创建一个交互式故事,并将选项连接到按钮。所以我会有类似......
richTextBox1.Text = "Would you like to...
1. Kill a dragon?
2. Run away?
3. Talk to a dragon."
if ( button choice )
{}
if else ( button choice )
{}
else ( button choice )
{}
然后我的表单上有3个名为1,2,3的按钮。如何使用这些按钮在程序中做出选择?
答案 0 :(得分:3)
给他们所有相同的点击处理程序,然后检查sender
以确定点击了哪个按钮
答案 1 :(得分:1)
试试这样:
public Form1()
{
InitializeComponent();
button1.Click += button_Click;
button2.Click += button_Click;
button3.Click += button_Click;
}
和
private void button_Click(object sender, EventArgs e)
{
Button btn = sender as Button;
if (btn.Name == "button1")
{
// do something here according to..
}
else if (btn.Name == "button2")
{
// do something here according to..
}
....
}
答案 2 :(得分:0)
应该看起来像:
private void btns_Click(object sender, EventArgs e)
{
var btn = sender as Button;
if(btn == btn1)
{
MessageBox.Show("Choice 1");
}
else if (btn == btn2)
{
MessageBox.Show("Choice 2");
}
else if (btn == btn3)
{
MessageBox.Show("Choice 3");
}
}