[英]Changing text of a textblock and verifying a boolean per click
我正在使用C#开发窗口电话应用程序。 我有2个文本块和4个单选按钮。 第一个文本块显示问题,第二个文本块显示结果递增的变量。 通过加载页面,它将在文本块中显示第一个问题。 通过检查单选按钮并单击应用程序栏,程序应验证选中的单选按钮为布尔型,如果选中了右单选按钮,则向变量a添加一个增量,然后将textBlock1.text更改为另一个文本(第二个问题) 。 然后再次检查单选按钮和应用栏,应重复上述步骤。 然后经过三个问题,变量a的最终值将显示在第二个textBlock上。 为了填充问题,我使用了这个数组,但没有成功:
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);
}`
它没有工作。 它仅在文本块中显示“欢迎回答问题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;`
这个阵列成功显示了每次点击的问题,但是只要点击一次,它就会不断重复显示问题2,3,4的变化。 另外,我不知道如何添加代码以验证布尔检查并在选中右单选按钮时增加变量。
这是代码正在使用
`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");
}
}`
我寻求有关1)增量,2)单选按钮布尔值验证和豁免管理的帮助。 我已经学习C#仅仅一年了。 请不要介意我的代码仍在学习中。 谢谢
我认为这是您想要的:
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.