Get selected and set selected in ListBox

// Loop through all items the ListBox.

for (int x = 0; x < listBox1.Items.Count; x++){

// Determine if the item is selected.

if(listBox1.GetSelected(x) == true)

// Deselect all items that are selected.

listBox1.SetSelected(x,false);

else

// Select all items that are not selected.

listBox1.SetSelected(x,true);

}