Sum of 10 entries

Borisoprit 371 Reputation points
2021-02-03T20:35:33.31+00:00

Want to make a scoreboard of a game , and want also a total of all

I have about 10 entries and want to add then up to a total .

How do i make this work ?

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,301 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 69,386 Reputation points Microsoft Vendor
    2021-02-04T02:12:47.83+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    I have about 10 entries and want to add then up to a total .

    Do you want to achieve the result like following screenshot?

    63791-image.png

    Here is background code. If any of entry's value will be changed, the value of sum will be changed at the same time.

       using System.ComponentModel;  
       using System.Linq;  
       using System.Text;  
       using System.Threading.Tasks;  
       using Xamarin.Forms;  
         
       namespace SumOfEntrys  
       {  
           public partial class MainPage : ContentPage, INotifyPropertyChanged  
           {  
               int _entry1 = 0;  
               public int Entry1  
               {  
                   get  
                   {  
                       return _entry1;  
                   }  
         
                   set  
                   {  
                       if (_entry1 != value)  
                       {  
                           _entry1 = value;  
                           OnPropertyChanged("Entry1");  
                           TextChangedCommand.Execute(null);  
                       }  
                   }  
         
               }  
         
               int _entry2 = 0;  
               public int Entry2  
               {  
                   get  
                   {  
                       return _entry2;  
                   }  
         
                   set  
                   {  
                       if (_entry2 != value)  
                       {  
                           _entry2 = value;  
                           OnPropertyChanged("Entry2");  
                           TextChangedCommand.Execute(null);  
                       }  
                   }  
         
               }  
         
         
               int _entry3 = 0;  
               public int Entry3  
               {  
                   get  
                   {  
                       return _entry3;  
                   }  
         
                   set  
                   {  
                       if (_entry3 != value)  
                       {  
                           _entry3 = value;  
                           OnPropertyChanged("Entry3");  
                           TextChangedCommand.Execute(null);  
                       }  
                   }  
         
               }  
         
               int _entry4 = 0;  
               public int Entry4  
               {  
                   get  
                   {  
                       return _entry4;  
                   }  
         
                   set  
                   {  
                       if (_entry4 != value)  
                       {  
                           _entry4 = value;  
                           OnPropertyChanged("Entry4");  
                           TextChangedCommand.Execute(null);  
                       }  
                   }  
         
               }  
         
               int _entry5 = 0;  
               public int Entry5  
               {  
                   get  
                   {  
                       return _entry5;  
                   }  
         
                   set  
                   {  
                       if (_entry5 != value)  
                       {  
                           _entry5 = value;  
                           OnPropertyChanged("Entry5");  
                           TextChangedCommand.Execute(null);  
                       }  
                   }  
         
               }  
               int _entry6 = 0;  
               public int Entry6  
               {  
                   get  
                   {  
                       return _entry6;  
                   }  
         
                   set  
                   {  
                       if (_entry6 != value)  
                       {  
                           _entry6 = value;  
                           OnPropertyChanged("Entry6");  
                           TextChangedCommand.Execute(null);  
                       }  
                   }  
         
               }  
         
               int _entry7 = 0;  
               public int Entry7  
               {  
                   get  
                   {  
                       return _entry7;  
                   }  
         
                   set  
                   {  
                       if (_entry7 != value)  
                       {  
                           _entry7 = value;  
                           OnPropertyChanged("Entry7");  
                           TextChangedCommand.Execute(null);  
                       }  
                   }  
         
               }  
         
               int _entry8 = 0;  
               public int Entry8  
               {  
                   get  
                   {  
                       return _entry8;  
                   }  
         
                   set  
                   {  
                       if (_entry8 != value)  
                       {  
                           _entry8 = value;  
                           OnPropertyChanged("Entry8");  
                           TextChangedCommand.Execute(null);  
                       }  
                   }  
         
               }  
               int _entry9 = 0;  
               public int Entry9  
               {  
                   get  
                   {  
                       return _entry9;  
                   }  
         
                   set  
                   {  
                       if (_entry9 != value)  
                       {  
                           _entry9 = value;  
                           OnPropertyChanged("Entry9");  
                           TextChangedCommand.Execute(null);  
                       }  
                   }  
         
               }  
         
               int _entry10 = 0;  
               public int Entry10  
               {  
                   get  
                   {  
                       return _entry10;  
                   }  
         
                   set  
                   {  
                       if (_entry10 != value)  
                       {  
                           _entry10 = value;  
                           OnPropertyChanged("Entry10");  
                           TextChangedCommand.Execute(null);  
                       }  
                   }  
         
               }  
               int _sum = 0;  
               public int Sum  
               {  
                   get  
                   {  
                       return _sum;  
                   }  
         
                   set  
                   {  
                       if (_sum != value)  
                       {  
                           _sum = value;  
                           OnPropertyChanged("Sum");  
         
                       }  
                   }  
               }  
         
               int GetAllOfValue()  
               {  
                   return Entry3 + Entry2 + Entry1+ Entry4 + Entry5 + Entry6+ Entry7 + Entry8 + Entry9+ Entry10;  
               }  
         
               public Command TextChangedCommand => new Command(() => TextChanged());  
         
               private void TextChanged()  
               {  
                   Sum = GetAllOfValue();  
               }  
         
               public event PropertyChangedEventHandler PropertyChanged;  
         
               protected virtual void OnPropertyChanged(string propertyName)  
               {  
                   PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));  
               }  
               public MainPage()  
               {  
         
                    
                   BindingContext = this;  
                   InitializeComponent();  
               }  
           }  
       }  
    

    Here is layout code.

       <StackLayout>  
               <Entry Text="{Binding Entry1}" Keyboard="Numeric"></Entry>  
               <Entry Text="{Binding Entry2}" Keyboard="Numeric"></Entry>  
               <Entry Text="{Binding Entry3}" Keyboard="Numeric"></Entry>  
               <Entry Text="{Binding Entry4}" Keyboard="Numeric"></Entry>  
               <Entry Text="{Binding Entry5}" Keyboard="Numeric"></Entry>  
               <Entry Text="{Binding Entry6}" Keyboard="Numeric"></Entry>  
               <Entry Text="{Binding Entry7}" Keyboard="Numeric"></Entry>  
               <Entry Text="{Binding Entry8}" Keyboard="Numeric"></Entry>  
               <Entry Text="{Binding Entry9}" Keyboard="Numeric"></Entry>  
               <Entry Text="{Binding Entry10}" Keyboard="Numeric"></Entry>  
               <Label Text="Sum:" FontSize="Body"></Label>  
               <Label Text="{Binding Sum}" FontSize="Header" ></Label>  
           </StackLayout>  
    

    ==========================
    Update================================

    Agree with JCmanke, ContentPage already implements INotifyPropertyChanged via inheritance from BindableObject so there is no need to reimplement it here. Just delete those methods. please refer to following code.

       using System;  
       using System.Collections.Generic;  
       using System.ComponentModel;  
       using System.Linq;  
       using System.Text;  
       using System.Threading.Tasks;  
       using Xamarin.Forms;  
         
       namespace SumOfEntrys  
       {  
           public partial class MainPage : ContentPage  
           {  
               int _entry1 = 0;  
               public int Entry1  
               {  
                   get  
                   {  
                       return _entry1;  
                   }  
         
                   set  
                   {  
                       if (_entry1 != value)  
                       {  
                           _entry1 = value;  
                           OnPropertyChanged("Entry1");  
                           TextChangedCommand.Execute(null);  
                       }  
                   }  
         
               }  
         
               int _entry2 = 0;  
               public int Entry2  
               {  
                   get  
                   {  
                       return _entry2;  
                   }  
         
                   set  
                   {  
                       if (_entry2 != value)  
                       {  
                           _entry2 = value;  
                           OnPropertyChanged("Entry2");  
                           TextChangedCommand.Execute(null);  
                       }  
                   }  
         
               }  
         
         
               int _entry3 = 0;  
               public int Entry3  
               {  
                   get  
                   {  
                       return _entry3;  
                   }  
         
                   set  
                   {  
                       if (_entry3 != value)  
                       {  
                           _entry3 = value;  
                           OnPropertyChanged("Entry3");  
                           TextChangedCommand.Execute(null);  
                       }  
                   }  
         
               }  
         
               int _entry4 = 0;  
               public int Entry4  
               {  
                   get  
                   {  
                       return _entry4;  
                   }  
         
                   set  
                   {  
                       if (_entry4 != value)  
                       {  
                           _entry4 = value;  
                           OnPropertyChanged("Entry4");  
                           TextChangedCommand.Execute(null);  
                       }  
                   }  
         
               }  
         
               int _entry5 = 0;  
               public int Entry5  
               {  
                   get  
                   {  
                       return _entry5;  
                   }  
         
                   set  
                   {  
                       if (_entry5 != value)  
                       {  
                           _entry5 = value;  
                           OnPropertyChanged("Entry5");  
                           TextChangedCommand.Execute(null);  
                       }  
                   }  
         
               }  
               int _entry6 = 0;  
               public int Entry6  
               {  
                   get  
                   {  
                       return _entry6;  
                   }  
         
                   set  
                   {  
                       if (_entry6 != value)  
                       {  
                           _entry6 = value;  
                           OnPropertyChanged("Entry6");  
                           TextChangedCommand.Execute(null);  
                       }  
                   }  
         
               }  
         
               int _entry7 = 0;  
               public int Entry7  
               {  
                   get  
                   {  
                       return _entry7;  
                   }  
         
                   set  
                   {  
                       if (_entry7 != value)  
                       {  
                           _entry7 = value;  
                           OnPropertyChanged("Entry7");  
                           TextChangedCommand.Execute(null);  
                       }  
                   }  
         
               }  
         
               int _entry8 = 0;  
               public int Entry8  
               {  
                   get  
                   {  
                       return _entry8;  
                   }  
         
                   set  
                   {  
                       if (_entry8 != value)  
                       {  
                           _entry8 = value;  
                           OnPropertyChanged("Entry8");  
                           TextChangedCommand.Execute(null);  
                       }  
                   }  
         
               }  
               int _entry9 = 0;  
               public int Entry9  
               {  
                   get  
                   {  
                       return _entry9;  
                   }  
         
                   set  
                   {  
                       if (_entry9 != value)  
                       {  
                           _entry9 = value;  
                           OnPropertyChanged("Entry9");  
                           TextChangedCommand.Execute(null);  
                       }  
                   }  
         
               }  
         
               int _entry10 = 0;  
               public int Entry10  
               {  
                   get  
                   {  
                       return _entry10;  
                   }  
         
                   set  
                   {  
                       if (_entry10 != value)  
                       {  
                           _entry10 = value;  
                           OnPropertyChanged("Entry10");  
                           TextChangedCommand.Execute(null);  
                       }  
                   }  
         
               }  
               int _sum = 0;  
               public int Sum  
               {  
                   get  
                   {  
                       return _sum;  
                   }  
         
                   set  
                   {  
                       if (_sum != value)  
                       {  
                           _sum = value;  
                           OnPropertyChanged("Sum");  
         
                       }  
                   }  
               }  
         
               int GetAllOfValue()  
               {  
                   return Entry3 + Entry2 + Entry1+ Entry4 + Entry5 + Entry6+ Entry7 + Entry8 + Entry9+ Entry10;  
               }  
         
               public Command TextChangedCommand => new Command(() => TextChanged());  
         
               private void TextChanged()  
               {  
                   Sum = GetAllOfValue();  
               }  
         
                 
               public MainPage()  
               {  
         
                    
                   BindingContext = this;  
                   InitializeComponent();  
               }  
         
                
           }  
       }  
    

    Best Regards,

    Leon Lu


    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.


0 additional answers

Sort by: Most helpful