Friday, June 02, 2006

SelectALL in Compact Framework

When you want to select all the text in a textbox, you are more likely to use SelectAll function of the textbox in the GotFocus event. Ironically, once this event is fired the text is unselected. The work around is to use a timer and in timer event call SelectAll function.

private void textBox1_GotFocus(object sender, EventArgs e)
{
//Use a timer to select all
System.Windows.Forms.Timer t = new System.Windows.Forms.Timer();
t.Interval = 10;
t.Tick += new EventHandler(t_Tick);
t.Enabled = true;
}
}
///
/// Event to select full text in textbox
///
///
///
private void t_Tick(object sender, EventArgs e)
{
((System.Windows.Forms.Timer)sender).Enabled = false; // Stop the timer
// Select all the text
textBox1.SelectAll();
}