ListBoxEx.cs
Upload User: nnpulika
Upload Date: 2013-02-15
Package Size: 597k
Code Size: 2k
Category:

StatusBar

Development Platform:

C#

  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4. using System.ComponentModel;
  5. using UtilityLibrary.General;
  6. namespace UtilityLibrary.WinControls
  7. {
  8. /// <summary>
  9. /// ListBoxEx
  10. /// </summary>
  11. [ToolboxItem(false)]
  12. public class ListBoxEx : System.Windows.Forms.ListBox
  13. {
  14. #region Constructors
  15. public ListBoxEx()
  16. {
  17. DrawMode = DrawMode.OwnerDrawFixed;
  18. ItemHeight = ItemHeight + 1;
  19. }
  20. #endregion
  21. #region Overrides
  22. protected override void OnDrawItem(DrawItemEventArgs e)
  23. {
  24. DrawListBoxItem(e);
  25. }
  26. #endregion
  27. #region Implementation
  28. protected void DrawListBoxItem(DrawItemEventArgs e)
  29. {
  30. Graphics g = e.Graphics;
  31. Rectangle bounds = e.Bounds;
  32. bool selected = (e.State & DrawItemState.Selected) > 0;
  33. // Draw List box item
  34. if ( e.Index != -1)
  35. {
  36. if ( selected && Enabled )
  37. {
  38. Color fillColor = ColorUtil.VSNetSelectionColor;
  39. if ( !ContainsFocus ) fillColor = ColorUtil.VSNetControlColor;
  40. // Draw highlight rectangle
  41. using ( Brush b = new SolidBrush(fillColor) )
  42. {
  43. g.FillRectangle(b, bounds.Left, bounds.Top, bounds.Width, bounds.Height);
  44. }
  45. if ( ContainsFocus )
  46. {
  47. using ( Pen p = new Pen(ColorUtil.VSNetBorderColor) )
  48. {
  49. g.DrawRectangle(p, bounds.Left, bounds.Top, bounds.Width-1, bounds.Height-1);
  50. }
  51. }
  52. }
  53. else
  54. {
  55. g.FillRectangle(SystemBrushes.Window, bounds.Left, bounds.Top, bounds.Width, bounds.Height);
  56. }
  57. if ( Items.Count > 0 )
  58. {
  59. object currentObject = Items[e.Index];
  60. string item = currentObject as String;
  61. if ( item != null )
  62. {
  63. if ( Enabled )
  64. g.DrawString(item, SystemInformation.MenuFont, 
  65. SystemBrushes.ControlText, new Point(bounds.Left+2, bounds.Top));
  66. else
  67. g.DrawString(item, SystemInformation.MenuFont, 
  68. SystemBrushes.ControlDark, new Point(bounds.Left+2, bounds.Top));
  69. }
  70. }
  71. }
  72. }
  73. #endregion
  74. }
  75. }