2011-08-23 63 views
0

我正在使用下面的代碼來重寫和繪製列表框中的每個列表項。如何將邊框添加到ListBox的下拉區域?

 if (e.Index < 0) return; 
     // if the item state is selected then 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, 
             Color.Red); // Choose the color 

     // Draw the background of the ListBox control for each item. 
     e.DrawBackground(); 
     // Draw the current item text 
     e.Graphics.DrawString(studentsListBox.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(); 

的代碼是工作,我會懷疑,但是我還想添加邊框列表框中的整個下拉部分(請參閱下面的示例圖像。)我如何添加邊框整個列表?

enter image description here

+0

你在問兩個問題 - 如何在下拉列表上繪製邊框,以及如何修改列表中每個元素的外觀?請更新您的問題以反映您的問題的性質或詢問兩個單獨的SO問題。謝謝。 – RLH

+0

@RLH我修改了我的問題,你會在這一個幫助.. –

回答

2

自題指的是邊界,我將專注於這一點。我假設你想要改變BorderStyle超出給定的「None」,「FixedSingle」和「Fixed3D」?

列表框不支持任何形式的BorderColors的等

最好的辦法是改變改變BorderStyle to "None"IntegralHeight = FalseDock = Fill,然後將其放置在面板內部。

對於您的面板,更改BorderStlye = None,Padding (All) = 2,BackColor = Red

使用WinForms,您的選擇是有限的。

+0

thanq larstech它的工作現在... –

+0

一個更多的問題,我想添加數據如圖所示,標題也....我會怎麼做你可以給任何想法.... –

+0

@ user899271你已經在使用DrawString,嘗試改變字體爲粗體的標題項目和改變你的e.Bounds矩形縮進到你的子項目的權利。而不是一個列表框,這看起來像一個TreeView,也許嘗試使用該控件。 – LarsTech