程序中有三个测试问题。这是主要的表单代码:
public partial class TestsForm : Form
{
private Ans ans = new Ans();
private int index = -1;
public TestsForm()
{
InitializeComponent();
ans.Question(groupBox1);
ans.Test(firstRadioButton, secondRadioButton, thirdRadioButton, fourthRadioButton);
resultLabel.Text = ans.GetCorrect;
}
private void radioButton_Click(object sender, EventArgs e)
{
RadioButton button = sender as RadioButton;
if (button == firstRadioButton) { index = 0; }
if (button == secondRadioButton) { index = 1; }
if (button == thirdRadioButton) { index = 2; }
if (button == fourthRadioButton) { index = 3; }
}
private void answerButton_Click(object sender, EventArgs e)
{
try
{
ans.Correct(index, resultLabel);
firstRadioButton.Checked = false;
secondRadioButton.Checked = false;
thirdRadioButton.Checked = false;
fourthRadioButton.Checked = false;
index = -1;
}
catch { return; }
ans.Index++;
if (ans.Index > 3) { MessageBox.Show("Congratulations!!!"); return; }
ans.Question(groupBox1);
ans.Test(firstRadioButton, secondRadioButton, thirdRadioButton, fourthRadioButton);
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
}
这是 Ans 类的代码:
private List<string> questions = new List<string>() { "Сколько будет два плюс два?", "Третья планета от Солнца?", "Какая из пречисленых планет не имеет твердой поверхности?" };
private List<string> answers = new List<string>() { "4", "Земля", "Юпитер" };
private List<string> test = new List<string>() { "5", "6", "4", "2", "Земля", "Марс", "Меркурий","Луна", "Плутон", "Юпитер", "Венера","Марс" };
private bool[,] boolTest = { { false, false, true, false }, { true, false, false, false }, { false, true, false ,false} };
private int correct = 0;
public int TestIndex { get; set; }
public int Index { get; set; }
public string GetCorrect { get; set; }
public void Question(Control control)
{
control.Text = questions[Index];
}
public void Test(Control control1, Control control2, Control control3, Control control4)
{
control1.Text = test[TestIndex];
TestIndex++;
control2.Text = test[TestIndex];
TestIndex++;
control3.Text = test[TestIndex];
TestIndex++;
control4.Text = test[TestIndex];
TestIndex++;
}
public void Correct(int i, Control control)
{
if (Index == 0)
{
if (boolTest[0, i] == true)
{
MessageBox.Show("Correct");
correct++;
GetCorrect = "Правильные ответы: " + correct.ToString() + " из 3";
control.Text = GetCorrect;
return;
}
else { MessageBox.Show("Not correct"); return; }
}
if (Index == 1)
{
if (boolTest[1, i] == true)
{
MessageBox.Show("Correct");
correct++;
GetCorrect = "Правильные ответы: " + correct.ToString() + " из 3";
control.Text = GetCorrect;
return;
}
else { MessageBox.Show("Not correct"); return; }
}
if (Index == 2)
{
if (boolTest[2, i] == true)
{
MessageBox.Show("Correct");
correct++;
GetCorrect = "Правильные ответы: " + correct.ToString() + " из 3";
control.Text = GetCorrect;
return;
}
else { MessageBox.Show("Not correct"); return; }
}
}
问题是第四个 radioButton 不起作用。但是,如果在此之前您单击其他人并在最后单击第四个,那么它会记住以前的 RadioButton 并给出其结果。在此先感谢您的帮助!这是来自 Designer.cs 的自动生成的代码
{
/// <summary>
/// Требуется переменная конструктора.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Освободить все используемые ресурсы.
/// </summary>
/// <param name="disposing">истинно, если управляемый ресурс должен быть удален; иначе ложно.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Код, автоматически созданный конструктором форм Windows
/// <summary>
/// Обязательный метод для поддержки конструктора - не изменяйте
/// содержимое данного метода при помощи редактора кода.
/// </summary>
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(TestsForm));
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.answerButton = new System.Windows.Forms.Button();
this.resultLabel = new System.Windows.Forms.Label();
this.thirdRadioButton = new System.Windows.Forms.RadioButton();
this.secondRadioButton = new System.Windows.Forms.RadioButton();
this.firstRadioButton = new System.Windows.Forms.RadioButton();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.panel1 = new System.Windows.Forms.Panel();
this.button1 = new System.Windows.Forms.Button();
this.StartTestButton = new System.Windows.Forms.Button();
this.panel2 = new System.Windows.Forms.Panel();
this.label1 = new System.Windows.Forms.Label();
this.fourthRadioButton = new System.Windows.Forms.RadioButton();
this.groupBox1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.panel1.SuspendLayout();
this.panel2.SuspendLayout();
this.SuspendLayout();
//
// groupBox1
//
this.groupBox1.BackColor = System.Drawing.Color.Transparent;
this.groupBox1.Controls.Add(this.fourthRadioButton);
this.groupBox1.Controls.Add(this.answerButton);
this.groupBox1.Controls.Add(this.resultLabel);
this.groupBox1.Controls.Add(this.thirdRadioButton);
this.groupBox1.Controls.Add(this.secondRadioButton);
this.groupBox1.Controls.Add(this.firstRadioButton);
this.groupBox1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.groupBox1.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204)));
this.groupBox1.Location = new System.Drawing.Point(0, 46);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(538, 540);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
//
// answerButton
//
this.answerButton.Location = new System.Drawing.Point(6, 144);
this.answerButton.Name = "answerButton";
this.answerButton.Size = new System.Drawing.Size(218, 23);
this.answerButton.TabIndex = 4;
this.answerButton.Text = "Ответить";
this.answerButton.UseVisualStyleBackColor = true;
this.answerButton.Click += new System.EventHandler(this.answerButton_Click);
//
// resultLabel
//
this.resultLabel.AutoSize = true;
this.resultLabel.Font = new System.Drawing.Font("Times New Roman", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204)));
this.resultLabel.Location = new System.Drawing.Point(6, 112);
this.resultLabel.Name = "resultLabel";
this.resultLabel.Size = new System.Drawing.Size(0, 15);
this.resultLabel.TabIndex = 3;
//
// thirdRadioButton
//
this.thirdRadioButton.AutoSize = true;
this.thirdRadioButton.Location = new System.Drawing.Point(6, 74);
this.thirdRadioButton.Name = "thirdRadioButton";
this.thirdRadioButton.Size = new System.Drawing.Size(14, 13);
this.thirdRadioButton.TabIndex = 2;
this.thirdRadioButton.UseVisualStyleBackColor = true;
this.thirdRadioButton.Click += new System.EventHandler(this.radioButton_Click);
//
// secondRadioButton
//
this.secondRadioButton.AutoSize = true;
this.secondRadioButton.Location = new System.Drawing.Point(6, 51);
this.secondRadioButton.Name = "secondRadioButton";
this.secondRadioButton.Size = new System.Drawing.Size(14, 13);
this.secondRadioButton.TabIndex = 1;
this.secondRadioButton.UseVisualStyleBackColor = true;
this.secondRadioButton.Click += new System.EventHandler(this.radioButton_Click);
//
// firstRadioButton
//
this.firstRadioButton.AutoSize = true;
this.firstRadioButton.Location = new System.Drawing.Point(6, 28);
this.firstRadioButton.Name = "firstRadioButton";
this.firstRadioButton.Size = new System.Drawing.Size(14, 13);
this.firstRadioButton.TabIndex = 0;
this.firstRadioButton.UseVisualStyleBackColor = true;
this.firstRadioButton.Click += new System.EventHandler(this.radioButton_Click);
//
// pictureBox1
//
this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
this.pictureBox1.Location = new System.Drawing.Point(-1, 0);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(538, 54);
this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.pictureBox1.TabIndex = 6;
this.pictureBox1.TabStop = false;
//
// panel1
//
this.panel1.BackColor = System.Drawing.SystemColors.InactiveBorder;
this.panel1.Controls.Add(this.button1);
this.panel1.Controls.Add(this.StartTestButton);
this.panel1.Location = new System.Drawing.Point(709, 28);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(538, 575);
this.panel1.TabIndex = 7;
//
// button1
//
this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button1.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204)));
this.button1.Location = new System.Drawing.Point(162, 328);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(217, 46);
this.button1.TabIndex = 14;
this.button1.Text = "Вийти";
this.button1.UseVisualStyleBackColor = true;
//
// StartTestButton
//
this.StartTestButton.BackColor = System.Drawing.SystemColors.Control;
this.StartTestButton.FlatAppearance.BorderSize = 0;
this.StartTestButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.StartTestButton.Font = new System.Drawing.Font("Microsoft Sans Serif", 13.8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204)));
this.StartTestButton.ForeColor = System.Drawing.Color.Black;
this.StartTestButton.Image = ((System.Drawing.Image)(resources.GetObject("StartTestButton.Image")));
this.StartTestButton.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.StartTestButton.Location = new System.Drawing.Point(128, 106);
this.StartTestButton.Margin = new System.Windows.Forms.Padding(2);
this.StartTestButton.Name = "StartTestButton";
this.StartTestButton.Size = new System.Drawing.Size(290, 144);
this.StartTestButton.TabIndex = 13;
this.StartTestButton.Text = "Пройти тест";
this.StartTestButton.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageBeforeText;
this.StartTestButton.UseVisualStyleBackColor = false;
this.StartTestButton.Click += new System.EventHandler(this.StartTestButton_Click_1);
//
// panel2
//
this.panel2.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("panel2.BackgroundImage")));
this.panel2.Controls.Add(this.label1);
this.panel2.Controls.Add(this.groupBox1);
this.panel2.Location = new System.Drawing.Point(-1, 51);
this.panel2.Name = "panel2";
this.panel2.Size = new System.Drawing.Size(538, 586);
this.panel2.TabIndex = 9;
//
// label1
//
this.label1.AutoSize = true;
this.label1.BackColor = System.Drawing.Color.Transparent;
this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204)));
this.label1.Location = new System.Drawing.Point(3, 6);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(313, 24);
this.label1.TabIndex = 1;
this.label1.Text = "Тема: Стародавня історія України";
//
// fourthRadioButton
//
this.fourthRadioButton.AutoSize = true;
this.fourthRadioButton.Location = new System.Drawing.Point(6, 96);
this.fourthRadioButton.Name = "fourthRadioButton";
this.fourthRadioButton.Size = new System.Drawing.Size(14, 13);
this.fourthRadioButton.TabIndex = 5;
this.fourthRadioButton.UseVisualStyleBackColor = true;
//
// TestsForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1529, 636);
this.Controls.Add(this.panel2);
this.Controls.Add(this.panel1);
this.Controls.Add(this.pictureBox1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.Name = "TestsForm";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Тести з історії України";
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.panel1.ResumeLayout(false);
this.panel2.ResumeLayout(false);
this.panel2.PerformLayout();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Button answerButton;
private System.Windows.Forms.Label resultLabel;
private System.Windows.Forms.RadioButton fourthRadioButton;
private System.Windows.Forms.RadioButton thirdRadioButton;
private System.Windows.Forms.RadioButton secondRadioButton;
private System.Windows.Forms.RadioButton firstRadioButton;
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Button StartTestButton;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Panel panel2;
private System.Windows.Forms.Label label1;
}
该方法
Correct()不处理这种情况Index == 4,因此resultLabel不受影响并保持上次修改状态。并且事件没有在设计器中指定: