TextBoxButton.cs 3.9 KB

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