简体   繁体   中英

Clear the Entry.Text value when if perceive a certain condition

Is there a way that I can clear the entry.text when ever meet some condition? I guess my question is how do I capture the text changed of the entry in Xamarin over (sender, TextChangedEventArgs)?

private void EntryBoxBarCode_TextChanged(object sender, TextChangedEventArgs e)
{
    if (EntryBoxBarCode.Text != "")
    {
        var entry = new Entry();
        entry.Text = e.NewTextValue;
        WorkFormCheck(entry.Text);

        if (typeOfBarCode != "")
        {
            //Here is the condition where I want to clear the text
            entry.Text = "";
            EntryBoxBarCode.Focus();
        }
    }
    else
    {
        //pasing the right value of the entry, then focus to other Entry
        EntryPackCode.Focus();
    }         
}
 

Xaml:

<Entry Grid.Row="0" Grid.Column="1" x:Name="EntryBoxBarCode" WidthRequest="250" TextChanged="EntryBoxBarCode_TextChanged"/>

What I don't understand is why are you creating an entry on runtime on TextChanged? which will literally create entry after entry every time you type a text in the entry that calls this Textchanged event, When you are creating a new entry here it's not something that's on your UI, if you want an entry on your UI to trigger this you will have to give the triggering entry a name and then use that name to check what's in that entry and update accordingly or you could use the sender object.

Your XAML would be something like:

<Entry Grid.Row="0" Grid.Column="1" x:Name="EntryBoxBarCode" WidthRequest="250" TextChanged="EntryBoxBarCode_TextChanged"/>
<Entry Grid.Row="" Grid.Column="1" x:Name="EntryPackCode" WidthRequest="250" />


private void EntryBoxBarCode_TextChanged(object sender, TextChangedEventArgs e)
    {
        if (EntryBoxBarCode.Text != "")
        {
            WorkFormCheck(EntryBoxBarCode.Text);

            if (typeOfBarCode != "")
            {
                //Here is the condition where I want to clear the text
                EntryBoxBarCode.Text = "";
                //EntryBoxBarCode.Focus(); //not sure this is required or not since you should already have focus here.
            }
        }
        else
        {
            //passing the right value of the entry, then focus to other Entry
            EntryPackCode.Focus();
        }         
    }

Goodluck

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM