2012-08-04 25 views
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 
} 
+0

btw。我不認爲在這種情況下多線程是一個好的選擇。 – CodesInChaos 2012-08-04 15:11:54

回答

1

對於我的特殊解決方案,我從上面發佈的解決方案開始。更多的谷歌之後,我發現最簡單的方式是

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

我在此發現的唯一錯誤是,我不得不兩次單擊按鈕,所以我禁用了按鈕,這是我想做的事,無論如何,然後renabled它後來。我讀過這是不好的編碼,但我還沒有找到一個更簡單的方法來實現這個目標。

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 
    } 
} 
+0

相同的想法,但與代碼,使您的方法更好。雖然它缺少'多重點擊守衛',但是當繼續按鈕正在被處理時,再次形成塊。 – ATaylor 2012-08-04 15:08:36

+0

使這個領域靜態似乎是一個壞主意給我。靜態和可變幾乎不是一個好的組合。 – CodesInChaos 2012-08-04 15:09:39

+0

@CodesInChaos - 可能是一個類var可以工作,但我沒有足夠的代碼來確定。雖然好點。 – 2012-08-04 15:17:20

0

這不是特別的C#,但是這裏有一個方法可以做你想做的事。

保留一個類變量,如Step或任何類型的整數。 在continuebutton_click的內部 - 方法,檢查Step % 2 == 0,如果是,則觸發下一個方法(使用開關/外殼或其他)並逐步增加。 完成該方法後,再次遞增步數。

這樣,您可以在不使用消息框的情況下獲得所需的行爲。

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"; 
     } 
    } 
} 
+0

+1:這絕對是要走的路。其他答案是醜陋的黑客。 – 2012-08-04 16:53:34

+0

謝謝你狒狒;-) – 2012-08-04 16:58:07

+0

雖然在你的情況下'mystep'確實是一個'功能' – 2012-08-04 18:11:01

0

它的fugly但工作,謝謝你的建議。該代碼正是我想要的。我相信有一種更優雅的方法可以解決這個問題,但正如我所說,我仍然是一個小菜鳥,這是有效的。

 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>