Home > Software design >  How do I reset my Button count to 0 in xamarin forms
How do I reset my Button count to 0 in xamarin forms

Time:10-11

Good day all, I'm new to Xamarin.Forms, I have created a button to count the number of maximum clicked and How can I create another button to reset it to 0 after clicking?

Secondly I want all my buttons to be in Circle shape and Horizontally left and right. Thanks. This is the example of my page:

CountPage.xaml

<StackLayout Spacing="10">
                                    <Label Text="0" x:Name="labelCount" 
                                           HorizontalOptions="Center"
                                           VerticalOptions="CenterAndExpand" />
                                    <Button Text="Count" Clicked="Increment"/>
                                </StackLayout>

CountPage.xaml.cs

int labelValue = 0;
        public CountPage()
        {
            InitializeComponent();
        }

        private void Increment(object sender, EventArgs e)
        {
            labelValue  ;
            labelCount.Text = labelValue.ToString();
        }

CodePudding user response:

For the circle buttons, I recommend reading this doc

Now let's go to the coding part

First, let's add another button

<StackLayout Spacing="10">                                        
  <Label Text="0" x:Name="labelCount" ... />
  <Button Text="Count" Clicked="Increment"/>
  <Button Text="Reset counter" Clicked="Reset"/>
</StackLayout>

Now in the code behind you will have to add

private void Reset(object sender, EventArgs e)
{
   labelValue=0;
   labelCount.Text = labelValue.ToString(); //this will be 0
}

So it's just using the same variable and the name of the other button.

Happy coding!

  • Related