2011-08-23 23 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