Home > Mobile >  How to focus on one Entry when Using TextChange for multiple entries
How to focus on one Entry when Using TextChange for multiple entries

Time:09-30

I have 2 entries. When I tap anything on entry 1 I would like to get "Yess" on entry 2 and when I type anything on entry 2 I would like to get "Noo"

The problem:

When I tap on entry 1, entry 2 change and get the value "Noo" but entry 1 change too and get the value "yess".

Question :

How can make entry 2 change when tapping on entry 1 without changing entry 1. And the same for entry 2

Here is Xaml code :

 <Entry ClassId="1" x:Name="myWord1"TextChanged="OnEntryTextChange"/>
 <Entry ClassId="2"  x:Name="myWord2"  TextChanged="OnEntryTextChange"/>

Code :

 private async void OnEntryTextChange(object sender, TextChangedEventArgs e)
    {
        
            var EntryTapped = (Xamarin.Forms.Entry)sender;

            Device.BeginInvokeOnMainThread(() => {


                        if (EntryTapped.ClassId == "1")
                        {
                   
                           myWord2.Text="Noo";
                                                         
                        }
                     
                        
                        else if (EntryTapped.ClassId == "2")
                        {
                          
                        myWord1.Text="yess";                           
                        }

            });
    }

Thanks for your help

CodePudding user response:

There are several ways of doing this:

  1. Using bindings

In this case you would have 2 private variables and 2 public variables, and the entries binded to each one. Check this link how to implement INotifyPropertyChanged

    private string entry1String;
    private string entry2String;
    public string Entry1String { 
        get => entry1String;
        set
        {
            entry2String = "Noo";
            entry1String = value;
            OnPropertyChanged(Entry1String);
            OnPropertyChanged(Entry2String);
        }
    }
    public string Entry2String { 
        get => entry2String;
        set
        {
            entry1String = "Yees";
            entry2String = value;
            OnPropertyChanged(Entry1String);
            OnPropertyChanged(Entry2String);
        }
    }

Another way could be using a variable as a Semaphore. While the variable is True, the method cannot be fired at the same time by another.

private bool semaphoreFlag=false;


private async void OnEntryTextChange(object sender, TextChangedEventArgs e)
{
    if(semaphoreFlag) return;
    semaphoreFlag=true;
        var EntryTapped = (Xamarin.Forms.Entry)sender;

        Device.BeginInvokeOnMainThread(() => {


                    if (EntryTapped.ClassId == "1")
                    {
               
                       myWord2.Text="Noo";
                                                     
                    }
                 
                    
                    else if (EntryTapped.ClassId == "2")
                    {
                      
                    myWord1.Text="yess";                           
                    }

        });
semaphoreFlag=false;
}

CodePudding user response:

You could use the Focused event instead of TextChanged event.

   <StackLayout>
        <Entry ClassId="1" x:Name="myWord1"  Focused="EntryFocused"/>
        <Entry ClassId="2"  x:Name="myWord2"  Focused="EntryFocused"/>
    </StackLayout>



   private void EntryFocused(object sender, FocusEventArgs e)
    {
        var EntryTapped = (Xamarin.Forms.Entry)sender;

        if (EntryTapped.ClassId == "1")
        {

            myWord2.Text = "Noo";

        }

        else if (EntryTapped.ClassId == "2")
        {

            myWord1.Text = "yess";
        }
    }
  • Related