1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- using System;
- using System.Drawing;
- using System.Windows.Forms;
- namespace FastReport.Controls
- {
- internal class SearchBox : TextBoxButton
- {
- private string emptyText;
- private string saveText;
- public string EmptyText
- {
- get => emptyText;
- set
- {
- emptyText = value;
- if (TextBox.Text == "")
- TextBox.Text = value;
- }
- }
- public override string Text
- {
- get => TextBox.Text == EmptyText ? "" : TextBox.Text;
- set => TextBox.Text = value == "" ? EmptyText : value;
- }
- public event EventHandler Search;
- protected virtual void OnSearch(EventArgs e)
- {
- saveText = Text;
- Search?.Invoke(this, e);
- }
- private void textBox_GotFocus(object sender, EventArgs e)
- {
- if (TextBox.Text == EmptyText)
- TextBox.Text = "";
- saveText = Text;
- }
- private void textBox_LostFocus(object sender, EventArgs e)
- {
- var timer = new Timer() { Interval = 50, Enabled = true };
- timer.Tick += (s, args) =>
- {
- if (!Button.Focused)
- Text = saveText;
- timer.Dispose();
- };
- }
- private void textBox_KeyPress(object sender, KeyPressEventArgs e)
- {
- if (e.KeyChar == (char)13)
- {
- e.Handled = true;
- OnSearch(e);
- }
- else if (e.KeyChar == (char)27)
- {
- e.Handled = true;
- TextBox.Text = saveText;
- }
- }
- public SearchBox()
- {
- Button.BackColor = SystemColors.Window;
- ButtonClick += (s, e) =>
- {
- OnSearch(e);
- };
- TextBox.GotFocus += textBox_GotFocus;
- TextBox.LostFocus += textBox_LostFocus;
- TextBox.KeyPress += textBox_KeyPress;
- TextBox.PreviewKeyDown += (s, e) =>
- {
- if (e.KeyCode == Keys.Escape)
- e.IsInputKey = true;
- };
- }
- }
- }
|