0

当我单击一个按钮时,我想一个接一个地运行多个方法。我想要一个单击按钮允许执行队列中的下一个方法。

消息框正是我想要的,但我讨厌消息框。

这就是我要实现的:

private void teststart_Click(object sender, EventArgs e)
{
    step1();
    //Wait for button to be clicked
    step2();
    //wait for button to be clicked
    step3();
    //wait for button to be clicked

}

private void continuebutton_Click(object sender, EventArgs e)
{
    //button to be clicked
}
4

5 回答 5

1

为您的 step 功能创建一个队列并委托它会更多练习。我已经完成了一个示例,当您单击按钮时将委托(步骤返回字符串)入队,它将包含您所有步骤的队列出列:

  using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public  delegate string mystep ();
        public Queue<mystep> queuestep;
        public Form1()
        {
            InitializeComponent();
            queuestep = new Queue<mystep>();
            queuestep.Enqueue(step1);
            queuestep.Enqueue(step2);
            queuestep.Enqueue(step3);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (queuestep.Count >0)
            {
           mystep currentstep =  queuestep.Dequeue();
           textBox1.Text =currentstep();
           }
        }

        private string step1()
        {
            return "step1";
        }
        private string step2()
        {
            return "step2";
        }
        private string step3()
        {
            return "step3";
        }
    }
}
于 2012-08-04T15:36:53.760 回答
1

您可以维护一个静态变量(即步数),每次按下正确的按钮时,增加步数。

static int _step = 0;

private void teststart_Click(object sender, EventArgs e) 
{ 
    if(_step > 0)
    {
      // already started
      return;
    }
    step1(); 
    _step++;

} 

private void continuebutton_Click(object sender, EventArgs e) 
{ 
    if(_step == 0) 
    {  
        // start button must be pushed first
        return;
    }

    if(_step == 1)
    {
      step2(); 
      _step++;
    }
    else
    {
      step3(); .
      _step = 0;  // start over
    } 
} 
于 2012-08-04T15:06:47.010 回答
1

对于我的特定解决方案,我从上面发布的解决方案开始。在更多谷歌之后,我发现最简单的方法是

do
{Application.Doevents()}
while (step == false)

我在其中发现的唯一错误是我必须单击该按钮两次,所以我禁用了该按钮,无论如何我都想这样做,然后稍后重新启用它。我读过这是糟糕的编码,但我还没有找到更简单的方法来实现这个目标。

于 2012-09-06T02:35:46.977 回答
0

这不是特别的 C#,但这是一种可以做你想做的事情的方法。

保留一个类变量,比如Step整数类型的任何东西。在continuebutton_click-Method 内部,检查 if Step % 2 == 0,如果是,则触发行中的下一个方法(使用 switch/case 或其他东西)并递增 Step 。方法完成后,再次增加步数。

这样,您无需使用消息框即可获得所需的行为。

于 2012-08-04T15:06:59.873 回答
0

它的丑陋但有效,感谢您的建议。该代码正是我想要的。我确信有一种更优雅的方式来解决这个问题,但正如我所说的,我仍然是一个菜鸟,这很有效。

        step++;
        if (step == 502 || step == 392 || step == 305 || step == 14 || step == 313 ||          step == 324 || step == 332 || step == 344 || step == 357 || step == 363 || step == 373 || step == 384 || step == 122 || step == 408 || step == 222)
        {defaults();}

        if (step == 300)
        {firstlo();}

        if (step == 1 || step == 301)
        {firstlo1();}

        if (step == 2 || step == 302)
        {firstlo2();}

        if (step == 3 || step == 303)
        {firstlo3();}

        if (step == 4)
        {loinband();}

        if (step == 5 || step == 311)
        {loinband1();}

        if (step == 6)
        { secondlo();}

        if (step == 7 || step == 321)
        { secondlo1(); }

        if (step == 8 || step == 322)
        { secondlo2(); }

        if (step == 9)
        {currents();}

        if (step == 10)
        {outputpower();}

        if (step == 11 || step == 341)
        { outputpower1(); }

        if (step == 12 || step == 342)
        { outputpower2(); }

        if (step == 215)
        {satatt();}

        /*if (step == 351)
        { satatt1(); }*/

        if (step == 351 || step == 101 || step == 216)
        { satatt2(); }

        if (step == 352 || step == 102 || step == 217)
        { satatt3(); }

        if (step == 353 || step == 103 || step == 218)
        { satatt4(); }

        if (step == 354 || step == 104 || step == 219)
        { satatt5(); }

        if (step == 355 || step == 105 || step == 220)
        { satatt6(); }

        if (step == 106 || step == 211)
        { lineargain(); }

        if (step == 361 || step == 107 || step == 212)
        { lineargain1(); }

        if (step == 108 || step == 213)
        { compression(); }

        if (step == 109 || step == 371 || step == 214)
        { compression1(); }

        if (step == 110 || step == 208)
        { outputinband(); }

        if (step == 111 || step == 381 || step == 209)
        { outputinband1(); }

        if (step == 112 || step == 382 || step == 210)
        { outputinband2(); }

        if (step == 113 || step == 207)
        { ifrl(); }

        if (step == 114)
        { twotone(); }

        if (step == 115 || step == 401 || step == 201)
        { twotone1(); }

        if (step == 116 || step == 402 || step == 202)
        { twotone2(); }

        if (step == 117 || step == 403 || step == 203)
        { twotone3(); }

        if (step == 118 || step == 404 || step == 204)
        { twotone4(); }

        if (step == 119 || step == 405 || step == 205)
        { twotone5(); }

        if (step == 120 || step == 406 || step == 206)
        { twotone6(); }

        if (step == 304 || step == 13 || step == 501 || step == 312 || step == 323 || step == 331 || step == 343 || step == 356 || step == 362 || step == 372 || step == 383 || step == 391 || step == 121 || step == 407 || step == 221)
        {
            cont.Hide();
            instructions.Text = "Test Complete\n\n\nPress continue to return program to start condition\nor close program.";</code>
于 2012-08-05T18:14:51.930 回答