RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1255598
Accepted
Ярослав Овчар
Ярослав Овчар
Asked:2022-03-14 14:51:24 +0000 UTC2022-03-14 14:51:24 +0000 UTC 2022-03-14 14:51:24 +0000 UTC

C# WinForms 测试程序

  • 772

程序中有三个测试问题。这是主要的表单代码:

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;
    }
c#
  • 1 1 个回答
  • 10 Views

1 个回答

  • Voted
  1. Best Answer
    Nyuhnyash
    2022-03-14T18:26:23Z2022-03-14T18:26:23Z

    该方法Correct()不处理这种情况Index == 4,因此resultLabel不受影响并保持上次修改状态。

    并且事件没有在设计器中指定:

    this.fourthRadioButton.Click += new System.EventHandler(this.radioButton_Click);
    
    • 1

相关问题

  • 使用嵌套类导出 xml 文件

  • 分层数据模板 [WPF]

  • 如何在 WPF 中为 ListView 手动创建列?

  • 在 2D 空间中,Collider 2D 挂在玩家身上,它对敌人的重量相同,我需要它这样当它们碰撞时,它们不会飞向不同的方向。统一

  • 如何在 c# 中使用 python 神经网络来创建语音合成?

  • 如何知道类中的方法是否属于接口?

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    表格填充不起作用

    • 2 个回答
  • Marko Smith

    提示 50/50,有两个,其中一个是正确的

    • 1 个回答
  • Marko Smith

    在 PyQt5 中停止进程

    • 1 个回答
  • Marko Smith

    我的脚本不工作

    • 1 个回答
  • Marko Smith

    在文本文件中写入和读取列表

    • 2 个回答
  • Marko Smith

    如何像屏幕截图中那样并排排列这些块?

    • 1 个回答
  • Marko Smith

    确定文本文件中每一行的字符数

    • 2 个回答
  • Marko Smith

    将接口对象传递给 JAVA 构造函数

    • 1 个回答
  • Marko Smith

    正确更新数据库中的数据

    • 1 个回答
  • Marko Smith

    Python解析不是css

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5