[英]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.