当我点击一个按钮时,我希望一个接一个地运行多个方法。我想要一个单击按钮,允许队列中的下一个方法执行。
消息框正是我想要的,但我讨厌消息框。
这就是我要实现的目标:
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
}
答案 0 :(得分: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
}
}
答案 1 :(得分:1)
为你的步骤函数创建一个队列和委托它将更多练习。我已经完成了一个将委托(步骤返回字符串)排入队列的示例,当您单击按钮时,它会使包含所有步骤的队列出列队列:
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";
}
}
}
答案 2 :(得分:1)
对于我的特定解决方案,我从上面发布的解决方案开始。经过更多的谷歌,我发现简单的方法是
do
{Application.Doevents()}
while (step == false)
我发现的唯一错误就是我必须单击按钮两次,所以我禁用了按钮,无论如何我都想做,然后再将其重新设置。我读过这是一个糟糕的编码,但我还没有找到一种更简单的方法来实现这个目标。
答案 3 :(得分:0)
这不是特别的C#,但这是一种可以做你想要的方法。
保留一个类变量,如Step
或任何类型为整数的变量。
在continuebutton_click
- 方法内部,检查是否Step % 2 == 0
,如果是,则触发下一个方法(使用开关/大小写等)并逐步递增。
方法完成后,再次增加步数。
这样,您就可以在不使用消息框的情况下获得所需的行为。
答案 4 :(得分: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>