暂停并恢复Foreach循环

时间:2011-12-12 19:28:57

标签: c# winforms foreach

我有一个类似于此的foreach循环:

foreach (var line in theCP4UnknownList.Distinct())
{
    var splitUnknowns = line.Split(' ');
    KTS_Save saveForm = new KTS_Save(splitUnknowns[0], splitUnknowns[1], splitUnknowns[3], splitUnknowns[4], openFile.FileName);

    saveForm.Show();
    saveForm.Activate();
    //This is where I would like to add a check to see if a button is clicked, if true continue the foreach loop, else wait for the button to be clicked.
}

KTS_Save表单有4个文本框字段,每次都会自动填充(以及需要由用户填写的其他空白字段)foreach循环启动。但是,我只希望一次打开一个新表单,因此程序需要等待用户在下一个KTS_Save表单打开之前单击“保存”按钮....保存按钮将将表单中的当前数据保存到文本文件中。

有没有办法暂停foreach循环并等待单击按钮?我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:4)

只需将您的代码更改为:

foreach (var line in theCP4UnknownList.Distinct()) 
{ 
    var splitUnknowns = line.Split(' '); 
    KTS_Save saveForm = new KTS_Save(splitUnknowns[0], splitUnknowns[1], splitUnknowns[3], splitUnknowns[4], openFile.FileName); 

    saveForm.ShowDialog(); 
} 

答案 1 :(得分:2)

使用saveForm.ShowDialog()代替saveForm.Show()