Textbox.selectall

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

What options do I have to simply select all text in a textbox when it
receives focus? I have seen the option of the timer, but I have 10 textboxes.

any suggestions please.
reagrds,
Bill
 
You can use a thread to select all the text. Try this..it's a hack but it
works. You can have all your textbox Focus events use
GenericTextBox_GotFocus to handle the text selection.

private TextBox currentTB = null;
private void GenericTextBox_GotFocus(object sender, System.EventArgs e)
{
this.currentTB = (TextBox)sender;
System.Threading.Thread t = new System.Threading.Thread(new
System.Threading.ThreadStart(SelectAll));
t.Start();
}
private void SelectAll()
{
if(this.currentTB!=null)
{
//Usuall the Invoke should finish after the got focus event finishs. If
you experience problems add a sleep
//System.Threading.Thread.Sleep(50);
currentTB.Invoke(new EventHandler(InvokeSelectAll));
}
}
private void InvokeSelectAll(object sender, System.EventArgs e)
{
((TextBox)sender).SelectAll();
}
 
Back
Top