1
private void Form1_Load(object sender, EventArgs e)
   {
         if (count == 2)
            {
                MessageBox.Show("Congrats You Score is : " + Marks, "Result", MessageBoxButtons.OK);
                SendKeys.Send("%{F4}");//tried Application.Exit() this.Close();

            }
            string choice = src.ReadLine();
            string ques = srq.ReadLine();
            opt = choice.Split('\t'); 
            label1.Font = new Font("Times New Roman", 15);
            label1.Text = ques;
            ch1.Font = new Font("Times New Roman", 15);
            ch1.Text = opt[0];
            ch2.Font = new Font("Times New Roman", 15);
            ch2.Text = opt[1];
            ch3.Font = new Font("Times New Roman", 15);
            ch3.Text = opt[2];
            ch4.Font = new Font("Times New Roman", 15);
            ch4.Text = opt[3];            
         }

我正在尝试在 GUI 中进行简单的测验,这不是家庭作业顺便说一句,我已经制作了一个控制台测验程序,现在希望在 GUI 中进行。我是一个初学者,只是在网上搜索了很多并尝试创建这个 Windows 窗体:

private void button1_Click(object sender, EventArgs e)
    {

        if (ch1.Checked == false && ch2.Checked==false && ch3.Checked==false && ch4.Checked==false)
        {
            MessageBox.Show("Please Choose An Answer", "Error", MessageBoxButtons.OK,MessageBoxIcon.Error);

        }
        else if (ch1.Checked){
            check(ch1);
           // MessageBox.Show("Marks : "+Marks);
            ++count;
            Form1_Load(new Object(), new EventArgs());
            ch1.Checked = false;


        }
        else if(ch2.Checked){
            check(ch2);
            ++count;
            Form1_Load(new Object(), new EventArgs());
            ch2.Checked = false;

        }
        else if(ch3.Checked){
            check(ch3);
            ++count;
            Form1_Load(new Object(), new EventArgs());
            ch3.Checked = false;
        }
        else if (ch4.Checked){
            check(ch4);
            ++count;
            Form1_Load(new Object(), new EventArgs());
            ch4.Checked = false;
        }
    }

上述方法继续加载新问题及其选项,并在按下下一步按钮后。

现在我希望测验在计数达到 2 或可能更多后自行退出。我试过了this.Close()SendKey,Environment.Exit(0, inputsimulator(是的,我确实下载了.dll文件并添加了它的参考,使用命名空间)也不起作用。

此外,inputsimulator 的缺点是它仅在选择应用程序时才有效...... sendkeys无论是否选择应用程序都有效,所以不是更好......

我知道像鼠标点击之类的事件需要this.close()工作,但我希望测验显示分数并在回答所有问题后自行关闭...

目前,测验没有关闭并且抛出异常,因为从中读取问题和选项的文件没有任何剩余......

我访问了以下链接 Link1 Link2 Link3

4

2 回答 2

2

我认为您应该将附加代码包装在 else 语句中。这将使您不想执行的内容无法执行。

“this.Close();” 应该管用。如果这是您的应用程序的主窗口,并且您想关闭应用程序,那么您将需要使用“Application.Exit();”

    if (count == 2)
        {
            MessageBox.Show("Congrats You Score is : " + Marks, "Result", MessageBoxButtons.OK);
            this.Close();

        }
    else
        {
           string choice = src.ReadLine();
           string ques = srq.ReadLine();
           opt = choice.Split('\t'); 
           label1.Font = new Font("Times New Roman", 15);
           label1.Text = ques;
           ch1.Font = new Font("Times New Roman", 15);
           ch1.Text = opt[0];
           ch2.Font = new Font("Times New Roman", 15);
           ch2.Text = opt[1];
           ch3.Font = new Font("Times New Roman", 15);
           ch3.Text = opt[2];
           ch4.Font = new Font("Times New Roman", 15);
           ch4.Text = opt[3];  
        }

至于你的数组部分,我实际上会这样做。

       List<string> opt = choice.Split('\t').ToList<string>(); 
       label1.Font = new Font("Times New Roman", 15);
       label1.Text = ques;

       if(opt.Count >= 1)
       {
          ch1.Font = new Font("Times New Roman", 15);
          ch1.Text = opt[0];
       }

       if(opt.Count >= 2)
       {
          ch2.Font = new Font("Times New Roman", 15);
          ch2.Text = opt[1];
       }

       if(opt.Count >= 3)
       {
          ch3.Font = new Font("Times New Roman", 15);
          ch3.Text = opt[2];
       }

       if(opt.Count >= 4)
       {
          ch4.Font = new Font("Times New Roman", 15);
          ch4.Text = opt[3];
       }

您可能需要将此添加到顶部。

       using System.Collections.Generic;
于 2013-06-21T22:44:04.770 回答
0

首先检查您的计数变量的值,我认为您的计数变量的值与二不同,这就是您的应用程序未关闭的原因,因为您仅在计数变量值等于二时才下令关闭应用程序。

为了确保您的计数变量有问题,请尝试将计数变量值设置为 2,然后再检查它是否等于 2。否则你可以使用调试模式来调试这个

  count= 2 ; // Set count to two , it doesn't matter where you set it to two , however it has to be set to two before you call this code if you really need to exit the program when you call this code.
     if (count == 2)
        {
            MessageBox.Show("Congrats You Score is : " + Marks, "Result", MessageBoxButtons.OK);
            SendKeys.Send("%{F4}");//tried Application.Exit() this.Close();

        }
于 2013-06-22T04:29:19.003 回答