How to set focus on a next entry control dynamicaaly???

AbhayJadounAbhayJadoun Member ✭✭
edited July 10 in Xamarin.Forms

In my case, I have a dynamically created entry in a grid. I want to set focus on the next entry. Can someone help me out on this?

`if ((donationTypeList != null) && (donationTypeList.Count() > 0))
{
foreach (var item in donationTypeList)
{
var frame = new Frame();

                var stacklayout = new StackLayout() { Orientation = StackOrientation.Vertical };
                var grid = new Grid();

                var label = new Label() { Text = item.name, ClassId = "1" };
                var xentry = new XEntry() { VerticalOptions = LayoutOptions.End, PlaceholderColor = Color.Black, Placeholder = Constants.ZeroDollar, FontSize = 14, HeightRequest = 50, TextColor = Color.Black };
                xEntries.Add(xentry);
                grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
                grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
                grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });

                grid.Children.Add(label, 0, 0);
                grid.Children.Add(xentry, 1, 0);

                stacklayout.Children.Add(grid);
                frame.Content = stacklayout;

                customizedFlexlayout.Children.Add(frame);

                if (item.amount != null && Convert.ToDouble(item.amount) != 0)
                {
                    xentry.Text = Constants.Dollar + item.amount;
                    frame.BackgroundColor = Color.FromHex("#0383BE");
                    label.TextColor = Color.White;
                }

                xentry.Focused += (s, e) =>
                {
                    frame.BackgroundColor = Color.FromHex("#054a90");
                    label.TextColor = Color.White;
                };

                xentry.TextChanged += (s, e) =>
                {
                    string text = xentry.Text;
                    if (text.Contains('.'))
                    {
                        int count = text.Length - text.IndexOf(".") - 1;
                        if (count > 2)
                            xentry.Text = text.Substring(0, text.Length - 1);
                    }
                };

                xentry.Unfocused += (s, e) =>
                {
                    donationCatButtons.IsEnabled = false;
                    if (!string.IsNullOrEmpty(xentry.Text))
                    {
                        if ((xentry.Text == Constants.ZeroDollar) || (Convert.ToDouble(xentry.Text.Trim('$')) == 0))
                        {
                            frame.BackgroundColor = Color.FromHex("#EEEFEF");
                            label.TextColor = Color.FromHex("#054A90");
                            item.amount = 0.ToString();
                            xentry.Text = "";
                        }
                        else
                        {
                            frame.BackgroundColor = Color.FromHex("#0383BE");
                            label.TextColor = Color.White;
                            if (item.fundsMaxLimit != "" && Convert.ToDouble(item.fundsMaxLimit) > 0 && Convert.ToDouble(xentry.Text.Trim('$')) > Convert.ToDouble(item.fundsMaxLimit))
                            {
                                DisplayAlert(Constants.MaxLimitTitle, "You can not donate more than $" + item.fundsMaxLimit + " for " + item.name + ".", Constants.Ok);
                                item.amount = string.Format("{0:F2}", Convert.ToDouble(xentry.Text.Trim('$').TrimStart(new Char[] { '0' })));
                            }
                            else
                                item.amount = string.Format("{0:F2}", Convert.ToDouble(xentry.Text.Trim('$').TrimStart(new Char[] { '0' })));
                            xentry.Text = Constants.Dollar + item.amount;
                        }
                    }
                    else
                    {
                        xentry.Text = Constants.ZeroDollar;
                        item.amount = 0.ToString();
                        frame.BackgroundColor = Color.FromHex("#EEEFEF");
                        label.TextColor = Color.FromHex("#054A90");
                    }
                    foreach (var designation in donationTypeList)
                    {
                        if (designation.amount != 0.ToString() && designation.amount != null && designation.amount != "")
                            donationCatButtons.IsEnabled = true;
                    }
                    foreach (var designation in donationTypeList)
                    {
                        if (designation.fundsMaxLimit != "" && Convert.ToDouble(designation.fundsMaxLimit) > 0 &&
                            Convert.ToDouble(designation.amount) > Convert.ToDouble(designation.fundsMaxLimit))
                            donationCatButtons.IsEnabled = false;
                    }
                };
            }              
        }
        else
            DisplayNoContentFrame(Constants.NoSearchResultMessage);
    }`
Tagged:

Answers

  • JohnHardmanJohnHardman GBUniversity mod
    edited July 10

    @AbhayJadoun

    How soon do you need it? @JoeManke has recently done a pull request to put the functionality into Xamarin.Forms itself. See https://forums.xamarin.com/discussion/comment/380454/#Comment_380454 . Hopefully, that will make it into the next XF release, but you never know for sure.

    If you need the functionality before then, you should use the Completed handler of each Entry to move the Focus to the next Entry.

  • AbhayJadounAbhayJadoun Member ✭✭

    @JohnHardman we need this in my current implementation. But unable to do it dynamically from cs file.
    In my case entry is also created dynamically.

  • JohnHardmanJohnHardman GBUniversity mod

    @AbhayJadoun said:
    But unable to do it dynamically from cs file. In my case entry is also created dynamically.

    It is possible to do it in your .cs file.

    Depending on how you build your page, you can either:

    (1) build a collection of references to Entry views as you create them, then once they have all been added iterate through the collection adding Completed handlers where each Completed handler simply moves the Focus to the next Entry.

    or

    (2) have a variable that contains a reference to the last Entry added (or null if none have yet been added). When a new Entry is added to the page's UI (after any existing Entry), grab the reference from the variable. If it is non-null, add a Completed handler that simply moves the focus to the newly created Entry. Then update the variable with a reference to the newly created Entry.

Sign In or Register to comment.