2016-09-16 89 views
1

我在Windows窗體應用程序中有一個ComboBox,其中項目需要顯示,如下圖所示。我只需要第一個分組的固體分隔符。其他項目可以不顯示分組頭。使用ComboBox它可以按照要求來實現,或者我必須嘗試任何第三方控件。任何有價值的建議都會有幫助。在Windows窗體應用程序中分組組合框項目

enter image description here

回答

2

您可以處理通過設置其DrawModeOwnerDrawFixed和處理DrawItem事件繪製的ComboBox項目自己。然後你就可以繪製分離器要在該項目下:

private void comboBox1_DrawItem(object sender, DrawItemEventArgs e) 
{ 
    var comboBox = sender as ComboBox; 
    var txt = ""; 
    if (e.Index > -1) 
     txt = comboBox.GetItemText(comboBox.Items[e.Index]); 
    e.DrawBackground(); 
    TextRenderer.DrawText(e.Graphics, txt, comboBox.Font, e.Bounds, e.ForeColor, 
     TextFormatFlags.VerticalCenter | TextFormatFlags.Left); 
    if (e.Index == 2 && !e.State.HasFlag(DrawItemState.ComboBoxEdit)) //Index of separator 
     e.Graphics.DrawLine(Pens.Red, e.Bounds.Left, e.Bounds.Bottom - 1, 
      e.Bounds.Right, e.Bounds.Bottom - 1); 
} 

的代碼e.State.HasFlag(DrawItemState.ComboBoxEdit)這部分是爲了防止在控件的編輯部分繪製分隔符。

enter image description here

注意

  • 答案滿足您要求的問題的要求。但是一些用戶可能希望用組文本對項目進行分組,而不僅僅是分隔符。要查看支持這些組文本的ComboBox,您可以查看Brad Smith的 A ComboBox Control with Groupig
相關問題