我有4个标签,有4个答案。一个是正确的。当我按下按钮时,我希望隐藏2个答案。所以我有:
string[] possible_answers = { "A", "B", "C", "D" };
string correct_answer= keyletter;
string[] wrong_answers= posibile_variante.Where(val => val != correct_answer ).ToArray();
和标签
label_0
label_2
label_1
label_3
假设
label_0 = A
label_1 = B
label_2 = C
label_3 = D
如何隐藏2个错误答案标签?
答案 0 :(得分:0)
拥有一个包含
标签的词典var labels = Dictionary<string, Label> { { "A", label_0 }, { "B", label_1 }, ... };
然后按下按钮点击
foreach (var wa in wrong_answers)
labels[wa].Visible = false;
也许它不完全像你需要它,但你可以从中得到一个想法。
答案 1 :(得分:0)
汇总了一个过于精细的例子,假设您可能想要更改给定问题的可能答案数。它根据possible_answers(ResetLabels()
)中的元素数量动态设置屏幕上的标签数量,然后随机选择N(显示的#Items - 要显示的数字)错误的数量从屏幕RemoveWrongLabels
中删除的答案。 @Ondrej建议使用Take(2),但是静态地选择列表中的前2个元素(例如,用户将始终知道剩余项目中的正确答案是什么)。 MSDN:Returns a specified number of contiguous elements from the start of a sequence.
string[] possible_answers = { "A", "B", "C", "D" };
private void button2_Click(object sender, EventArgs e)
{
ResetLabels();
}
private void ResetLabels()
{
for (int i = 0; i < this.labelContainer.Controls.Count - 1; i++)
{
this.labelContainer.Controls[i].Dispose();
}
this.labelContainer.Controls.Clear();
for (int i = possible_answers.Count() - 1; i >= 0; i--)
{
Label l = new Label();
l.Text = possible_answers[i];
l.Dock = DockStyle.Top;
this.labelContainer.Controls.Add(l);
}
}
private void button3_Click(object sender, EventArgs e)
{
RemoveWrongLabels(3, 2);
}
private void RemoveWrongLabels(int RightAnswer, int ItemsToDisplay)
{
Random rand = new Random();
List<int> AnswersToHide = new List<int>();
while (AnswersToHide.Count < labelContainer.Controls.Count - ItemsToDisplay)
{
int indexToHide = rand.Next(0, labelContainer.Controls.Count);
if (!AnswersToHide.Contains(indexToHide) && labelContainer.Controls[indexToHide].Text != possible_answers[RightAnswer])
{
AnswersToHide.Add(indexToHide);
}
}
foreach (var item in AnswersToHide)
{
this.labelContainer.Controls[item].Visible = false;
}
}