猿问

如何将参数传递给按钮单击

我有一个提问的程序,我希望它跟踪用户失败了多少问题,单击按钮会生成问题,我希望它跟踪用户是否被问到问题所以它可以将他们标记为未回答。


如果用户单击按钮生成另一个问题,它将知道该问题没有被回答并增加计数器。它目前给出错误消息


“NextGasQuestion_Click”没有重载匹配委托“EventHandler”


这是处理程序代码:


private void NextGasQuestion_Click(object sender, EventArgs e,bool QuestionAnswered,int GasQuestionsFailed)

{

    if (QuestionAnswered == false)

     {

        GasQuestionsFailed++;

     }

}    // Added by edit


慕盖茨4494581
浏览 89回答 2
2回答

一只甜甜圈

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

拉风的咖菲猫

如果您真的想将某些内容传递给按钮事件,您可以使用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    }}正如其他人所说,您可能应该只在表单上创建一个变量并增加该值,而不是在按钮本身上维护状态。
随时随地看视频慕课网APP
我要回答