Text selection problem

4 posts, 1 answers
  1. Alain Beaulieu
    Alain Beaulieu avatar
    2 posts
    Member since:
    Oct 2009

    Posted 30 Mar 2011 Link to this post


    I have a combobox filled with data such as this (GL Accounts):

    10000 - GL Account Name Here
    11000 - GL Account Name Here
    12000 - GL Account Name Here
    13000 - GL Account Name Here
    14000 - GL Account Name Here
    15000 - GL Account Name Here
    20000 - GL Account Name Here

    The combobox has a fixed width that is smaller than the text above, say:

    [                            ]

    When the combobox gets the focus it auto-selects the text in the textbox, as it should and this is great. But it does it by starting the selection on the first character and ending on the last character, which ends up like this:

    [ccount Name Here]

    If it started the selection from the last character to the first one, then I would get this:

    [10000 - GL Accou]

    For me, the important part is the account ID, and it should always be visible in the textbox whether or not the combobox has the focus (unless the user is typing of course).

    I have a feeling that this is not possible at this time with this control, but is it a feature that I can request for an eventual update to the Silverlight Controls pack?


  2. Valeri Hristov
    Valeri Hristov avatar
    2252 posts

    Posted 30 Mar 2011 Link to this post

    Hello Alain,

    You could prevent the ComboBox from scrolling to the right by setting the SelectAllTextEvent property to None:

    All the best,
    Valeri Hristov
    the Telerik team
  3. DevCraft R3 2016 release webinar banner
  4. Answer
    Justin avatar
    6 posts
    Member since:
    Jul 2010

    Posted 13 May 2011 Link to this post

    I've worked on this problem Alain and here is the fix. The problem isn't with the RadComboBox itself, it's the TextBox control inside its template that's causing the undesirable behaviour. Unfortunately the text selection functionality in the TextBox has a SelectAll function which selects text from Left to Right forcing the cursor to the end of the text and cannot be changed or overridden. If you generate a Style of the RadComboBox with Blend you will notice a control called PickerTextBox named PART_EditableTextBox, this essentially inherits TextBox. All you need to do is inherit the RadComboBox and overide the OnLostFocus handler to place the cursor back to the start when the focus is lost.
    Here is the Code for the custom control. The project I created it for was in VB so just use a C# code converter if need in c#.

    Imports Telerik.Windows.Controls
    Public Class ctwRadComboBox
        Inherits RadComboBox
        Private PART_EditableTextBox As PickerTextBox
        Public Sub New()
            Me.DefaultStyleKey = GetType(ctwRadComboBox)
        End Sub
        Public Overrides Sub OnApplyTemplate()
            PART_EditableTextBox = DirectCast(GetTemplateChild("PART_EditableTextBox"), PickerTextBox)
            PART_EditableTextBox.Select(0, 0)
        End Sub
        Protected Overrides Sub OnLostFocus(e As RoutedEventArgs)
            PART_EditableTextBox.Select(0, 0)
        End Sub
    End Class

    Be sure to add a copy of the RadComboBox default style to your Generic.xaml file containing your default style of the new Control and replace all RadComboBox TargetTypes by ctwRadComBox or any class name of your choosing. Or you can simply remove DefaultStyleKey in the Constructor and let RadComboBox draw the control. Hope this helps.

    Mark me as answered if it works :).
  5. laxman
    laxman avatar
    9 posts
    Member since:
    Jan 2011

    Posted 22 Sep 2011 Link to this post

    It didn't worked...I want to show the text selected once the drop down gets close.
Back to Top