C# ListBox有什么作用

C# ListBox有什么作用

本篇内容介绍了“C# ListBox有什么作用”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

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

重写C# Listbox的drawitem事件

privatevoidlistBox1_DrawItem(objectsender,System.Windows.Forms.DrawItemEventArgse){  //SettheDrawModepropertytodrawfixedsizeditems.  listBox1.DrawMode=DrawMode.OwnerDrawFixed;  //DrawthebackgroundoftheListBoxcontrolforeachitem.  e.DrawBackground();  //Definethedefaultcolorofthebrushasblack.  BrushmyBrush=Brushes.Black;  //Determinethecolorofthebrushtodraweachitembasedontheindexoftheitemtodraw.  switch(e.Index){  case0:myBrush=Brushes.Red;break;  case1:myBrush=Brushes.Orange;break;  case2:myBrush=Brushes.Purple;break;  }  //DrawthecurrentitemtextbasedonthecurrentFontandthecustombrushsettings.  e.Graphics.DrawString(listBox1.Items[e.Index].ToString(),e.Font,myBrush,  e.Bounds,StringFormat.GenericDefault);  //IftheListBoxhasfocus,drawafocusrectanglearoundtheselecteditem.  e.DrawFocusRectangle();  }

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

“C# ListBox有什么作用”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!