Ticket Q427181
Visible to All Users

TextEdit ignoring SelectionStart/Length

created 13 years ago

My program is generating a default string for a textbox and selects a part of it which is likely to be modified by the user.

After the Text of a TextEdit is changed (programatically), the next time it gets the focus it will automatically select all the text in the editor, despite of what SelectionStart and SelectionLength have been set to.

Here's a sample to demonstrate it:

private void labelControl1_Click(object sender, EventArgs e)
    {
// set and then change the text
this.textEdit1.Text = "";
      this.textEdit1.Text = "FooBar";
      this.textEdit1.SelectionStart = 3;
this.textEdit1.SelectionLength = 3;
      this.textEdit1.Focus();
// without further action, the whole "FooBar" becomes selected

this.textEdit1.SelectionStart = 3;
      this.textEdit1.SelectionLength = 3;
      this.textEdit1.Focus();
// now only "Bar" is selected
    }

Answers

created 13 years ago (modified 13 years ago)

Hi,
Thank you for contacting us.
I suggest you use the following order of the mentioned code lines to implement this functionality:
textEdit1.Text = "FooBar";
textEdit1.Focus();
textEdit1.SelectionStart = 3;
textEdit1.SelectionLength = 3;
We look forward to your feedback once you’ve had the opportunity to try the suggested solution.

    Comments (3)

      Hi Oleg, thanks for your Response.
      The code snippet I posted was just to demonstrate the problem.
      In my real application a Grid's FocusedRowChanged event handler generates some text, puts it into textEdit1.Text and sets the SelectionStart and SelectionLength. While the user is still moving/editing in the grid I don't want to move the focus out of it, which would start whole chain of events (e.g. data validation).
      Only when the user hits the Tab key, clicks on the TextEdit's label or use the label's mnemonic key the focus moves into the TextEdit. And that's when the selection is automatically reset to cover the whole text.
      I created a workaround which looks like this:
          private void textEdit1_Enter(object sender, EventArgs e)
          {
            this.BeginInvoke((Action)(() => {
              this.textEdit1.SelectionStart = this.textEdit1.Text.Length - 2;
              this.textEdit1.SelectionLength = 2;
            }));
          }
      The caveats are:

      1. I have to set the selction both in the Grid's event so the user can see the selection before he enters the TextEdit (using textEdit1.Properties.HideSelection=false) and again in the textEdit1_Enter event. In this example the code is trivial, but in a real-world scenario it would be necessary to store the selection start/length somewhere and then restore it to those values.
      2. There's a short visible flicker when first the whole text gets selected and then only the desired part
      3. This must be done for each TextEdit individually.
        Generally the default behavior to select everything in the TextEdit when it gains focus after a Text change is fine. However when I programatically set the Selection after I change the text this auto-magic should not happen.
        My guess is that the setter of TextEdit.Text (or TextEdit.EditValue) sets an internal flag when the text was changed and when it gets the focus the next time, it selects everything. And IMHO the setters of SelectionStart and SelectionLength should reset any such internal flag.
      DevExpress Support Team 13 years ago

        We need additional time to find a solution for you. Please bear with us. We will answer you ASAP.

        DevExpress Support Team 13 years ago

          Hi,
          Thank you for your feedback.
          You can simply implement the mentioned functionality in the corresponding TextEdit descendant. In this case, there is no need to write this code for all editors in your application.
          In the attachment you can find a simple sample that illustrates this approach in action.
          As for re-designing the implementation of the SelectionStart and SelectionLength properties, we are not ready to override it. I believe that this scenario is specific, and this functionality can be used only in rare cases.
          This behavior is similar to the .NET TextBox editor.

          Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

          Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.