Hi everbody,
I just made a shift from JAVA to .NET and I wonder how to make a custom (extended) text box, which will simply be used in a form. I will be using several of these extended textboxes and each should have the same event handler capabilities without explicitly coding event handler methods for each of them.
Actually I just want to change the backcolor of the text boxes when i click on them, and re-change the color to its default when i focus on some other component.
Is there a way to implement this by just extending System.Windows.Forms.TextBox class and adding some capabilities to this extended class.
Thanks in advance.
Ahmet
Scott LyslePosted Sep 3, 2007, 12:27 AM
Ahmet:
Starting with the addition of a standard class, here is an example of what you asked about (I used leave and enter events rather than click since this will work whether the user clicks in the box or tabs into the box):
using System; using System.Collections.Generic; using System.Text; using System.Windows.Forms; using System.Drawing; namespace TextControl { // create a public class that inherits from textbox public class ExtText : TextBox { // define a default constructor that calls // initialize component public ExtText() { InitializeComponent(); } // add event handlers for the events you want to // manage private void InitializeComponent() { this.SuspendLayout(); // ExtText this.Enter += new System.EventHandler(this.ExtText_Enter); this.Leave += new System.EventHandler(this.ExtText_Leave); this.ResumeLayout(false); } // write the handlers for the control // on leave, turn the control back color to white private void ExtText_Leave(object sender, EventArgs e) { this.BackColor = Color.White; } // on enter, turn the control back color to azure private void ExtText_Enter(object sender, EventArgs e) { this.BackColor = Color.Azure; } } }