123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- using FastReport.Utils;
- using System;
- using System.ComponentModel;
- using System.Drawing;
- using System.Windows.Forms;
- namespace FastReport.Controls
- {
- /// <summary>
- /// Represents the control that combines a textbox and a button.
- /// </summary>
- #if !DEBUG
- [DesignTimeVisible(false)]
- #endif
- public class TextBoxButton : UserControl
- {
- private TextBox textBox;
- private Button button;
- internal TextBox TextBox => textBox;
- internal Button Button => button;
- /// <inheritdoc/>
- public override string Text
- {
- get { return textBox.Text; }
- set { textBox.Text = value; }
- }
- /// <summary>
- /// Gets or sets the button's image.
- /// </summary>
- public Image Image
- {
- get { return button.Image; }
- set { button.Image = value; }
- }
- /// <summary>
- /// Gets or sets the button's text.
- /// </summary>
- public string ButtonText
- {
- get { return button.Text; }
- set { button.Text = value; }
- }
- /// <summary>
- /// Occurs when the button is clicked.
- /// </summary>
- public event EventHandler ButtonClick;
- /// <summary>
- /// Occurs when the text is changed.
- /// </summary>
- public new event EventHandler TextChanged;
- private void textBox_TextChanged(object sender, EventArgs e)
- {
- if (TextChanged != null)
- TextChanged(this, EventArgs.Empty);
- }
- private void button_Click(object sender, EventArgs e)
- {
- if (ButtonClick != null)
- ButtonClick(this, EventArgs.Empty);
- }
- private void LayoutControls()
- {
- int _1 = (int)Math.Round(this.LogicalToDevice(1f));
- int _2 = _1 + _1;
- int btnWidth = Height - _2;
- if (button != null)
- button.SetBounds(RightToLeft == RightToLeft.Yes ? _1 : Width - btnWidth - _1, _1, btnWidth, btnWidth);
- if (textBox != null)
- textBox.SetBounds(RightToLeft == RightToLeft.Yes ? Height : 3, (Height - textBox.PreferredHeight) / 2, Width - Height - 3, textBox.PreferredHeight);
- }
- /// <inheritdoc/>
- protected override void OnPaint(PaintEventArgs e)
- {
- Graphics g = e.Graphics;
- g.FillRectangle(Enabled ? SystemBrushes.Window : SystemBrushes.Control, DisplayRectangle);
- if (Enabled)
- this.DrawVisualStyleBorder(g, new Rectangle(0, 0, Width - 1, Height - 1));
- else
- g.DrawRectangle(SystemPens.InactiveBorder, new Rectangle(0, 0, Width - 1, Height - 1));
- }
- /// <inheritdoc/>
- protected override void OnResize(EventArgs e)
- {
- base.OnResize(e);
- LayoutControls();
- }
- /// <inheritdoc/>
- protected override void OnLayout(LayoutEventArgs e)
- {
- base.OnLayout(e);
- LayoutControls();
- }
- /// <summary>
- /// Set focus on text box.
- /// </summary>
- public void FocusTextBox()
- {
- textBox.Focus();
- }
- /// <inheritdoc/>
- /// <summary>
- /// Initializes a new instance of the <see cref="TextBoxButton"/> class.
- /// </summary>
- public TextBoxButton()
- {
- button = new Button();
- button.FlatStyle = FlatStyle.Flat;
- button.FlatAppearance.BorderSize = 0;
- button.Click += button_Click;
- Controls.Add(button);
- textBox = new TextBox();
- textBox.BorderStyle = BorderStyle.None;
- textBox.TextChanged += textBox_TextChanged;
- Controls.Add(textBox);
- // prevent autoscale of child controls
- AutoScaleMode = AutoScaleMode.None;
- SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer, true);
- }
- }
- }
|