Advertisement
DrAungWinHtut

frmExam.CS

Mar 25th, 2022
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.91 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.IO;
  11.  
  12. namespace Exam_System
  13. {
  14.     public partial class frmExam : Form
  15.     {
  16.  
  17.         string[] saQuestions = new string[100];
  18.         string[] saCurrentQuestion = new string[7];
  19.         int iCurrentQno = 0;
  20.         int iTotalQ = 0;
  21.         string sCurrentCorrectAns = "";
  22.  
  23.         public frmExam()
  24.         {
  25.             InitializeComponent();
  26.         }
  27.  
  28.         private void frmExam_Load(object sender, EventArgs e)
  29.         {
  30.            
  31.         }
  32.  
  33.         private void funMarking()
  34.         {
  35.             if( rdo1.Checked == false && rdo2.Checked == false && rdo3.Checked == false && rdo4.Checked == false )
  36.             {
  37.                 MessageBox.Show("Please Choose One Answer");
  38.             }
  39.             else
  40.             {
  41.                 int iScore = int.Parse(txtScores.Text);
  42.                 if (sCurrentCorrectAns == "1" && rdo1.Checked == true)
  43.                 {
  44.                     iScore = iScore + 10;
  45.                 }
  46.                 else if (sCurrentCorrectAns == "2" && rdo2.Checked == true)
  47.                 {
  48.                     iScore = iScore + 10;
  49.                 }
  50.                 else if (sCurrentCorrectAns == "3" && rdo3.Checked == true)
  51.                 {
  52.                     iScore = iScore + 10;
  53.                 }
  54.                 else if (sCurrentCorrectAns == "4" && rdo4.Checked == true)
  55.                 {
  56.                     iScore = iScore + 10;
  57.                 }
  58.                 txtScores.Text = iScore.ToString();
  59.                 btnSubmit.Enabled = false;
  60.                 rdo1.Enabled = false;
  61.                 rdo2.Enabled = false;
  62.                 rdo3.Enabled = false;
  63.                 rdo4.Enabled = false;
  64.             }
  65.  
  66.            
  67.            
  68.         }
  69.  
  70.         private void btnSubmit_Click(object sender, EventArgs e)
  71.         {
  72.             funMarking();
  73.             btnNext .Enabled = true;
  74.            
  75.         }
  76.  
  77.         private void btnNext_Click(object sender, EventArgs e)
  78.         {
  79.             if(iCurrentQno < iTotalQ ) //    4 - 0 1 2 3
  80.             {
  81.                 funNextQuestion();
  82.             }
  83.             else{
  84.                 MessageBox.Show("The Quiz is Over");
  85.                 btnNext .Enabled = false;    
  86.             }
  87.            
  88.         }
  89.  
  90.         private void funNextQuestion()
  91.         {
  92.             saCurrentQuestion = saQuestions[iCurrentQno].Split('#');
  93.             if(saCurrentQuestion .Length ==7)
  94.             {
  95.                 txtQuestion.Text = saCurrentQuestion[1];
  96.                 rdo1.Text = saCurrentQuestion[2];
  97.                 rdo2.Text = saCurrentQuestion[3];
  98.                 rdo3.Text = saCurrentQuestion[4];
  99.                 rdo4.Text = saCurrentQuestion[5];
  100.                 sCurrentCorrectAns = saCurrentQuestion[6];
  101.                 iCurrentQno++;
  102.                 lblQno.Text = iCurrentQno + "/" + iTotalQ;
  103.                 btnSubmit.Enabled = true;
  104.                 rdo1.Enabled = true;
  105.                 rdo2.Enabled = true;
  106.                 rdo3.Enabled = true;
  107.                 rdo4.Enabled = true;
  108.  
  109.                 btnNext.Enabled = false;
  110.             }
  111.             else
  112.             {
  113.                 iCurrentQno++;
  114.                 funNextQuestion();
  115.             }
  116.          
  117.         }
  118.  
  119.         private void frmExam_FormClosing(object sender, FormClosingEventArgs e)
  120.         {
  121.             Application.Exit();
  122.         }
  123.  
  124.         private void btnBrowse_Click(object sender, EventArgs e)
  125.         {
  126.             if(ofdFname .ShowDialog() == DialogResult.OK)
  127.             {
  128.                 txtFname .Text = ofdFname.FileName;
  129.             }
  130.         }
  131.  
  132.         private void btnStart_Click(object sender, EventArgs e)
  133.         {
  134.             txtScores.Text = "0";
  135.             if(txtFname .Text ==string.Empty )
  136.             {
  137.                 MessageBox.Show("Invalid question");
  138.                 return;
  139.             }
  140.             string sQuestion = txtFname .Text ;
  141.             try
  142.             {
  143.                 // Create an instance of StreamReader to read from a file.
  144.                 // The using statement also closes the StreamReader.
  145.                 using (StreamReader sr = new StreamReader(sQuestion))
  146.                 {
  147.                     string line;
  148.                     // Read and display lines from the file until the end of
  149.                     // the file is reached.
  150.                     while ((line = sr.ReadLine()) != null)
  151.                     {
  152.                         saQuestions[iTotalQ] = line;
  153.                         iTotalQ = iTotalQ + 1;
  154.                     }
  155.                 }
  156.             }
  157.             catch (Exception ex)
  158.             {
  159.                 MessageBox.Show(ex.Message);
  160.             }
  161.             funNextQuestion();
  162.  
  163.         }
  164.     }
  165. }
  166.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement