Tag:开源 , 源码下载 , 控件 , 控件开发 , 皮肤 , Skin , Socket , 水印 , QQ表情 , 透明控件 , 异步编程 , 多线程编程 , WebService , WCF , WPF , Ribbon , 数据库 , SQL , WinForm教程 , SQLite , 接口 , 抽象类 , 设计模式 , 架构 , 插件 , Visual Studio , P/Invoke , C# ListBox控件

 
您的位置: >> 首页 >> C# 博文 >> C# ListBox控件学习笔记

C# ListBox控件学习笔记

2009-11-25  来自:网上整理  字体大小:【  
  • 摘要:本文介绍C# ListBox,以及介绍为 MeasureItem 事件创建事件处理程序,以在 DrawItem 事件的事件处理程序中指定要绘制的项的大小。

    学习C# ListBox使用,仅当 DrawMode 属性设置为 DrawMode.OwnerDrawFixed 或 DrawMode.OwnerDrawVariable 时,才引发该事件。可以使用该事件来执行在C# ListBox 中绘制项所需的任务。如果具有大小可变的项(当 DrawMode 属性设置为 DrawMode.OwnerDrawVariable 时),在绘制项前,引发 MeasureItem 事件。可以为 MeasureItem 事件创建事件处理程序,以在 DrawItem 事件的事件处理程序中指定要绘制的项的大小。有关处理事件的更多信息,请参见 使用事件。

    重写C# Listbox的drawitem事件

  1. private void listBox1_DrawItem(objects ender,DrawItemEventArgs e){  
  2. //SettheDrawModepropertytodrawfixedsizeditems.  
  3. listBox1.DrawMode=DrawMode.OwnerDrawFixed;  
  4. //DrawthebackgroundoftheListBoxcontrolforeachitem.  
  5. e.DrawBackground();  
  6. //Definethedefaultcolorofthebrushasblack.  
  7. Brush myBrush=Brushes.Black;  
  8. //Determinethecolorofthebrushtodraweachitembasedontheindexoftheitemtodraw.  
  9. switch(e.Index){  
  10. case 0: myBrush=Brushes.Red;break;  
  11. case 1: myBrush=Brushes.Orange;break;  
  12. case 2: myBrush=Brushes.Purple;break;  
  13. }  
  14. //DrawthecurrentitemtextbasedonthecurrentFontandthecustombrushsettings.  
  15. e.Graphics.DrawString(listBox1.Items[e.Index].ToString(),e.Font,myBrush,  
  16. e.Bounds,StringFormat.GenericDefault);  
  17. //IftheListBoxhasfocus,drawafocusrectanglearoundtheselecteditem.  
  18. e.DrawFocusRectangle();  

    从这个例子,我们发现在C#下面重画控件,比在vc++6.0中定义自绘方便多了。

该文章已有条评论 我要发表评论