locked
Score count in quiz RRS feed

  • Question

  • Hi all I am making a windows store app using VS 2012. One of the elements will be a quiz which I am trying to make a score counter for that will display the user's current score.

    So currently I have created an integer and an increment for when the correct answer is selected. I have made a textbox to display the score but not sure how to display the score in it? Which event handler would be suitable to use in this situation? I already wrote the code that should convert the integer to a string so it can displayed in the textbox.

    Thanks

    • Moved by Caillen Friday, April 4, 2014 9:50 AM
    Thursday, April 3, 2014 2:04 PM

Answers

  • One of the easiest ways to doing this is through Databinding, You can have a textblock that has a text property bound to a property of your page or viewmodel.

    <TextBlock Text={Binding Score}/>

    In the code behind the page or in a viewmodel you can create a property called Score and leverage the INotifyPropertyChanged event to handle updates for you.

    See code below

     // Constructor
            public MainPage()
            {
                InitializeComponent();
    
                Score = 1;            
            }
                    
            private int score;
    
            public int Score
            {
                get { return score; }
                set { score = value; PropChanged("Score"); }
            }
    
            private void DoStuff()
            {
    
                Score = 10;
            }
            
    
            
            
            private void PropChanged(string name)
            {
                if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(name));
            }
    
            public event PropertyChangedEventHandler PropertyChanged;

    You can now change the score value by just setting the Property Score. if you only set the variable score the UI will not get updated

    Friday, April 4, 2014 10:00 AM

All replies

  • Hi Boombap,

    Welcome to post in MSDN forums.

    I'm moving your question to Windows Store App forum for better help.

    Thanks for your understanding.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, April 4, 2014 9:51 AM
  • One of the easiest ways to doing this is through Databinding, You can have a textblock that has a text property bound to a property of your page or viewmodel.

    <TextBlock Text={Binding Score}/>

    In the code behind the page or in a viewmodel you can create a property called Score and leverage the INotifyPropertyChanged event to handle updates for you.

    See code below

     // Constructor
            public MainPage()
            {
                InitializeComponent();
    
                Score = 1;            
            }
                    
            private int score;
    
            public int Score
            {
                get { return score; }
                set { score = value; PropChanged("Score"); }
            }
    
            private void DoStuff()
            {
    
                Score = 10;
            }
            
    
            
            
            private void PropChanged(string name)
            {
                if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(name));
            }
    
            public event PropertyChangedEventHandler PropertyChanged;

    You can now change the score value by just setting the Property Score. if you only set the variable score the UI will not get updated

    Friday, April 4, 2014 10:00 AM