2015-05-26 39 views
0

我有一個列表框,其中包含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(); 


    } 

任何幫助非常讚賞。

+0

你想成爲背景顏色?嘗試用你正在做的任何想要的值替換e.BackColor ... e.State^DrawItemState.Selected,e.ForeColor,** e.BackColor **); –

回答

0

您需要創建一個變量來保存所選圖片框的顏色:

Color selectedColor; 

然後,您可以通過單擊事件單擊PictureBox時設置此變量值(顏色)。點擊時,所有的圖片框應該調用此事件:

private void picBox_Click(object sender, EventArgs e) 
    { 
     PictureBox selectedPic = (PictureBox)sender; 
     selectedColor = selectedPic.BackColor; 
     listBox1.Refresh(); // to update the actual selected item 
    } 

並修改DRAWITEM功能使用這種顏色,而不是e.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, 
            selectedColor); 

    // 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(); 
} 

你需要採取一些在這之前的步驟,我不知道,如果你做的事:

1)本添加到窗體構造函數:

listBox1.DrawItem += new System.Windows.Forms.DrawItemEventHandler(listBox1_DrawItem); 

2)將listBox屬性DrawMode更改爲OwnerDrawFixed

+0

這真的有助於謝謝:)但是,當我在列表框中選擇一個新項目時,它會將其設置爲正確的顏色,但最後選定的項目會重新繪製回原始背景(即透明)。我如何確保所選最後一個框的顏色持續存在? – GTownAndroid

+0

你還有問題嗎?有些日子已經過去了,我忘記了這個評論=/ 無論如何......正如Hadron所說,如果你可以使用ListView,它會容易得多。如果你使用WPF而不是Windows Form,它會變得更好。如果您仍在使用以前的解決方案,請告訴我並嘗試幫助您:D –

0

改爲使用ListView。你可以用ListBox做的所有事情,你也可以用ListView

然後,只需更改選定的指數背景色,像這樣:

private void pictureBox1_Click(object sender, EventArgs e) 
    { 
     for (int i = 0; i < listView1.SelectedIndices.Count; i++) 
      listView1.Items[listView1.SelectedIndices[i]].BackColor = pictureBox1.BackColor; 
    } 

你打算給自己省了不少麻煩

相關問題