Advertisement
Balda

Textbox with placeholder

Dec 19th, 2022
742
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.55 KB | Source Code | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10.  
  11. namespace Diplom.Custom_Elements
  12. {
  13.     class TextBoxWithPlaceholder : TextBox
  14.     {
  15.         private string PlaceholderTextDefault = string.Empty;
  16.         private Color PlaceholderColorDefault = Color.Gray;
  17.  
  18.         [Category("Appearance")]
  19.         public string PlaceholderText { get => PlaceholderTextDefault; set => PlaceholderTextDefault = value; }
  20.         [Category("Appearance")]
  21.         public Color PlaceholderColor { get => PlaceholderColorDefault; set => PlaceholderColorDefault = value; }
  22.  
  23.         protected override void OnCreateControl()
  24.         {
  25.             base.OnCreateControl();
  26.  
  27.             if (!DesignMode)
  28.             {
  29.                 this.Text = PlaceholderText;
  30.                 this.ForeColor = PlaceholderColor;
  31.             }
  32.         }
  33.  
  34.         protected override void OnLostFocus(EventArgs e)
  35.         {
  36.             base.OnLostFocus(e);
  37.  
  38.             if (Text.Equals(string.Empty))
  39.             {
  40.                 this.Text = PlaceholderText;
  41.                 this.ForeColor = PlaceholderColor;
  42.             }
  43.         }
  44.  
  45.         protected override void OnGotFocus(EventArgs e)
  46.         {
  47.             base.OnGotFocus(e);
  48.  
  49.             if (Text.Equals(PlaceholderText))
  50.             {
  51.                 this.Text = string.Empty;
  52.                 this.ForeColor = Color.Black;
  53.             }
  54.         }
  55.     }
  56. }
  57.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement