2 回答

TA贡献1836条经验 获得超5个赞
您不能将参数传递给按钮单击函数,但可以创建一个在您的按钮单击和其他范围内有效的全局变量:
例如:
bool QuestionAnswered = true; // it is outside the button click or other functions
void SomeMethod()
{
QuestionAnswered = false;
}
private void NextGasQuestion_Click(object sender, EventArgs e)
{
if (!QuestionAnswered)
{
GasQuestionsFailed++;
}
}

TA贡献1995条经验 获得超2个赞
如果您真的想将某些内容传递给按钮事件,您可以使用Tag按钮上的属性。您可以通过执行以下操作在事件处理程序内部访问它。
private void button1_Click(object sender, EventArgs e)
{
var btn = sender as Button;
if (btn == null) return;
var yourValue = btn.Tag as YourType;
if (yourValue != null)
{
//Do Stuff
}
}
正如其他人所说,您可能应该只在表单上创建一个变量并增加该值,而不是在按钮本身上维护状态。
- 2 回答
- 0 关注
- 88 浏览
添加回答
举报