2017-10-12 61 views
0

我對組合框有點問題。如何更改所有組合框元素上的組合框的背景顏色?

我需要將組合框的背景顏色設置爲紅色。

我用下面的代碼:

if (!string.IsNullOrEmpty(ComboTransmis.Text)) 
    ComboTransmis.BackColor = Color.OrangeRed; 
else 
    ComboTransmis.BackColor = Color.White; 

但結果是這樣的:

enter image description here

只有具有背景色的文本,我需要的所有元素都與它我不知道該怎麼做。

如果有人有想法?

預先感謝您

+0

我想,如果我理解正確的問題,你需要編寫一個自定義控件來做到這一點。附:爲了遍歷表單中的每個組合框,您需要指定所有後續容器並遍歷每個項目。 –

+0

@MasterYoda它只是爲這個 –

+0

看看這個問題:https://stackoverflow.com/questions/6468024/how-to-change-combobox-backgound-color-not-just-the-drop-down-列表部分。你需要自己修改組合框來達到這個目的,但是你會失去3D風格並且變得平坦。 –

回答

1

你需要自己修改組合框但是你失去了3D風格和增益平坦實現這一

基於this答案:

更改組合框DrawMode屬性來OwnerDrawFixed,並處理 DrawItem事件:

private void ComboTransmis_DrawItem(object sender, DrawItemEventArgs e) 
{ 
    int index = e.Index >= 0 ? e.Index : 0; 
    var brush = Brushes.Black; 
    e.DrawBackground(); 
    e.Graphics.DrawString(ComboTransmis.Items[index].ToString(), e.Font, brush, e.Bounds, StringFormat.GenericDefault); 
    e.DrawFocusRectangle(); 
} 
+0

p.s.這個問題也許對你前進很有用:https://stackoverflow.com/questions/20812275/windows-form-combobox-custom-form-color :) –