そのようなプログラミングの方法をフロー駆動型プログラミングと言います。フォームを使う場合にはイベント駆動型プログラミングをするのが普通です。
まずフォームにラベル、テキストボックス、ボタンを貼り付け、それぞれを label1, textBox1, button1 と名付けてください。
次に button1 をダブルクリックしてクリックイベントを作成し、コードを次のように書き換えて実行してください。
C#
1using System;
2using System.Windows.Forms;
3
4namespace WindowsFormsApp1
5{
6 public partial class Form1 : Form
7 {
8 public Form1()
9 {
10 InitializeComponent();
11 label1.DataBindings.Add(new Binding(nameof(label1.Text), QandABindingSource, nameof(QandA.Question)));
12 }
13
14 private BindingSource QandABindingSource = new BindingSource()
15 {
16 DataSource = new[]
17 {
18 new QandA("日本で一番高い山は?", "富士山"),
19 new QandA("日本で二番目に高い山は?", "北岳"),
20 new QandA("日本で三番目に高い山は?", "奥穂高岳"),
21 }
22 };
23
24 private void button1_Click(object sender, EventArgs e)
25 {
26 if (!(QandABindingSource.Current is QandA qa)) return;
27 if (textBox1.Text == qa.Answer)
28 {
29 MessageBox.Show("正解!");
30 if (QandABindingSource.Position < QandABindingSource.Count - 1)
31 {
32 QandABindingSource.MoveNext();
33 }
34 else
35 {
36 QandABindingSource.MoveFirst();
37 }
38 }
39 else
40 {
41 MessageBox.Show("不正解。もう一度回答してください。");
42 }
43 }
44 }
45
46 class QandA
47 {
48 public QandA(string question, string answer)
49 {
50 (Question, Answer) = (question, answer);
51 }
52 public string Question { get; set; }
53 public string Answer { get; set; }
54 }
55}
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/03/22 00:50
2019/03/22 01:27
2019/03/22 05:40
2019/03/22 05:49
2019/03/22 05:50
2019/03/22 06:36