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

Unable to programmatically focus

2 Answers 244 Views
NumericUpDown
This is a migrated thread and some comments may be shown as answers.
ITC
Top achievements
Rank 1
ITC asked on 13 Nov 2017, 12:55 PM

Hi,
I'm using 2017.3.913.45 and I've been trying to focus a NumericUpDown-Control when the user changes the current Tab of a RadTabControl.

So I've got a RadTabControl:

<telerik:RadTabControl ... SelectionChanged="TabControl_SelectionChanged" ...>
    ..
    <telerik:RadTabItem Name="MyTab">
        ..

        <telerik:RadNumericUpDown Name="MyNumericUpDown" ..></telerik:RadNumericUpDown>
         ..

    </telerik:RadTabItem>
    ..
</telerik:RadTabControl>

 

And a TabControl_SelectionChanged Method:

private void TabControl_SelectionChanged(object sender, RadSelectionChangedEventArgs e)
{
    try
    {
        if(e.AddedItems[0] == MyTab)
        {
            MyNumericUpDown.Focus();
            Keyboard.Focus(MyNumericUpDown);
        }
    }
    catch
    {
        System.Diagnostics.Debugger.Break();
    }
}

 

I initially only tried with MyNumericUpDown.Focus() which did not work, a colleague asked me to add the Keyboard.Focus(MyNumericUpDown) call, but that also does not work.

What's the workflow here?

2 Answers, 1 is accepted

Sort by
0
Dinko | Tech Support Engineer
Telerik team
answered on 16 Nov 2017, 09:39 AM
Hi Rolf,

Thank you for your interest in RadNumericUpDown control.

Let me first elaborate on the default focus behavior of the RadTabItem content. Internally in code, we are setting focus to the first element placed in the content of the RadTabItem. Currently, this logic cannot be overridden. 

As a workaround, you can use a dispatcher to focus the desired control. You also need to set the DispatcherPriority to ApplicationIdle. Check the following code snippet.
this.Dispatcher.BeginInvoke(new Action(() =>
{
    MyNumericUpDown.Focus();
 
}),DispatcherPriority.ApplicationIdle);

Regards,
Dinko
Progress Telerik
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which you to write beautiful native mobile apps using a single shared C# codebase.
0
ITC
Top achievements
Rank 1
answered on 16 Nov 2017, 09:52 AM

Hi Dinko,

thanks for the reply, I replaced the code and it seems to work. However, strangely the two lines I had there first worked the last two days without me even realizing it. Seems arbitrary.

Thanks!

Tags
NumericUpDown
Asked by
ITC
Top achievements
Rank 1
Answers by
Dinko | Tech Support Engineer
Telerik team
ITC
Top achievements
Rank 1
Share this question
or