Implementing ButtonTextClear on TextBox WinForms

Introduction 

 
One of the top features of mobile apps is the 'x' button on the Textboxes, making it very easy to clear a textbox form.
 
In this post, I'll show you how to implement it by sharing a code made by myself. It can be used for all your C# WinForms Apps just by changing the object name.
 
This works for both .NET Framework and .NET Core 3.0.
 

Implementation of the Textbox heritage

  1. using System.Drawing;  
  2.   
  3. namespace System.Windows.Forms  
  4. {  
  5.     public class MyTextBox : TextBox  
  6.     {  
  7.         private readonly Label lblTheClose;  
  8.   
  9.         public bool ButtonTextClear { getset; } = true;  
  10.   
  11.         public MyTextBox()  
  12.         {  
  13.             Resize += PositionX;             
  14.   
  15.             TextChanged += ShowHideX;  
  16.   
  17.             lblTheClose = new Label()  
  18.             {  
  19.                 Location = new Point(100, 0),  
  20.                 AutoSize = true,  
  21.                 Text = "x",  
  22.                 ForeColor = Color.Gray,  
  23.                 Visible = false,  
  24.                 Font = new Font("Tahoma", 8.25F),  
  25.                 Cursor = Cursors.Arrow  
  26.             };  
  27.   
  28.             Controls.Add(lblTheClose);  
  29.             lblTheClose.Click += (ss, ee) => { ((Label)ss).Visible = false; Text = string.Empty; };  
  30.             lblTheClose.BringToFront();  
  31.         }  
  32.           
  33.         private void ShowHideX(object sender, EventArgs e) => lblTheClose.Visible = ButtonTextClear && !string.IsNullOrEmpty(Text);  
  34.         private void PositionX(object sender, EventArgs e) => lblTheClose.Location = new Point(Width - 15, ((Height - lblTheClose.Height) / 2)-3);  
  35.     }  
  36. }  
Form Sample 
  1. using System.Drawing;  
  2.   
  3. namespace System.Windows.Forms  
  4. {  
  5.     public partial class Form1 : Form  
  6.     {  
  7.         /// <summary>  
  8.         /// Replace all Textbox by MyTextbox (NO SETUP NEEDED)  
  9.         /// </summary>  
  10.         private MyTextBox textBox1;  
  11.         public Form1()  
  12.         {  
  13.             InitializeComponent();  
  14.   
  15.             // Just create a Textbox  
  16.             textBox1 = new MyTextBox  
  17.             {  
  18.                 Location = new Point(10, 10),  
  19.                 Size = new Size(300, 20)  
  20.             };  
  21.   
  22.             Controls.Add(textBox1);  
  23.   
  24.             // Sample text  
  25.             textBox1.Text = "You can clear this field with 'x' button on it.";  
  26.   
  27.         }  
  28.     }  
  29. }  
How it works:
 
 

Using

 
Now you need to replace your current code 
 
(THIS IS AT YOUR OWN RISK, BE CAREFULLY, TRY-FIRST ON ONLY ONE FORM)
 
 
 

Conclusion

 
This is a simple implementation but made a big difference for end-users who use mouse or touch screens.
 
Please remember to like this post and follow me.
 
Happy coding.