下面是一个简单的 Windows 窗体选择题代码示例:

```csharp
using System;
using System.Windows.Forms;
public class QuizForm : Form
{
private Label questionLabel;
private RadioButton[] answerRadioButtons;
private Button submitButton;
private int currentQuestionIndex = 0;
private int[] correctAnswers = { 1, 3, 2 }; // 每个问题的正确答案索引
public QuizForm()
{
this.Text = "Windows 窗体选择题";
this.Size = new System.Drawing.Size(400, 300);
this.StartPosition = FormStartPosition.CenterScreen;
// 创建问题标签
this.questionLabel = new Label();
this.questionLabel.Text = "问题 1: 什么是 .NET Framework?";
this.questionLabel.Location = new System.Drawing.Point(20, 20);
this.questionLabel.AutoSize = true;
this.Controls.Add(this.questionLabel);
// 创建选择答案的 RadioButton 组
this.answerRadioButtons = new RadioButton[3];
for (int i = 0; i < this.answerRadioButtons.Length; i++)
{
this.answerRadioButtons[i] = new RadioButton();
this.answerRadioButtons[i].Text = "选项 " + (i + 1);
this.answerRadioButtons[i].Location = new System.Drawing.Point(20, 50 + i * 30);
this.Controls.Add(this.answerRadioButtons[i]);
}
// 创建提交按钮
this.submitButton = new Button();
this.submitButton.Text = "提交";
this.submitButton.Location = new System.Drawing.Point(20, 170);
this.submitButton.Click += SubmitButton_Click;
this.Controls.Add(this.submitButton);
}
private void SubmitButton_Click(object sender, EventArgs e)
{
// 检查用户选择的答案是否正确
int selectedAnswerIndex = Array.IndexOf(this.answerRadioButtons, this.answerRadioButtons.FirstOrDefault(rb => rb.Checked));
if (selectedAnswerIndex == this.correctAnswers[this.currentQuestionIndex])
{
MessageBox.Show("答对了!");
}
else
{
MessageBox.Show("很遗憾,答错了。");
}
// 移动到下一个问题
this.currentQuestionIndex++;
if (this.currentQuestionIndex < this.correctAnswers.Length)
{
this.questionLabel.Text = $"问题 {this.currentQuestionIndex + 1}: 什么是 C#?";
for (int i = 0; i < this.answerRadioButtons.Length; i++)
{
this.answerRadioButtons[i].Checked = false;
}
}
else
{
MessageBox.Show("测试结束,感谢参与!");
this.Close();
}
}
}
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new QuizForm());
}
}
```
这个示例创建了一个简单的 Windows 窗体选择题应用程序。它包含一个问题标签、三个单选按钮作为答案选择,以及一个提交按钮。当用户选择答案并点击提交按钮时,程序会检查答案是否正确,并显示相应的提示。然后移动到下一个问题。当所有问题都回答完成后,程序会显示一个退出提示。
你可以根据需要修改问题、答案选项和相关逻辑,来创建自己的选择题应用程序。