简体   繁体   English

更改文本块的文本并验证每次单击的布尔值

[英]Changing text of a textblock and verifying a boolean per click

Am working on a window phone application using C#. 我正在使用C#开发窗口电话应用程序。 i have a 2 text blocks and four radio buttons. 我有2个文本块和4个单选按钮。 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). 通过检查单选按钮并单击应用程序栏,程序应验证选中的单选按钮为布尔型,如果选中了右单选按钮,则向变量a添加一个增量,然后将textBlock1.text更改为另一个文本(第二个问题) 。 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. 然后经过三个问题,变量a的最终值将显示在第二个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: 它仅在文本块中显示“欢迎回答问题4”,但我尝试了从此站点获得的这段代码,并且可以正常工作:

 `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. 这个阵列成功显示了每次点击的问题,但是只要点击一次,它就会不断重复显示问题2,3,4的变化。 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. 我寻求有关1)增量,2)单选按钮布尔值验证和豁免管理的帮助。 i have been learning C# for just a year. 我已经学习C#仅仅一年了。 pls dont mind my codes as am still learning. 请不要介意我的代码仍在学习中。 thanks 谢谢

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());

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM