question

Julien-2587 avatar image
0 Votes"
Julien-2587 asked Julien-2587 commented

LimitNumber of attempts on Entry

Hello, I have an Entry which accommodates a riddle, I can get a right or wrong answer but I want to limit to 3 attempts.
Except that my code currently only counts one attempt even if I type several answers.

My xaml:

 <Entry x:Name="Enigme01"
                    IsVisible="false"
                    Placeholder="Ecrire la réponse."
                    ClearButtonVisibility="WhileEditing"
                    IsTextPredictionEnabled="False"
                    ReturnType="Send"
                    Completed="Entry_Completed"
                    Grid.ColumnSpan="2"
                    Grid.Row="8"/>

My xaml.cs :

 public void Entry_Completed (object sender, EventArgs e)
         {
             var reponseEnigme01 = Enigme01.Text;
             int tentative = 0;
             int tentativeMax = 3;
             string soluce1 = "chaussette";
    
             if ((soluce1 != reponseEnigme01) & (tentative < tentativeMax))
             {
                 DisplayAlert("Info:", "Mauvaise réponse", "ok");
                 tentative ++;
                 Reponse04.Text = "Essayez encore ";
             }
             else if (tentative >= tentativeMax)
             {
                 Reponse04.TextColor = Color.DarkRed;
                 Reponse04.Text = "A force de cogiter pour trouver la réponse, vous ne voyez pas les gardes arriver. Ils vous attrapent et finisser ainsi votre carrière d'aventurier dans une geôle.";
             }
             else
             { 
                 reponseEnigme01 = soluce1;
                 Reponse04.Text = "Bravo, vous avez donné la bonne réponse et la porte s'ouvre.";
                 Reponse04.IsVisible = true;
                 Enigme01.IsEnabled = false;
             }
         }
dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

KyleWang-MSFT avatar image
1 Vote"
KyleWang-MSFT answered Julien-2587 commented

Hi Julien-2587,

Welcome to our Microsoft Q&A platform!

I noticed that you set IsVisible="false". Should it be IsVisible="true"? Otherwise Enigme01 will not be displayed on the screen.

only counts one attempt even if I type several answers

The cause for the problem is that every time you execute Entry_Completed, the variable tentative inside will be reinitialized to 0.

So declaring the variable tentative as a global field will handle this issue.

 // decalre tentative outside
 int tentative = 0;
    
 public void Entry_Completed(object sender, EventArgs e)
 {
     var reponseEnigme01 = Enigme01.Text;
     int tentativeMax = 3;
     string soluce1 = "chaussette";
    
     if ((soluce1 != reponseEnigme01) & (tentative < tentativeMax))
     {
         DisplayAlert("Info:", "Mauvaise réponse", "ok");
         tentative++;
         Reponse04.Text = "Essayez encore ";
     }
     else if (tentative >= tentativeMax)
     {
         Reponse04.TextColor = Color.DarkRed;
         Reponse04.Text = "A force de cogiter pour trouver la réponse, vous ne voyez pas les gardes arriver. Ils vous attrapent et finisser ainsi votre carrière d'aventurier dans une geôle.";
     }
     else
     {
         reponseEnigme01 = soluce1;
         Reponse04.Text = "Bravo, vous avez donné la bonne réponse et la porte s'ouvre.";
         Reponse04.IsVisible = true;
         Enigme01.IsEnabled = false;
     }
 }

Regards,
Kyle


If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you very much, the solution was indeed very simple. For the displays, this is normal, I am working on a small role play with actions / reactions which vary according to the choices.

0 Votes 0 ·