DataColumnComboBox.cs 7.1 KB

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