i m using vs 2005 framework 2.0
i have simple c# application
in my form i have 5 textboxes
what i want to do is to change the color of the textbox when it is clicked
textBox1.BackColor = Color.LightGoldenrodYellow;
i have done it through by writing the above code in click event of each textbox
now i want to write only one code which detects which textbox is clicked and appropriately changes the color of one clicked
regards
ahsan ashfaq
Loading
ahsan ashfaqPosted Nov 10, 2009, 6:17 AM
i have achieved what i required
thanx to all of u
special thanx to DANATAS
:)
regards,
Ahsan Ashfaq
Danatas GerviPosted Nov 10, 2009, 3:02 AM
Add to you form this code:
(Just change the colors to your preferenses)
namespace TextBoxClickYellow
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
foreach (Control mayBeTextbox in this.Controls)
{
if (mayBeTextbox is TextBox)
{
mayBeTextbox.MouseClick += new MouseEventHandler(mayBeTextbox_MouseClick);
mayBeTextbox.LostFocus += new EventHandler(mayBeTextbox_LostFocus);
}
}
}
void mayBeTextbox_LostFocus(object sender, EventArgs e)
{
((Control)sender).BackColor = Color.Gray;
}
void mayBeTextbox_MouseClick(object sender, MouseEventArgs e)
{
((Control)sender).BackColor = Color.Yellow;
}
}
}
Nilanka DharmadasaPosted Nov 10, 2009, 2:04 AM
You don;t have to write a lot of code for this. Instead you can write one method and bind this method to all the text boxes.
private void textBox_Click(object sender, EventArgs e)
{
((TextBox)sender).BackColor = Color.Blue;
}
this.textBox1.Click += new System.EventHandler(this.textBox_Click);
this.textBox2.Click += new System.EventHandler(this.textBox_Click);
this.textBox3.Click += new System.EventHandler(this.textBox_Click);
Hope this will help you. If it helps you please mark my answer as accepted.