Question

Am working on a window phone application using C#. i have a 2 text blocks and four radio buttons. the first text block is to display the questions while the second is to display the incremented variable as the result. by loading a page, it displays the first question in the text block. by checking a radio button and clicking the application bar, the program should verify the checked radio button Boolean, add an increment to the variable a if the right radio button is checked and then change the textBlock1.text to another text (the second question). then another radio button check and app bar click should repeat the above steps. then after three questions, the final value of the variable a is displayed on the second textBlock. to populate the questions, i used this array and it didnt work:

public void Click_AppBar(object sender, System.EventArgs e)
        {
            // TODO: Add event handler implementation here.

        String[] pages = 
        {"welcome to question 2", 
            "welcome to question 3",
            "welcome to question 4"};
        foreach (string s in pages)
            textBlock1.Text = (s);
    }`

and it didnt work. it only displayed the "welcome to question 4" in the text block but i tried this code which i got from this site and it worked:

 `private List<string> questions= new List<string>()
        {"welcome to question 2", "welcome to question 3", "welcome to question 4"};
        private int clickCount = 0;

        protected void Click_AppBar(object sender, EventArgs e)
        {
        textBlock1.Text = questions[clickCount];
        clickCount++;
        if (clickCount == questions.Count)
        clickCount = 0;`

this array successfully displayed the questions per click but it keep repeating showing question 2,3,4 changes continuously as long as am clicking. Also, i have no idea how i will add the code to verify the Boolean check and to increment the variable if the right radio button is checked.
this is the code am using now

`public void Click_AppBar(object sender, System.EventArgs e)
    {
        // TODO: Add event handler implementation here.

        int a= 0;
            if (RadioA.IsChecked == true)
            {

                a++;

            }

            textBlock1.Text = ("welcome to question 2");

            else if (RadioB.IsChecked == true)
            {

                a++;

            }

            textBlock1.Text = ("welcome to question 3");
            else if (RadioC.IsChecked == true)
            {

                a++;

            }

            ShowResult.Text = (a.ToString());
            MessageBox.Show("You have finished");
        }

    }`

i seek for help with the 1) Increment, 2) radio button Boolean verifying and exemption managements. i have been learning C# for just a year. pls dont mind my codes as am still learning. thanks

Was it helpful?

Solution

I think here is what you want:

private List<Questions> questions = new List<Questions>()
    {
        new Questions()
        {
             Question = "welcome to question 1",
             CorrectAnswer = 2,
        },
        new Questions()
        {
             Question = "welcome to question 2",
             CorrectAnswer = 1,
        },
        new Questions()
        {
            Question =  "welcome to question 3",
            CorrectAnswer = 3,
        },
        new Questions()
        {
            Question = "welcome to question 4",
            CorrectAnswer = 2,
        },

    };
    int currentQuestionIndex = 0;
    private int numberCorectAnswer = 0;

    public List<RadioButton> listRadioButton = null;

    public void Click_AppBar(object sender, System.EventArgs e)
    {
        if (listRadioButton == null)
        {
            listRadioButton = new List<RadioButton>()
            {
                RadioA,
                RadioB,
                RadioC,
            };
        }
        Questions previousQuestions = this.questions[currentQuestionIndex];

        if (listRadioButton[previousQuestions.CorrectAnswer].IsChecked == true)
        {

            numberCorectAnswer++;
        }


        if (currentQuestionIndex == questions.Count-1)
        {
            MessageBox.Show("You have finished, number correct answer:" +numberCorectAnswer);
        }

        currentQuestionIndex++;
        textBlock1.Text = this.questions[currentQuestionIndex];

        //ShowResult.Text = (a.ToString());

    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top