TextBoxButton.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using FastReport.Utils;
  2. using System;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Windows.Forms;
  6. namespace FastReport.Controls
  7. {
  8. /// <summary>
  9. /// Represents the control that combines a textbox and a button.
  10. /// </summary>
  11. #if !DEBUG
  12. [DesignTimeVisible(false)]
  13. #endif
  14. public class TextBoxButton : UserControl
  15. {
  16. private TextBox textBox;
  17. private Button button;
  18. internal TextBox TextBox => textBox;
  19. internal Button Button => button;
  20. /// <inheritdoc/>
  21. public override string Text
  22. {
  23. get { return textBox.Text; }
  24. set { textBox.Text = value; }
  25. }
  26. /// <summary>
  27. /// Gets or sets the button's image.
  28. /// </summary>
  29. public Image Image
  30. {
  31. get { return button.Image; }
  32. set { button.Image = value; }
  33. }
  34. /// <summary>
  35. /// Gets or sets the button's text.
  36. /// </summary>
  37. public string ButtonText
  38. {
  39. get { return button.Text; }
  40. set { button.Text = value; }
  41. }
  42. /// <summary>
  43. /// Occurs when the button is clicked.
  44. /// </summary>
  45. public event EventHandler ButtonClick;
  46. /// <summary>
  47. /// Occurs when the text is changed.
  48. /// </summary>
  49. public new event EventHandler TextChanged;
  50. private void textBox_TextChanged(object sender, EventArgs e)
  51. {
  52. if (TextChanged != null)
  53. TextChanged(this, EventArgs.Empty);
  54. }
  55. private void button_Click(object sender, EventArgs e)
  56. {
  57. if (ButtonClick != null)
  58. ButtonClick(this, EventArgs.Empty);
  59. }
  60. private void LayoutControls()
  61. {
  62. int _1 = (int)Math.Round(this.LogicalToDevice(1f));
  63. int _2 = _1 + _1;
  64. int btnWidth = Height - _2;
  65. if (button != null)
  66. button.SetBounds(RightToLeft == RightToLeft.Yes ? _1 : Width - btnWidth - _1, _1, btnWidth, btnWidth);
  67. if (textBox != null)
  68. textBox.SetBounds(RightToLeft == RightToLeft.Yes ? Height : 3, (Height - textBox.PreferredHeight) / 2, Width - Height - 3, textBox.PreferredHeight);
  69. }
  70. /// <inheritdoc/>
  71. protected override void OnPaint(PaintEventArgs e)
  72. {
  73. Graphics g = e.Graphics;
  74. g.FillRectangle(Enabled ? SystemBrushes.Window : SystemBrushes.Control, DisplayRectangle);
  75. if (Enabled)
  76. this.DrawVisualStyleBorder(g, new Rectangle(0, 0, Width - 1, Height - 1));
  77. else
  78. g.DrawRectangle(SystemPens.InactiveBorder, new Rectangle(0, 0, Width - 1, Height - 1));
  79. }
  80. /// <inheritdoc/>
  81. protected override void OnResize(EventArgs e)
  82. {
  83. base.OnResize(e);
  84. LayoutControls();
  85. }
  86. /// <inheritdoc/>
  87. protected override void OnLayout(LayoutEventArgs e)
  88. {
  89. base.OnLayout(e);
  90. LayoutControls();
  91. }
  92. /// <summary>
  93. /// Set focus on text box.
  94. /// </summary>
  95. public void FocusTextBox()
  96. {
  97. textBox.Focus();
  98. }
  99. /// <inheritdoc/>
  100. /// <summary>
  101. /// Initializes a new instance of the <see cref="TextBoxButton"/> class.
  102. /// </summary>
  103. public TextBoxButton()
  104. {
  105. button = new Button();
  106. button.FlatStyle = FlatStyle.Flat;
  107. button.FlatAppearance.BorderSize = 0;
  108. button.Click += button_Click;
  109. Controls.Add(button);
  110. textBox = new TextBox();
  111. textBox.BorderStyle = BorderStyle.None;
  112. textBox.TextChanged += textBox_TextChanged;
  113. Controls.Add(textBox);
  114. // prevent autoscale of child controls
  115. AutoScaleMode = AutoScaleMode.None;
  116. SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer, true);
  117. }
  118. }
  119. }