7451

Silverlight Programmatically select text

Question:

I am playing with a Silverlight control that has a TextBox and Button. When I click the button (which calls SelectText below), I want to select all the text in the textbox.

Here's my code:

private void SelectText() { TextBox tb = this.txtFirstName; tb.SelectionStart = 0; tb.SelectionLength = 3; // tb.Select(0, this.txtFirstName.Text.Trim().Length - 1); // tb.SelectAll(); // tb.Text = String.Empty; }

The commented code is what I tried already, but neither is working.

Anyone have suggestions on what I'm doing wrong?

Answer1:

You might need to give focus to the textbox to see the actual selection happening (either before or after selecting, it might not matter but you'll have to try):

private void SelectText() { TextBox tb = this.txtFirstName; tb.SelectionStart = 0; tb.SelectionLength = 3; // tb.Select(0, this.txtFirstName.Text.Trim().Length - 1); // tb.SelectAll(); // tb.Text = String.Empty; tb.Focus(); }

Answer2:

SelectAll() works -- I've used it more than once. Is something manipulating the text box after this method happens?



Answer3:

This does the trick for the entire User Control.

public partial class MainPage : UserControl
{
    public MainPage()
    {
        InitializeComponent();
    }
    protected override void OnGotFocus(RoutedEventArgs e)
    {
        TextBox tb = e.OriginalSource as TextBox;
        if (tb != null)
            tb.SelectAll();
        base.OnGotFocus(e);
    }
}



Answer4:

The focus solution worked for me, but sometimes I get this ExecutionEngineException during the SelectAll() and it's totally bogus! Even a try...catch won't stop it and there's no more details about the error.



Recommend

  • Making Quotation(' or \") a String Possible?
  • Mozilla firefox problem in javascript
  • GetCharIndexFromPosition() for the last char
  • How to remove selected text from an input Text?
  • How to know which character(s) is(are) get deleted on backspace or delete
  • Silverlight Programmatically select text
  • How to pass data from a node js file to an html file
  • Implementing search&highlight - how can I avoid delays when highlight is slow?
  • check marking all the checkbox using jquery on click of a button
  • Removing child elements in XML using python
  • psycopg2.ProgrammingError: syntax error at or near “stdin” error when trying to copy_from redshift
  • dc.js / crossfilter dimensions (year / week)
  • C# “cannot assign field because it is a foreach iteration variable”
  • getting domain details and portrait_id and landscape_id for openX
  • Excel 2007: Format of email address from Outlook 2007
  • Updating and removing unique join relationships in CakePHP
  • Negating Regex PO BOX
  • d3.js selection conditional rendering
  • Defining enums in Cython code that will be used in the C part of code
  • Jackson Parser: ignore deserializing for type mismatch
  • How to use remove-erase idiom for removing empty vectors in a vector?
  • dc-js disable selecting slices on click for pie chart
  • Repeat a vertical line on every page in Report Builder / SSRS
  • Why is an OPTIONS request sent to the server?
  • MySQL WHERE-condition in procedure ignored
  • How to set my toolbar fixed while scrolling android
  • ORA-29908: missing primary invocation for ancillary operator
  • Web-crawler for facebook in python
  • How to get next/previous record number?
  • AT Commands to Send SMS not working in Windows 8.1
  • retrieve vertices with no linked edge in arangodb
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • trying to dynamically update Highchart column chart but series undefined
  • How do you join a server to an Active Directory (domain)?
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Change div Background jquery
  • How does Linux kernel interrupt the application?
  • apache spark aggregate function using min value
  • Sorting a 2D array using the second column C++
  • java string with new operator and a literal