This is a migrated thread and some comments may be shown as answers.

How to select text on got focus event

3 Answers 289 Views
AutoCompleteBox
This is a migrated thread and some comments may be shown as answers.
Konrad Sikorski
Top achievements
Rank 1
Konrad Sikorski asked on 30 Aug 2013, 10:18 AM
Hi,

I have RadAutoCompleteBox with SelectionMode set to Single. Now I would like to select all text in this control when it gets a focus. There is no property or method that allow me to select text in this control. Any suggestions? Thx.

I am using version: 2012.3.1129.1050

3 Answers, 1 is accepted

Sort by
0
Kalin
Telerik team
answered on 03 Sep 2013, 02:44 PM
Hi Konrad,

I would suggest that you use the GotFocus event of the AutoCompleteBox and in the handler you can easily select the whole text in the input field. Please see the code snippet below which demonstrates the exact approach:

private void RadAutoCompleteBox_GotFocus(object sender, RoutedEventArgs e)
{
    (e.OriginalSource as TextBox).SelectAll();
}

Hope this helps.

Regards,
Kalin
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for SILVERLIGHT.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
0
Konrad Sikorski
Top achievements
Rank 1
answered on 04 Sep 2013, 08:22 AM
Hi Kalin,

Thx it works but the control has one bug. When you pressing tab to navigate between controls in form the text in RadAtoCompleteBox is clearing. There are two scenarios:

Case 1:
1. Open new form where AutoCompleteBox is already set
2. press Tab, Tab to navigate to this control and next to out it
3. The text is cleared

Case 2:
1. Open new form where AutoCompleteBox is empty
2. press tab to navigate to this control
3. provide some valeua
4. press ctrl+A to select all text
5. press tab to navigate out this control
6. text is cleared

For both scenarios the GotFocus event is not implemented. When you implement gotFocus event like you did than all entrances to this control will clear its value. Do you have workaround for this?

code example:
<Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="139*"/>
        </Grid.RowDefinitions>
        <TextBox Text="Text1" GotFocus="UIElement_OnGotFocus"/>
        <telerik:RadAutoCompleteBox Grid.Row="1" SearchText="AutoText2" x:Name="ucAutoText" SelectionMode="Single" SelectionChanged="UcAutoText_OnSelectionChanged" SearchTextChanged="UcAutoText_OnSearchTextChanged" GotFocus="UIElement_OnGotFocus"/>
        <TextBox Grid.Row="2" Text="Text3" GotFocus="UIElement_OnGotFocus"/>
         
        <TextBox x:Name="ucEvents" Grid.Row="3" AcceptsReturn="True" IsTabStop="False" IsReadOnly="True"/>
    </Grid>

public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
 
            ucAutoText.ItemsSource = new[] { "AutoText1", "AutoText2", "AutoText3" };
        }
 
        private void UIElement_OnGotFocus(object sender, RoutedEventArgs e)
        {
            ((TextBox)e.OriginalSource).SelectAll();
        }
 
        private void UcAutoText_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (ucEvents == null) return;
            ucEvents.Text = DateTime.Now + ": UcAutoText_OnSelectionChanged" + Environment.NewLine + ucEvents.Text;
        }
 
        private void UcAutoText_OnSearchTextChanged(object sender, EventArgs e)
        {
            if (ucEvents == null) return;
            ucEvents.Text = DateTime.Now + ": UcAutoText_OnSearchTextChanged" + Environment.NewLine + ucEvents.Text;
        }
    }
0
Kalin
Telerik team
answered on 04 Sep 2013, 10:48 AM
Hello Konrad,

I tested the explained scenario with the 2012.3.1129.1050 version of the controls and observed the described behavior. However using the latest version of the controls this issue is not present as we have improved the AutoCompleteBox implementation recently. So my suggestion is to use the latest release version of RadControls if possible.

If you have any other questions let us know.

Regards,
Kalin
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for SILVERLIGHT.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
Tags
AutoCompleteBox
Asked by
Konrad Sikorski
Top achievements
Rank 1
Answers by
Kalin
Telerik team
Konrad Sikorski
Top achievements
Rank 1
Share this question
or