_C/C++语言_黑客防线网安服务器维护基地--Powered by WWW.RONGSEN.COM.CN

C# 中改变listbox的item颜色的方法

作者:黑客防线网安C/C++教程基地 来源:黑客防线网安C/C++教程基地 浏览次数:0

本篇关键词:颜色方法改变 
   (1)需要先设置属性:
该事件由所有者描述的 ListBox 使用仅当 DrawMode 属性设置为 DrawMode.OwnerDrawFixed 或 DrawMode.OwnerDrawVariable 时才引发该事件可以使用该事件来执行在 ListBox 中绘制项所需的任务。如果具有大小可变的项(当 DrawMode 属性设置为 DrawMode.OwnerDrawVariable 时)在绘制项前,引发 MeasureItem 事件。可以为 MeasureItem 事件创建事件处理程序,以在 DrawItem 事件的事件处理程序中指定要绘制的项的大小。有关处理事件的更多信息,请参见 使用事件。
(2)重写listbox的drawitem事件
private void listBox1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e){   // Set the DrawMode property to draw fixed sized items. 
 listBox1.DrawMode = DrawMode.OwnerDrawFixed;  
// Draw the background of the ListBox control for each item.  
e.DrawBackground();   // Define the default color of the brush as black.  
Brush myBrush = Brushes.Black;  
// Determine the color of the brush to draw each item based on the index of the item to draw.  
switch (e.Index)   {      case 0:         myBrush = Brushes.Red;         break;     
case 1:         myBrush = Brushes.Orange;         break;     
case 2:         myBrush = Brushes.Purple;         break;   }  
// Draw the current item text based on the current Font and the custom brush settings.  
e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), e.Font, myBrush,
e.Bounds,StringFormat.GenericDefault);  
// If the ListBox has focus, draw a focus rectangle around the selected item.  
e.DrawFocusRectangle();}
(3)从这个例子,我们发现在c#下面重画控件,比在vc++6.0中定义自绘方便多了

    希望这篇C# 中改变listbox的item颜色的方法的文章能够对您有所帮助,如果您觉得这篇网站维护教程有用的话,别忘了推荐给您的朋友哦!如果您有好的经验方法,不妨拿出来和大家一起分享:假如每个人都拿出一个经验,那么我们都将额外的获取一堆他人的经验。
请记住本站永久域名:(黑客防线网安服务器维护方案维护基地)Www.Rongsen.Com.Cn
    黑客防线网安服务器维护方案本篇连接:http://shop.rongsen.com.cn/show-15188-1.html
网站维护教程更新时间:2012-04-04 22:51:23  【打印此页】  【关闭
0
顶一下
0
踩一下
我要申请本站N点 | 黑客防线官网 |  
专业服务器维护及网站维护手工安全搭建环境,网站安全加固服务。黑客防线网安服务器维护基地招商进行中!QQ:29769479

footer  footer  footer  footer