ParametersComboBox.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. using System;
  2. using System.Windows.Forms;
  3. using System.Drawing;
  4. using FastReport.Utils;
  5. using System.ComponentModel;
  6. namespace FastReport.Controls
  7. {
  8. internal class ParametersComboBox : UserControl
  9. {
  10. private TextBox textBox;
  11. private Button comboButton;
  12. private Report report;
  13. private DataColumnDropDown dropDown;
  14. private Timer timer;
  15. private int closedTicks;
  16. private int dpi;
  17. public event EventHandler DropDownOpening;
  18. public new event EventHandler TextChanged;
  19. /// <inheritdoc/>
  20. public override string Text
  21. {
  22. get { return textBox.Text; }
  23. set { textBox.Text = value; }
  24. }
  25. [Browsable(false)]
  26. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  27. public Report Report
  28. {
  29. get { return report; }
  30. set
  31. {
  32. report = value;
  33. dropDown.CreateNodes(value);
  34. }
  35. }
  36. private void FTextBox_TextChanged(object sender, EventArgs e)
  37. {
  38. if (TextChanged != null)
  39. TextChanged(this, EventArgs.Empty);
  40. }
  41. private void FDropDown_ColumnSelected(object sender, EventArgs e)
  42. {
  43. Text = String.IsNullOrEmpty(dropDown.Column) ? "" : dropDown.Column;
  44. timer.Start();
  45. }
  46. private void FDropDown_Closed(object sender, ToolStripDropDownClosedEventArgs e)
  47. {
  48. closedTicks = Environment.TickCount;
  49. }
  50. private void FComboButton_Click(object sender, EventArgs e)
  51. {
  52. if (Math.Abs(Environment.TickCount - closedTicks) < 100)
  53. return;
  54. DropDownOpening?.Invoke(this, EventArgs.Empty);
  55. dropDown.Column = Text;
  56. dropDown.SetSize(Width, 250);
  57. dropDown.Owner = this;
  58. dropDown.Show(this, new Point(RightToLeft == RightToLeft.Yes ? Width : 0, Height));
  59. }
  60. private void FTimer_Tick(object sender, EventArgs e)
  61. {
  62. FindForm().BringToFront();
  63. textBox.Focus();
  64. textBox.Select(Text.Length, 0);
  65. timer.Stop();
  66. }
  67. private void LayoutControls()
  68. {
  69. int _1 = (int)Math.Round(this.LogicalToDevice(1f));
  70. int _2 = _1 + _1;
  71. int btnWidth = Height - _2;
  72. if (comboButton != null)
  73. comboButton.SetBounds(RightToLeft == RightToLeft.Yes ? _1 : Width - btnWidth - _1, _1, btnWidth, btnWidth);
  74. if (textBox != null)
  75. textBox.SetBounds(RightToLeft == RightToLeft.Yes ? Height : 3, (Height - textBox.PreferredHeight) / 2, Width - Height - 3, textBox.PreferredHeight);
  76. }
  77. protected override void OnPaint(PaintEventArgs e)
  78. {
  79. if (dpi != this.Dpi())
  80. {
  81. UpdateImages();
  82. dpi = this.Dpi();
  83. }
  84. Graphics g = e.Graphics;
  85. g.FillRectangle(Enabled ? SystemBrushes.Window : SystemBrushes.Control, DisplayRectangle);
  86. if (Enabled)
  87. this.DrawVisualStyleBorder(g, new Rectangle(0, 0, Width - 1, Height - 1));
  88. else
  89. g.DrawRectangle(SystemPens.InactiveBorder, new Rectangle(0, 0, Width - 1, Height - 1));
  90. }
  91. private void UpdateImages()
  92. {
  93. #if AVALONIA
  94. // doing this in the render code will result in InvalidOp exception
  95. Avalonia.Threading.Dispatcher.UIThread.InvokeAsync(() =>
  96. {
  97. comboButton.Image = this.GetImage(182);
  98. });
  99. #else
  100. comboButton.Image = this.GetImage(182);
  101. #endif
  102. }
  103. protected override void Dispose(bool disposing)
  104. {
  105. if (disposing)
  106. {
  107. if (timer != null)
  108. timer.Dispose();
  109. timer = null;
  110. }
  111. base.Dispose(disposing);
  112. }
  113. /// <inheritdoc/>
  114. protected override void OnResize(EventArgs e)
  115. {
  116. base.OnResize(e);
  117. LayoutControls();
  118. }
  119. /// <inheritdoc/>
  120. protected override void OnLayout(LayoutEventArgs e)
  121. {
  122. base.OnLayout(e);
  123. LayoutControls();
  124. }
  125. public ParametersComboBox()
  126. {
  127. comboButton = new Button();
  128. comboButton.FlatStyle = FlatStyle.Flat;
  129. comboButton.FlatAppearance.BorderSize = 0;
  130. comboButton.Click += new EventHandler(FComboButton_Click);
  131. Controls.Add(comboButton);
  132. textBox = new TextBox();
  133. textBox.BorderStyle = BorderStyle.None;
  134. textBox.TextChanged += new EventHandler(FTextBox_TextChanged);
  135. Controls.Add(textBox);
  136. dropDown = new DataColumnDropDown();
  137. dropDown.DataTree.ShowColumns = false;
  138. dropDown.DataTree.ShowDataSources = false;
  139. dropDown.DataTree.ShowRelations = false;
  140. dropDown.DataTree.ShowParameters = true;
  141. dropDown.ColumnSelected += new EventHandler(FDropDown_ColumnSelected);
  142. dropDown.Closed += new ToolStripDropDownClosedEventHandler(FDropDown_Closed);
  143. timer = new Timer();
  144. timer.Interval = 50;
  145. timer.Tick += new EventHandler(FTimer_Tick);
  146. // prevent autoscale of child controls
  147. AutoScaleMode = AutoScaleMode.None;
  148. SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer, true);
  149. }
  150. }
  151. }