sfNumericUpDown.Focus() doesn't seems to work

Im trying to set the focus on a sfNumericUpDown control, but the .Focus() method doesn't do anything

Is there a workaround?

3 Replies

DV Divya Venkatesan Syncfusion Team March 19, 2020 12:05 PM UTC

Hi Santiago, 
 
Greetings from Syncfusion. 
 
In iOS platform, SfNumericUpDown is just a UIView and hence calling Focus() will not work for SfNumericUpDown. So, we have achieved your requirement by getting the UITextField in the SfNumericUpDown and setting focus to it using DependencyService as shown in the below code snippets. 
 
Code snippets [INumericUpDownFocusService.cs]: 
 
public interface INumericUpDownFocusService 
{ 
    void Focus(SfNumericUpDown formsNumericUpDown); 
} 
 
Code snippets [NumericUpDownFocusService.cs]: 
 
[assembly: Dependency(typeof(Numericupdown.iOS.NumericUpDownFocusService))] 
namespace Numericupdown.iOS 
{ 
    public class NumericUpDownFocusService : INumericUpDownFocusService 
    { 
        public void Focus(SfNumericUpDown formsNumericUpDown) 
        { 
            var renderer = Platform.GetRenderer(formsNumericUpDown); 
 
            if (renderer is SfNumericUpDownRenderer) 
            { 
                Native.SfNumericUpDown nativeNumericUpDown = (renderer as SfNumericUpDownRenderer).Control as Native.SfNumericUpDown; 
 
                foreach (var subView in nativeNumericUpDown.Subviews) 
                { 
                    if (subView is UITextField SubviewTextField) 
                    { 
                        SubviewTextField.BecomeFirstResponder(); 
                    } 
                } 
            } 
        } 
    } 
} 
 
Code snippets [MainPage.xaml.cs]: 
 
private void Button_Clicked(object sender, EventArgs e) 
{ 
    if (Device.RuntimePlatform == Device.iOS) 
    { 
        DependencyService.Get<INumericUpDownFocusService>().Focus(upDown); 
    } 
    else 
    { 
        upDown.Focus(); 
    } 
} 
 
 
 
Please get back to us if you need any further assistance.   
 
Regards,  
Divya Venkatesan 



SA Santiago March 19, 2020 03:21 PM UTC

Hi, thanks for the reply. I'm actually having the issue on Android. Here is a sample project

I want to open the keyboard on the NumericUpDown when the Expanded event fires up

Attachment: SfNumericUpDownIssueSample_93611014.zip


DV Divya Venkatesan Syncfusion Team March 20, 2020 09:09 AM UTC

Hi Santiago, 
 
The Focus() will work only when it is called by the view which is already displayed in UI. Since you are calling the Focus() in Expanded event of SfAccordion, when the SfNumericUpDown is not yet displayed in screen, it is not working. Hence, we suggest you to calling the Focus() once the SfNumericUpDown is displayed as shown in the below code snippets. 
 
Code snippets [C#]: 
private async void SfAccordion_Expanded(object sender, ExpandedAndCollapsedEventArgs e) 
{ 
    SfAccordion sfAccordion = (SfAccordion)sender; 
 
    AccordionItem accordionItem = sfAccordion.Items.Where(item => item.IsExpanded).First(); 
 
    SfNumericUpDown sfNumericUpDown = accordionItem.FindByName<SfNumericUpDown>("numericUpDown"); 
 
    sfNumericUpDown.Value = 10; 
 
    await Task.Delay((int)sfAccordion.AnimationDuration); 
 
    sfNumericUpDown.Focus(); 
} 
 
 
Please get back to us if you need any further assistance. 
 
Regards, 
Divya Venkatesan 


Loader.
Up arrow icon