DataColumnComboBox.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. using System;
  2. using System.Windows.Forms;
  3. using System.Drawing;
  4. using FastReport.Utils;
  5. using System.ComponentModel;
  6. using FastReport.Data;
  7. namespace FastReport.Controls
  8. {
  9. /// <summary>
  10. /// Represents the combobox used to select a data column.
  11. /// </summary>
  12. [ToolboxItem(false)]
  13. public class DataColumnComboBox : UserControl
  14. {
  15. private TextBox textBox;
  16. private Button button;
  17. private Button comboButton;
  18. private Report report;
  19. private DataColumnDropDown dropDown;
  20. private Timer timer;
  21. private int closedTicks;
  22. private int dpi;
  23. /// <summary>
  24. /// Occurs when the text portion of the combobox is changed.
  25. /// </summary>
  26. public new event EventHandler TextChanged;
  27. /// <inheritdoc/>
  28. public override string Text
  29. {
  30. get { return textBox.Text; }
  31. set { textBox.Text = value; }
  32. }
  33. /// <summary>
  34. /// Gets or sets the data source.
  35. /// </summary>
  36. [Browsable(false)]
  37. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  38. public DataSourceBase DataSource
  39. {
  40. get { return dropDown.DataSource; }
  41. set
  42. {
  43. // to recreate datasource list
  44. Report = Report;
  45. dropDown.DataSource = value;
  46. }
  47. }
  48. /// <summary>
  49. /// Gets or sets the Report.
  50. /// </summary>
  51. [Browsable(false)]
  52. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  53. public Report Report
  54. {
  55. get { return report; }
  56. set
  57. {
  58. report = value;
  59. dropDown.CreateNodes(value);
  60. }
  61. }
  62. private void FTextBox_TextChanged(object sender, EventArgs e)
  63. {
  64. if (TextChanged != null)
  65. TextChanged(this, EventArgs.Empty);
  66. }
  67. private void FDropDown_ColumnSelected(object sender, EventArgs e)
  68. {
  69. Text = String.IsNullOrEmpty(dropDown.Column) ? "" : "[" + dropDown.Column + "]";
  70. timer.Start();
  71. }
  72. private void FDropDown_Closed(object sender, ToolStripDropDownClosedEventArgs e)
  73. {
  74. closedTicks = Environment.TickCount;
  75. }
  76. private void FComboButton_Click(object sender, EventArgs e)
  77. {
  78. if (Math.Abs(Environment.TickCount - closedTicks) < 100)
  79. return;
  80. string column = Text.Replace("[", "");
  81. column = column.Replace("]", "");
  82. dropDown.Column = column;
  83. dropDown.SetSize(Width, 250);
  84. dropDown.RightToLeft = RightToLeft;
  85. dropDown.Owner = this;
  86. dropDown.Show(this, new Point(RightToLeft == RightToLeft.Yes ? Width : 0, Height));
  87. }
  88. private void FButton_Click(object sender, EventArgs e)
  89. {
  90. Text = Editors.EditExpression(Report, Text);
  91. timer.Start();
  92. }
  93. private void FTimer_Tick(object sender, EventArgs e)
  94. {
  95. FindForm().BringToFront();
  96. textBox.Focus();
  97. textBox.Select(Text.Length, 0);
  98. timer.Stop();
  99. }
  100. private void LayoutControls()
  101. {
  102. int btnWidth = Height - 2;
  103. if (button != null)
  104. button.SetBounds(RightToLeft == RightToLeft.Yes ? 1 : Width - btnWidth - 1, 1, btnWidth, btnWidth);
  105. if (comboButton != null)
  106. comboButton.SetBounds(RightToLeft == RightToLeft.Yes ? btnWidth + 1 : Width - btnWidth * 2 - 1, 1, btnWidth, btnWidth);
  107. if (textBox != null)
  108. textBox.SetBounds(RightToLeft == RightToLeft.Yes ? btnWidth * 2 + 1 : 3, (Height - textBox.PreferredHeight) / 2 - 1, Width - btnWidth * 2 - 3, textBox.PreferredHeight);
  109. }
  110. private void UpdateImages()
  111. {
  112. button.Image = this.GetImage(52);
  113. comboButton.Image = this.GetImage(182);
  114. }
  115. /// <inheritdoc/>
  116. protected override void OnPaint(PaintEventArgs e)
  117. {
  118. if (dpi != this.Dpi())
  119. {
  120. UpdateImages();
  121. dpi = this.Dpi();
  122. }
  123. Graphics g = e.Graphics;
  124. g.FillRectangle(Enabled ? SystemBrushes.Window : SystemBrushes.Control, DisplayRectangle);
  125. if (Enabled)
  126. ControlPaint.DrawVisualStyleBorder(g, new Rectangle(0, 0, Width - 1, Height - 1));
  127. else
  128. g.DrawRectangle(SystemPens.InactiveBorder, new Rectangle(0, 0, Width - 1, Height - 1));
  129. }
  130. /// <inheritdoc/>
  131. protected override void Dispose(bool disposing)
  132. {
  133. if (disposing)
  134. {
  135. if (timer != null)
  136. timer.Dispose();
  137. timer = null;
  138. }
  139. base.Dispose(disposing);
  140. }
  141. /// <inheritdoc/>
  142. protected override void OnResize(EventArgs e)
  143. {
  144. base.OnResize(e);
  145. LayoutControls();
  146. }
  147. /// <inheritdoc/>
  148. protected override void OnLayout(LayoutEventArgs e)
  149. {
  150. base.OnLayout(e);
  151. LayoutControls();
  152. }
  153. /// <summary>
  154. /// Initializes a new instance of the <see cref="DataColumnComboBox"/> class.
  155. /// </summary>
  156. public DataColumnComboBox()
  157. {
  158. button = new Button();
  159. button.FlatStyle = FlatStyle.Flat;
  160. button.FlatAppearance.BorderSize = 0;
  161. button.Click += new EventHandler(FButton_Click);
  162. Controls.Add(button);
  163. comboButton = new Button();
  164. comboButton.FlatStyle = FlatStyle.Flat;
  165. comboButton.FlatAppearance.BorderSize = 0;
  166. comboButton.Click += new EventHandler(FComboButton_Click);
  167. Controls.Add(comboButton);
  168. textBox = new TextBox();
  169. textBox.BorderStyle = BorderStyle.None;
  170. textBox.TextChanged += new EventHandler(FTextBox_TextChanged);
  171. Controls.Add(textBox);
  172. dropDown = new DataColumnDropDown();
  173. dropDown.ColumnSelected += new EventHandler(FDropDown_ColumnSelected);
  174. dropDown.Closed += new ToolStripDropDownClosedEventHandler(FDropDown_Closed);
  175. timer = new Timer();
  176. timer.Interval = 50;
  177. timer.Tick += new EventHandler(FTimer_Tick);
  178. // prevent autoscale of child controls
  179. AutoScaleMode = AutoScaleMode.None;
  180. SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw, true);
  181. }
  182. }
  183. }