在列表框中选择的每个项目调用一个方法。我需要以编程方式调用每个项目的方法,

A method is called on each item selected in a listbox. I need to call the method with each item progromatically,

对列表框中 selected 的每个项目调用一个方法。我需要以编程方式调用列表框中每个项目的方法,而不需要 select 每个项目。方法如下:

private void btnMove_Click(object sender, EventArgs e)
    {
        Cursor.Current = Cursors.WaitCursor;

        CreateFoldersMoveFiles(); // work is performed in this method with the selected listbox text.
        if (listBox2.SelectedIndex < listBox2.Items.Count - 1)
        {
            listBox2.SelectedIndex = listBox2.SelectedIndex + 1;
        }
        Cursor.Current = Cursors.Default;
    }

由于这不是您问题的一部分,我相信您在 CreateFoldersMoveFiles 方法中使用 listBox2.SelectedItem 来获取选定的项目,然后对其进行处理。相反,您需要允许 CreateFoldersMoveFiles 接受一个争论,并在您的 BtnMove_Click 中循环遍历列表框项目并调用该方法。

foreach (var item in listBox1.Items)
{
    CreateFoldersMoveFiles(item);
}