2015-11-03 35 views
0

我有一個for loop,它在按鈕點擊處理程序中對一組標記(連接組件標記算法)進行編號和着色,如何使循環在執行任何步驟後暫停並在點擊按鈕後在循環中的相同位置重放再次?下面 是我的代碼:如何暫停for循環,然後在同一位置重新啓動?

private void Button1_Click(object sender, EventArgs e) 
{ 
    int counter = 1; 
    for (int i = 1; i < 161; i++) 
    { 
     try 
     { 
      if (i == 1 && labelss[i].Text == "") 
      { 
       labelss[i].Text = "1"; 
      } 

      if (labelss[i].Text == "0") 
      { 
       //do nothing 
      } 
      else //if the label is not 0 
      {      
       if (labelss[i - 1].Text !="0") 
       { 
        labelss[i].Text = labelss[i-1].Text; 
       } 
       else //if (labelss[i-1].Text=="0") 
       {        
        if (i > 20) 
        { 
         if (labelss[i - 20].Text != "0") 
         { 
          labelss[i].Text = labelss[i - 20].Text; 
         } 
         else 
         { 
          counter++; 
          labelss[i].Text = counter.ToString(); 
         } 
        } 
        else if(i < 20) 
        { 
         counter++; 
         labelss[i].Text = counter.ToString();        
        } 
       } 
      } 
     } 
     catch 
     { 
     } 
    } 
} 
+0

提示:全球布爾變量,變量保存位置,而循環/條件檢查布爾變量.... – TheLethalCoder

+1

在一個側面沒有'如果這...做nothing'這是否真的需要仍然在代碼中?正好反過來if語句,即'if(labelss [i] .Text!=「0」)' – TheLethalCoder

+1

你還想什麼時候該循環暫停,在什麼條件下...... – TheLethalCoder

回答

3

你需要做的是抽象的算法進行中獨立於按鈕點擊的獨立課程。這可以保持狀態(例如它改變了多少個標籤的顏色)。你需要傳遞一個類似於Func<int, bool>的東西,這可以稱爲每次迭代,並返回truefalse,具體取決於您是否希望它停止。

class Colourizer 
{ 

    private int _current; 

    public void Colourize(Func<int, bool> predicate) 
    { 
     for(int i = _current; _current < 161; ++ _current) 
     { 
      // change labels here 
      if(predicate(i)) 
       break; 
     } 
    } 

    public void Reset() 
    { 
     _current = 0; 
    } 
} 

private Colourizer _colourizer = new Colourizer(); 
private void Button1_OnClick(object sender, EventArgs args) 
{ 
    _colourizer.Colourize((i) => false); // this just does all 
} 
+0

親愛的@Moo_Juice謝謝,我沒有真正檢查你的方式,看看它是如何工作的,但我利用了你的循環的聲明,並改變了我的'for(int i = 1; i <161 ; i ++)'to'int iter = 1:for(int i = iter; i <161; i ++)',並在每次標記後使用'break'打開循環,並將循環的位置保存在變量「iter」中斷。它現在工作完美,非常感謝你:) – Hakar

1

你需要兩樣東西

  • for循環作爲一個單獨的功能,走的是「當前索引」作爲參數。
  • 在點擊之間存儲「當前索引」的方法。
    在這個例子中,我使用Session。

    // Process one label only. 
    public void ProcessLabel(int i) 
    { 
        // ... body of for loop ... 
    } 
    
    // Store current index between clicks. 
    private int CurrentIndex 
    { 
        get { return (int)Session["MyCurrentIndex"]; } 
        set { Session["MyCurrentIndex"] = value; } 
    } 
    
    // Processes one label, per click. 
    private void Button1_Click(object sender, EventArgs e) 
    { 
        int i = CurrentIndex; 
    
        if (i >= 161) 
         return;  // End of for loop 
    
        ProcessLabel(i); 
    
        // On next click, CurrentIndex+1 will be processed. 
        CurrentIndex = i + 1; 
    } 
    
    // Call this function during page initialisation. 
    private void OnInit() 
    { 
        // Setup the first click. 
        CurrentIndex = 1; 
    } 
    
相關問題