Change text color of a listbox item

  1. Change DrawMode = OwnerDrawFixed

private void listBox1_DrawItem(object sender, DrawItemEventArgs e) {

e.DrawBackground();

e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), listBox1.Font, Brushes.Green, e.Bounds);

}

Anuncios

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);

}

Add combo box item from ListBox

List<string> CCode = new List<string>();

CylinderCOdeComboBox.DataSource = null;

CCode.Add(“item1”);

CylinderCodeComboBox.DataSource = CCode;

Get text of selected items in a listbox

foreach(var item in listBox1.SelectedItems){

MessageBox.Show(item.ToString());

}