ParametersComboBox.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. #if !DEBUG
  9. [DesignTimeVisible(false)]
  10. #endif
  11. internal class ParametersComboBox : UserControl
  12. {
  13. private TextBox textBox;
  14. private Button comboButton;
  15. private Report report;
  16. private DataColumnDropDown dropDown;
  17. private Timer timer;
  18. private int closedTicks;
  19. private int dpi;
  20. public event EventHandler DropDownOpening;
  21. public new event EventHandler TextChanged;
  22. /// <inheritdoc/>
  23. public override string Text
  24. {
  25. get { return textBox.Text; }
  26. set { textBox.Text = value; }
  27. }
  28. [Browsable(false)]
  29. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  30. public Report Report
  31. {
  32. get { return report; }
  33. set
  34. {
  35. report = value;
  36. dropDown.CreateNodes(value);
  37. }
  38. }
  39. private void FTextBox_TextChanged(object sender, EventArgs e)
  40. {
  41. if (TextChanged != null)
  42. TextChanged(this, EventArgs.Empty);
  43. }
  44. private void FDropDown_ColumnSelected(object sender, EventArgs e)
  45. {
  46. Text = String.IsNullOrEmpty(dropDown.Column) ? "" : dropDown.Column;
  47. timer.Start();
  48. }
  49. private void FDropDown_Closed(object sender, ToolStripDropDownClosedEventArgs e)
  50. {
  51. closedTicks = Environment.TickCount;
  52. }
  53. private void FComboButton_Click(object sender, EventArgs e)
  54. {
  55. if (Math.Abs(Environment.TickCount - closedTicks) < 100)
  56. return;
  57. DropDownOpening?.Invoke(this, EventArgs.Empty);
  58. dropDown.Column = Text;
  59. dropDown.SetSize(Width, 250);
  60. dropDown.Owner = this;
  61. dropDown.Show(this, new Point(RightToLeft == RightToLeft.Yes ? Width : 0, Height));
  62. }
  63. private void FTimer_Tick(object sender, EventArgs e)
  64. {
  65. FindForm().BringToFront();
  66. textBox.Focus();
  67. textBox.Select(Text.Length, 0);
  68. timer.Stop();
  69. }
  70. private void LayoutControls()
  71. {
  72. if (comboButton != null)
  73. comboButton.SetBounds(RightToLeft == RightToLeft.Yes ? 1 : Width - Height + 1, 1, Height - 2, Height - 2);
  74. if (textBox != null)
  75. textBox.SetBounds(RightToLeft == RightToLeft.Yes ? Height : 3, (Height - textBox.PreferredHeight) / 2 - 1, 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. ControlPaint.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. comboButton.Image = this.GetImage(182);
  94. }
  95. protected override void Dispose(bool disposing)
  96. {
  97. if (disposing)
  98. {
  99. if (timer != null)
  100. timer.Dispose();
  101. timer = null;
  102. }
  103. base.Dispose(disposing);
  104. }
  105. /// <inheritdoc/>
  106. protected override void OnResize(EventArgs e)
  107. {
  108. base.OnResize(e);
  109. LayoutControls();
  110. }
  111. /// <inheritdoc/>
  112. protected override void OnLayout(LayoutEventArgs e)
  113. {
  114. base.OnLayout(e);
  115. LayoutControls();
  116. }
  117. public ParametersComboBox()
  118. {
  119. comboButton = new Button();
  120. comboButton.FlatStyle = FlatStyle.Flat;
  121. comboButton.FlatAppearance.BorderSize = 0;
  122. comboButton.Click += new EventHandler(FComboButton_Click);
  123. Controls.Add(comboButton);
  124. textBox = new TextBox();
  125. textBox.BorderStyle = BorderStyle.None;
  126. textBox.TextChanged += new EventHandler(FTextBox_TextChanged);
  127. Controls.Add(textBox);
  128. dropDown = new DataColumnDropDown();
  129. dropDown.DataTree.ShowColumns = false;
  130. dropDown.DataTree.ShowDataSources = false;
  131. dropDown.DataTree.ShowRelations = false;
  132. dropDown.DataTree.ShowParameters = true;
  133. dropDown.ColumnSelected += new EventHandler(FDropDown_ColumnSelected);
  134. dropDown.Closed += new ToolStripDropDownClosedEventHandler(FDropDown_Closed);
  135. timer = new Timer();
  136. timer.Interval = 50;
  137. timer.Tick += new EventHandler(FTimer_Tick);
  138. // prevent autoscale of child controls
  139. AutoScaleMode = AutoScaleMode.None;
  140. SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer, true);
  141. }
  142. }
  143. }