我有一個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
{
}
}
}
提示:全球布爾變量,變量保存位置,而循環/條件檢查布爾變量.... – TheLethalCoder
在一個側面沒有'如果這...做nothing'這是否真的需要仍然在代碼中?正好反過來if語句,即'if(labelss [i] .Text!=「0」)' – TheLethalCoder
你還想什麼時候該循環暫停,在什麼條件下...... – TheLethalCoder