.NET : Quiz Application

In this tutorial we are developing a "simple quiz application" using C# and .NET. In this tutorial we have taken 5 windows forms namely Form1,Form2,Form3,Form4 and Result for
Question 1, Question 2, Question 3, Question 4 and for Result and all under Quiz namespace. For this we are also using constructors for passing values from one form to another and lastly will show correct and incorrect no of answers in result form respectively. Let's get started!!

First of all open Visual Studios. Go to File>New>Projects>Windows Forms Application and name it "Quiz". You will see "Form1" in your screen. This Form1 is for your Question1. Here we are making Quiz of 4 ques respectively.



Now to rename "Form1" to "Question1" click on form to select it and go to form properties and in Text option remove "Form1" to "Question1".




Customize your form as you want and for questions we are using "Picture Box", "Radio button" and "Button" controls. To add these controls to form just go to your toolbox and search for "Picture Box", "Radio button" and "Button" control, drag and then drop them on your form.



Just like this add "radio buttons" and "button" control to your form. And in the text field rename them.




Now, to add functionalities to your form just double click on individual controls and add following codes mentioned below in your Form1,Form2,Form3,Form4 respectively.

This is your Form1 code for Question1

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Quiz
{
    public partial class Form1 : Form
    {
        int m = 0;
        public Form1()
        {
            InitializeComponent();
        }

        private void radioButton4_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // for unchecking all radio buttons
            this.radioButton1.TabStop = false;
            this.radioButton2.TabStop = false;
            this.radioButton3.TabStop = false;
            this.radioButton4.TabStop = false;
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (radioButton1.Checked == true)
            {
                m = m + 1;
            }

            else { m = 0; }
            Form2 obj1 = new Form2(m);
            obj1.Show();
            this.Hide();
            
        }
    }
}
This is your Form2 code for Question2

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Quiz
{
    public partial class Form2 : Form
    {
        int m;
        public Form2(int n)
        {
            InitializeComponent();
            m = n;
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            // for unchecking all radio buttons
            this.radioButton1.TabStop = false;
            this.radioButton2.TabStop = false;
            this.radioButton3.TabStop = false;
            this.radioButton4.TabStop = false;
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (radioButton3.Checked == true)
            {
                m = m + 1;
            }
            else { m = m-0; }
            Form3 obj2 = new Form3(m);
            obj2.Show();
            this.Hide();
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void radioButton4_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }
    }
}
This is your Form3 code for Question3

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Quiz
{
    public partial class Form3 : Form
    {
        int m;
        public Form3(int o)
        {
            InitializeComponent();
            m = o;
        }

        private void Form3_Load(object sender, EventArgs e)
        {
            // for unchecking all radio buttons
            this.radioButton1.TabStop = false;
            this.radioButton2.TabStop = false;
            this.radioButton3.TabStop = false;
            this.radioButton4.TabStop = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (radioButton2.Checked == true)
            {
                m = m + 1;
            }

            else { m = m-0; }
            Form4 obj3 = new Form4(m);
            obj3.Show();
            this.Hide();
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {

        }
    }
}
This is your Form4 code for Question4

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Quiz
{
    public partial class Form4 : Form
    {
        int m;
        public Form4(int q)
        {
            InitializeComponent();
            m = q;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (radioButton1.Checked == true)
            {
                m = m + 1;
            }

            else { m = m - 0; }
            Result res = new Result(m);
            res.Show();
            this.Hide();
        }

        private void Form4_Load(object sender, EventArgs e)
        {
            // for unchecking all radio buttons
            this.radioButton1.TabStop = false;
            this.radioButton2.TabStop = false;
            this.radioButton3.TabStop = false;
            this.radioButton4.TabStop = false;
        }
    }
}
"this.radioButton1.TabStop = false;, this.radioButton2.TabStop = false;, this.radioButton3.TabStop = false;, this.radioButton4.TabStop = false;". We are using this line to uncheck all radio buttons because by default one radiobutton is selected. And as we are making a Quiz App then we only select those questions which are correct and we do this on loading a form. To add these codes just double click on your form and add these lines.

Form2 obj1 = new Form2(m); obj1.Show();Also, you will see these two lines of code in every line. Here we are creating object of those form which we open when we click next button. "obj.Show();" will open Form2 when user clicks on next. To add this functionality on "Next" button just double click on your next button and add these lines. Also, if you want to hide previous question and don't want to open multiple forms use "this.Hide();" like we used here.

Here, we are also using constructors to pass value(m) from one form to another. If user selects a correct question i.e selects a correct radio button like in question1 radiobutton1 is correct then m will be updated to 1 (m=m+1) and if not then m=m-0;. "radioButton1.Checked == true" means if radiobutton1 is checked that means if user clicks on correct answer(radiobutton1) then only m will be updated and hence value of m is sent to another form.

Now, in Result.cs we display our result in labels.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Quiz
{
    public partial class Result : Form
    {
        int m;
        public Result(int p)
        {
            InitializeComponent();
            m = p;
        }

        private void Result_Load(object sender, EventArgs e)
        {
            label1.Text = m.ToString();
            int n = 4 - m;
            label5.Text = n.ToString();
        }
    }
}

Done!!


You can download project files here.

SHARE

Raunak Hajela

Hi. I’m CEO/Founder of Kodestat. I’m Student, Geek, Web Designer, Developer, Blog Enthusiast and Player. Inspired to make things looks better. I like playing with codes plus I am marvel fan :-P You can check my design portfolio here. For consulation fell free to drop an email at raunakhajela@gmail.com.

  • Image
  • Image
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment