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

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

Community
  • 1
  • 1
AAB
  • 1,594
  • 2
  • 25
  • 40
  • 5
    Have you attempted to debg the program, where is it erroring out, what is the exception? Both `Form.Close()` (if this is your only form) and `Application.Exit()` should do the job. – Jason Jun 21 '13 at 22:17
  • Do you have form closing event as well? – Shaharyar Jun 21 '13 at 22:21
  • Hi the Error is shown in the First Method it highlights string temp=choice.split('\t'); the Exception is **A first chance exception of type 'System.NullReferenceException' occurred in FirstGUI.exe** – AAB Jun 21 '13 at 22:39
  • @Shaharyar The Close button Works Fine.. The Exception happens after the Message box Displays the Marks **one possible other problem occuring place could be count as this value is used as an index for an array that holds the Answer Keys** But VS2012 Higlights only choice.Split part in the Code; – AAB Jun 21 '13 at 22:46

2 Answers2

2

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;
Benji Vesterby
  • 574
  • 5
  • 21
  • Hi Thanks for the **else** it solves the Exception that was coming(bottom code not executed) I don`t understand what you mean by FormLoaded I am just trying out GUI for the First time so am sorry if its simple thing – AAB Jun 21 '13 at 22:54
  • Actually I stand corrected. I looked it up to give you a link but there is no Loaded event for a windows form. My bad. I'm glad the else worked for you. I've run into that error in the past. It has to do with the form being closed, and the controls on the form being dereferenced because the form doesn't exist anymore but the code will still try to run because it's not in an else. – Benji Vesterby Jun 21 '13 at 23:00
  • **else** has solved the exception but the problem now is the code stays at Last Question that was Answered No More Exceptions If i add a try catch block at choice.split('\t') the Exception that is thrown is caught but now a Array Index out of range is generated at the Check Method as Count value exceeds Array Index Thanks You For Your Help... – AAB Jun 21 '13 at 23:05
  • Can you please give an example of what would be stored in the "src" object? – Benji Vesterby Jun 21 '13 at 23:12
  • @ devnw thanks a ton for the "else" tip just tried the SendKeys.Send("%{F4}"); again it worked like a charm.. I think the Error was Previously caused because maybe a thread began to execute the else part before the form closed or may have already started working the else part while the Message Box was being Displayed I assumed that the Control would flow one by one..... that is after if block executes only then will the code below it execute guess thats not the case here what do you think is a possible reason for the error – AAB Jun 21 '13 at 23:14
  • hi the src object is of class StreamReader to read from the file where the Question and Choices exist I initialized src,srq and few file object inside the **form Constructor** is that a bad? – AAB Jun 21 '13 at 23:17
  • I'm just wondering because what you are describing my have to do with accessing an uninitialized section of your array. I modified my answer, and if you use the new method I suggest instead of the array it may make it work for you. – Benji Vesterby Jun 21 '13 at 23:25
  • Hi I`m Sorry to not have stated clearly but count value is used as an index by 1 variable ans_key[count] and the method **check(RadioButton b)** created for just comparing the text of a radio button with a string array that has values already initialized i.e. the Answers their is no problem while display choice for the Question infact after **else** the Code has not generated any More issues If the Form gets closed at count==2 then there won`t be any array Index out range as well as that part of the code is not been executed now turns out the Simplest Modification has solved all the issues – AAB Jun 21 '13 at 23:35
  • I still have those try catch blocks around choice.Split() Let me remove those to see if problems throws out or not :P Edit: Update No More Exceptions Problem Solved – AAB Jun 21 '13 at 23:45
0

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

        }
Kas
  • 3,747
  • 5
  • 29
  • 56
  • @ Dooby Inc Thanks the Count Value is Fine and now the Quiz is working Fine as well with an added Timer Facility that Counts down and shuts down the Quiz after 30 Seconds Thanks for taking interest in Answering My Questions – AAB Jun 22 '13 at 16:27