AAB
AAB

Reputation: 1654

Close Windows Form After Execution of Message Box

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];            
         }

I am trying to Make a Simple Quiz in GUI this is Not Homework BTW I have Made a Console Quiz Program and Now wish to do it in GUI. I am a beginner and am just searching the net a lot and trying to create this Windows Form:

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;
        }
    }

The Above Method Keeps Loading a new Question and its options and After the Next button is pressed.

Now I want the Quiz to quit itself after count reaches 2 or may be more. I have tried this.Close(), SendKey,Environment.Exit(0, inputsimulator(Yes I did Download the .dll File and add its Reference,using namespace) as well does not work.

Also inputsimulator has the disadvantage that it works only when the App is selected... sendkeys works whether the app is selected or not so is it not better......

I understand that an event like mouse click or something is required for this.close() to work but I want the Quiz to Display the Score and shut it self after all Questions are Answered...

Currently the Quiz does not close and an exception is thrown as the File from which questions and options are read does not have anything left......

I have visited Following Links Link1 Link2 Link3

Upvotes: 1

Views: 5408

Answers (2)

Kas
Kas

Reputation: 3923

First check the value of your count varible , I think your count varibale is holding a different value than two , that's why your application is not closing , becuase you are ordering application to be closed only if the count variable value is equal to two.

For make sure that your count varible has a problem try setting count varbile value to two before you check if it equal two. otherwise you can use debug mode to debug this

  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();

        }

Upvotes: 0

user834319
user834319

Reputation:

I believe that you should wrap your additional code in an else statement. This will keep stuff you don't want executing from executing.

The "this.Close();" should work. If this is your primary window of the Application, and you want to close the application then you would want to use "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];  
        }

As for your Array Section I would actually do this instead.

       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];
       }

You may need to add this to the top.

       using System.Collections.Generic;

Upvotes: 2

Related Questions