How to do a foreach in a ListBox control in Windows Form

listbox

Problem:

I need to loop using foreach to each elements of the ListBox control.

Solution:

Use the below code to loop using foreach

public void checkStock()
    {
        foreach (var listBoxItem in listBox1.Items)
        {
            // use the currently iterated list box item
            MessageBox.Show(string.Format("{0} is not in stock!",listBoxItem.ToString()));

        }
    }