using FastReport.Utils; using System; using System.ComponentModel; using System.Drawing; using System.Windows.Forms; namespace FastReport.Controls { /// /// Represents the control that combines a textbox and a button. /// #if !DEBUG [DesignTimeVisible(false)] #endif public class TextBoxButton : UserControl { private TextBox textBox; private Button button; internal TextBox TextBox => textBox; internal Button Button => button; /// public override string Text { get { return textBox.Text; } set { textBox.Text = value; } } /// /// Gets or sets the button's image. /// public Image Image { get { return button.Image; } set { button.Image = value; } } /// /// Gets or sets the button's text. /// public string ButtonText { get { return button.Text; } set { button.Text = value; } } /// /// Occurs when the button is clicked. /// public event EventHandler ButtonClick; /// /// Occurs when the text is changed. /// 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); } /// 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)); } /// protected override void OnResize(EventArgs e) { base.OnResize(e); LayoutControls(); } /// protected override void OnLayout(LayoutEventArgs e) { base.OnLayout(e); LayoutControls(); } /// /// Set focus on text box. /// public void FocusTextBox() { textBox.Focus(); } /// /// /// Initializes a new instance of the class. /// 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); } } }