递归调用事件C#

时间:2013-04-30 13:33:47

标签: c# events recursion

我正在寻找一种递归调用事件的方法。我有以下

private void btn_choose_Click(object sender, EventArgs e)
{
    // switch statement to take the user input and decide the outcome.
    switch (Convert.ToInt32(nud_cat_chooser.Value))
    {
        case 1:
            if (Convert.ToInt32(lbl_p1_cat_1_value.Text) == Convert.ToInt32(lbl_p2_cat_1_value.Text))
            {
                MessageBox.Show("Stalemate");//message box to inform the user of a statemate.
                playingcards card1 = player1.Dequeue();//creates tempoary instance of the abstract class playign cards to store the cards
                playingcards card2 = player2.Dequeue();//creates tempoary instance of the abstract class playign cards to store the cards
                assign_Values();
                btn_choose_Click();
            }
        ....
    }
}

我想再次调用btn_choose_click事件来解决僵局。将使用assign方法为标签指定值。但是我正在努力实现对btn_choose_click()的调用;我必须通过哪些论据?有人能告诉我一个例子吗?

谢谢:)

7 个答案:

答案 0 :(得分:5)

传递发件人和e。

但是,如果我是你,我会简单地从处理程序中取出逻辑并将其放入方法中。明确地调用处理程序是一种非常糟糕的做法。事件处理程序应该响应事件。如果你要在你的处理程序中设置一个断点,你会期望它只是在调试时被击中以响应句柄中的事件,而不是因为你班级中其他地方的其他方法称它。 E.g:

private void btn_choose_Click(object sender, EventArgs e)
{
    NewMethod();
}
private void NewMethod()
{ 
    switch (Convert.ToInt32(nud_cat_chooser.Value))
    {


        case 1:
            if (Convert.ToInt32(lbl_p1_cat_1_value.Text) == Convert.ToInt32(lbl_p2_cat_1_value.Text))
            {
                MessageBox.Show("Stalemate");//message box to inform the user of a statemate.
                playingcards card1 = player1.Dequeue();//creates tempoary instance of the abstract class playign cards to store the cards
                playingcards card2 = player2.Dequeue();//creates tempoary instance of the abstract class playign cards to store the cards
                assign_Values();
                NewMethod();


}

答案 1 :(得分:3)

您只需致电Button.PerformClick()即可。我不知道按钮的名称。这将触发您的方法。

另请参阅MSDN上的Button.PerformClick()方法。

  

可以调用此方法来引发Click事件。

答案 2 :(得分:2)

你要进入一个递归的土地,要小心! 不要直接调用该方法。这是一个糟糕的设计。创建一个不同的函数,并明确这是一个递归函数。

private void btn_choose_Click(object sender, EventArgs e)
{
    int action = Convert.ToInt32(nud_cat_chooser.Value);
    this.DequeuePlayer(action); 
}

/// <summary>
/// Recursivly called until there is no more cards
/// </summary>
private void DequeuePlayer(int action)
{
    // switch statement to take the user input and decide the outcome.
    switch (action)
    {
        case 1:
            if (Convert.ToInt32(lbl_p1_cat_1_value.Text) == Convert.ToInt32(lbl_p2_cat_1_value.Text))
            {
                MessageBox.Show("Stalemate");//message box to inform the user of a statemate.
                playingcards card1 = player1.Dequeue();//creates tempoary instance of the abstract class playign cards to store the cards
                playingcards card2 = player2.Dequeue();//creates tempoary instance of the abstract class playign cards to store the cards
                assign_Values();
                this.DequeuePlayer(action);
            }
        ....
    }
  • 在调用函数之前从UI中提取数据。分离你的逻辑层
  • 不要在不知名的地方启动MessageBox.Show
  • 测试文本框文本属性的值。用户可以放任何东西。

答案 3 :(得分:1)

在这种情况下,您可以调用

btn_choose_Click(this, new EventArgs());

但是要非常小心,发件人将被用来识别哪个按钮正在调用该事件...现在您违反了它,您必须编写适当的文档以提及不依赖于发件人和eventargs参数内部事件

你也可以考虑编写一个新方法并使用循环调用它...这样会更可读和可靠..

答案 4 :(得分:1)

我会在while循环中做这件事。使其不必要地递归只会使代码更加混乱。

private void btn_choose_Click(object sender, EventArgs e)
{
  var continuing= true;
  while (continuing)
  {
    // switch statement to take the user input and decide the outcome.
    switch (Convert.ToInt32(nud_cat_chooser.Value))
    {
        case 1:
            if (Convert.ToInt32(lbl_p1_cat_1_value.Text) == Convert.ToInt32(lbl_p2_cat_1_value.Text))
            {
                MessageBox.Show("Stalemate");//message box to inform the user of a statemate.
                playingcards card1 = player1.Dequeue();//creates tempoary instance of the abstract class playign cards to store the cards
                playingcards card2 = player2.Dequeue();//creates tempoary instance of the abstract class playign cards to store the cards
                assign_Values();
            }
        ....
        case end state:
          do something
          continuing= false;
    }
  }
}

答案 5 :(得分:0)

只需使用:

btn_choose_Click(null, null);

只要您不依赖于方法中的发件人参数。您甚至可以从代码中提取函数并在onClick事件中调用它。

答案 6 :(得分:0)

您可以从上面的代码创建单独的方法。你可以从事件和任何你想要的地方打电话给他们。