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

StatusBar

Development Platform:

C#

  1. using System;
  2. using System.Collections;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Windows.Forms;
  7. using System.Diagnostics;
  8. using System.Reflection;
  9. using UtilityLibrary.General;
  10. using UtilityLibrary.Win32;
  11. namespace UtilityLibrary.WinControls
  12. {
  13. /// <summary>
  14. /// Summary description for TextComboBox.
  15. /// </summary>
  16. [ToolboxItem(false)]
  17. public class TextComboBox : ComboBoxBase, IListViewEmbeddedControl
  18. {
  19. #region Constructors
  20. // For use when hosted by a toolbar
  21. public TextComboBox(bool toolBarUse) : base(toolBarUse)
  22. {
  23. // Override parent, we don't want to do all the painting ourselves
  24. // since we want to let the edit control deal with the text for editing
  25. // the parent class ComboBoxBase knows to do the right stuff with 
  26. // non-editable comboboxes as well as editable comboboxes as long
  27. // as we change these flags below
  28. SetStyle(ControlStyles.AllPaintingInWmPaint
  29. |ControlStyles.UserPaint|ControlStyles.Opaque, false);
  30. }
  31. public TextComboBox()
  32. {
  33. // Override parent, we don't want to do all the painting ourselves
  34. // since we want to let the edit control deal with the text for editing
  35. // the parent class ComboBoxBase knows to do the right stuff with 
  36. // non-editable comboboxes as well as editable comboboxes as long
  37. // as we change these flags below
  38. SetStyle(ControlStyles.AllPaintingInWmPaint
  39. |ControlStyles.UserPaint|ControlStyles.Opaque, false);
  40. }
  41. #endregion
  42. #region Overrides
  43. protected override void DrawComboBoxItem(Graphics g, Rectangle bounds, int Index, bool selected, bool editSel)
  44. {
  45. // Call base class to do the "Flat ComboBox" drawing
  46. base.DrawComboBoxItem(g, bounds, Index, selected, editSel);
  47. if ( Index != -1)
  48. {
  49. SolidBrush brush;
  50. brush = new SolidBrush(SystemColors.MenuText);
  51. Size textSize = TextUtil.GetTextSize(g, Items[Index].ToString(), Font);
  52. int top = bounds.Top + (bounds.Height - textSize.Height)/2;
  53. // Check if the combobox is bound to a Data Source
  54.                 if ( DataSource != null)
  55. DrawDataBoundMember(g, brush, Index, new Point(bounds.Left + 1, top));
  56. else
  57. g.DrawString(Items[Index].ToString(), Font, brush, new Point(bounds.Left + 1, top));
  58. brush.Dispose();
  59. }
  60. }
  61. protected override void DrawComboBoxItemEx(Graphics g, Rectangle bounds, int Index, bool selected, bool editSel)
  62. {
  63. // This "hack" is necessary to avoid a clipping bug that comes from the fact that sometimes
  64. // we are drawing using the Graphics object for the edit control in the combobox and sometimes
  65. // we are using the graphics object for the combobox itself. If we use the same function to do our custom
  66. // drawing it is hard to adjust for the clipping because of what was said about
  67. base.DrawComboBoxItemEx(g, bounds, Index, selected, editSel);
  68. if ( Index != -1)
  69. {
  70. SolidBrush brush;
  71. brush = new SolidBrush(SystemColors.MenuText);
  72. Size textSize = TextUtil.GetTextSize(g, Items[Index].ToString(), Font);
  73. int top = bounds.Top + (bounds.Height - textSize.Height)/2;
  74. // Clipping rectangle
  75. Rectangle clipRect = new Rectangle(bounds.Left + 4, top, bounds.Width - ARROW_WIDTH - 4, top+textSize.Height);
  76. Debug.WriteLine(Items[Index].ToString());
  77. if ( DataSource != null )
  78. DrawDataBoundMember(g, brush, Index, new Point(bounds.Left + 4, top));
  79. else
  80.                     g.DrawString(Items[Index].ToString(), Font, brush, clipRect);
  81. brush.Dispose();
  82. }
  83. }
  84. protected override void DrawDisableState()
  85. {
  86. // Draw the combobox state disable
  87. base.DrawDisableState();
  88. // Draw the specific disable state to
  89. // this derive class
  90. int index = SelectedIndex;
  91. if ( index == -1 ) return;
  92. using ( Graphics g = CreateGraphics() )
  93. {
  94. using ( Brush b = new SolidBrush(SystemColors.ControlDark) )
  95. {
  96. Rectangle rc = ClientRectangle;
  97. Size textSize = TextUtil.GetTextSize(g, Items[index].ToString(), Font);
  98. // Clipping rectangle
  99. int top = rc.Top + (rc.Height - textSize.Height)/2;
  100. Rectangle clipRect = new Rectangle(rc.Left + 4, 
  101. top, rc.Width - 4 - ARROW_WIDTH - 4, top+textSize.Height);
  102. g.DrawString(Items[index].ToString(), Font, b, clipRect);
  103. }
  104. }
  105. }
  106. protected void DrawDataBoundMember(Graphics g, Brush brush, int index, Point point)
  107. {
  108. string text = string.Empty;
  109. if ( DataSource != null )
  110. {
  111. IList list = null;
  112. // We coud be bound to an array list which implement the IList interface
  113. // or we could be bound to a DataSet which implements the IListSource from which
  114. // we can get the IList interface
  115. if ( DataSource is IList)
  116. {
  117. // Assume this is an array object and try to get the data based
  118. // on this assumption
  119. list = (IList)DataSource;
  120. Debug.Assert(list != null, "ComboBox is bound to unrecognized DataSource");
  121. // Now extract the actual text to be displayed
  122. object o = list[index];
  123. Type objectType = o.GetType();
  124. // Now invoke the method that is associate with the
  125. // Display name property of the ComboBox
  126. PropertyInfo pi = objectType.GetProperty(DisplayMember);
  127. text = (pi!=null)?(string)pi.GetValue(o, null):o.ToString();
  128. }
  129. else
  130. {
  131. // Data set object
  132. if ( DataSource is IListSource )
  133. {
  134. IListSource ls = (IListSource)DataSource;
  135. list = ls.GetList();
  136. Debug.Assert(list != null, "ComboBox is bound to unrecognized DataSource");
  137.                         // This is a data set object, get the value under that assumption
  138. DataRowView dataRowView = (DataRowView)list[index];
  139. DataRow dataRow = dataRowView.Row;
  140. object o = dataRow[DisplayMember];
  141. text = o.ToString();
  142. }
  143. }
  144. Debug.WriteLine(text);
  145. g.DrawString(text, Font, brush, point);
  146. }
  147. }
  148. #endregion
  149. #region Methods
  150. public void FastRender(Graphics g, Rectangle bounds)
  151. {
  152. // Draw a quick representation of the combo box
  153. // so that it can be used as an embedded control in a list view
  154.             
  155. // Get current text
  156. object selectedItem = SelectedItem;
  157. string text = selectedItem.ToString();
  158. // Draw text
  159. Size textSize = TextUtil.GetTextSize(g, text, Font);
  160. int top = bounds.Top + (bounds.Height - textSize.Height)/2;
  161. Debug.WriteLine("Drawing Text: " + text);
  162. TextUtil.DrawText(g, text, Font, new Rectangle(bounds.Left + 4, top, textSize.Width, textSize.Height));
  163. // Draw arrow background and arrow glyph
  164. Rectangle arrowRect = new Rectangle(bounds.Right-ARROW_WIDTH-1, bounds.Top+2, ARROW_WIDTH, bounds.Height-4);
  165. using ( Brush b = new SolidBrush(ColorUtil.VSNetControlColor) )
  166. {
  167. g.FillRectangle(b, arrowRect);
  168. }
  169. // Draw glyph
  170. GDIUtil.DrawArrowGlyph(g, arrowRect, ArrowGlyph.Down, SystemBrushes.ControlText);
  171. }
  172. #endregion
  173. }
  174. }