WinForm中自定义搜索框(水印、清空按钮、加载中图标)

  1    public partial class CustomSearchBar : TextBox
  2     {
  3         private readonly Label lblwaterText = new Label();
  4 
  5         private readonly PictureBox clearButton = new PictureBox();
  6 
  7         private readonly int cancelImageSize = 12;
  8 
  9         private int loadingCharsCount = 0;
 10 
 11         public CustomSearchBar()
 12         {
 13             InitializeComponent();
 14 
 15             lblwaterText.BorderStyle = BorderStyle.None;
 16             lblwaterText.Enabled = false;
 17             lblwaterText.BackColor = Color.White;
 18             lblwaterText.AutoSize = false;
 19             lblwaterText.Top = 1;
 20             lblwaterText.Left = 2;
 21             lblwaterText.FlatStyle = FlatStyle.System;
 22             Controls.Add(lblwaterText);
 23 
 24             clearButton.Size = new Size(cancelImageSize, cancelImageSize);
 25             //clearButton.AutoSize = false;
 26             clearButton.SizeMode = PictureBoxSizeMode.StretchImage;
 27             //clearButton.Image = ResourceHandler.LoadImage("Loading");
 28             clearButton.Top = 1;
 29             clearButton.Left = this.Width - cancelImageSize - 2;
 30             clearButton.Click += new EventHandler(clearButton_Click);
 31             Controls.Add(clearButton);
 32         }
 33 
 34         public void AdjustClearButtonPosition(int width)
 35         {
 36             this.clearButton.Left = width - cancelImageSize - 2;
 37         }
 38 
 39         public string WaterText
 40         {
 41             get { return lblwaterText.Text; }
 42             set { lblwaterText.Text = value; }
 43         }
 44 
 45         public override string Text
 46         {
 47             set
 48             {
 49                 lblwaterText.Visible = value == string.Empty;
 50                 base.Text = value;
 51             }
 52             get
 53             {
 54                 return base.Text;
 55             }
 56         }
 57 
 58         protected override void OnSizeChanged(EventArgs e)
 59         {
 60             if (Multiline && (ScrollBars == ScrollBars.Vertical || ScrollBars == ScrollBars.Both))
 61                 lblwaterText.Width = Width - 20;
 62             else
 63                 lblwaterText.Width = Width;
 64             lblwaterText.Height = Height - 2;
 65             base.OnSizeChanged(e);
 66         }
 67 
 68         protected override void OnTextChanged(EventArgs e)
 69         {
 70             lblwaterText.Visible = base.Text == string.Empty;
 71             base.OnTextChanged(e);
 72         }
 73 
 74         protected override void OnMouseDown(MouseEventArgs e)
 75         {
 76             lblwaterText.Visible = false;
 77             base.OnMouseDown(e);
 78         }
 79 
 80         protected override void OnMouseLeave(EventArgs e)
 81         {
 82             lblwaterText.Visible = base.Text == string.Empty;
 83             base.OnMouseLeave(e);
 84         }
 85 
 86         /// <summary>
 87         /// 
 88         /// </summary>
 89         /// <param name="sender"></param>
 90         /// <param name="e"></param>
 91         private void clearButton_Click(object sender, EventArgs e)
 92         {
 93             this.Text = string.Empty;
 94             ClearSearchingTextHandler();
 95         }
 96 
 97         public void StartLoadSmartTip()
 98         {
 99             this.clearButton.Image = ResourceHandler.LoadImage("Loading");
100             loadingCharsCount++;
101         }
102 
103         public void EndLoadSmartTip()
104         {
105             loadingCharsCount--;
106             if (loadingCharsCount <= 0)
107             {
108                 this.clearButton.Image = ResourceHandler.LoadImage("CloseButtonInactive");
109             }
110         }
111 
112         public delegate void ClearSearchingText();
113         public event ClearSearchingText ClearSearchingTextHandler;
114     }