我有一個列表框,其中包含100個項目和多個不同顏色的圖片框。當我點擊一個圖片框時,我想讓列表框中當前選定項目的背景變爲圖片框的顏色。我想我需要從picturebox的click事件中調用listbox drawitem事件的方式,但我不知道如何做到這一點。如何設置列表框項目的顏色以匹配已點擊的圖片框的顏色
到目前爲止,我有以下的代碼,但它僅設置所選項目到藍色的默認顏色:
private void redBox_Click(object sender, EventArgs e)
{
Color redBoxColour = redBox.BackColor;
}
private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
if (e.Index<0) return;
//if the item state is selected them change the back color
if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
e = new DrawItemEventArgs(e.Graphics,
e.Font,
e.Bounds,
e.Index,
e.State^DrawItemState.Selected,
e.ForeColor,
e.BackColor);
// Draw the background of the ListBox control for each item.
e.DrawBackground();
// Draw the current item text
e.Graphics.DrawString(listBox1.Items[e.Index].ToString(),e.Font, Brushes.Black, e.Bounds, StringFormat.GenericDefault);
// If the ListBox has focus, draw a focus rectangle around the selected item.
e.DrawFocusRectangle();
}
任何幫助非常讚賞。
你想成爲背景顏色?嘗試用你正在做的任何想要的值替換e.BackColor ... e.State^DrawItemState.Selected,e.ForeColor,** e.BackColor **); –