Advertisement
DrAungWinHtut

Quiz Tutorial 1 in CS

Mar 21st, 2022
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.13 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.             try
  32.             {
  33.                 // Create an instance of StreamReader to read from a file.
  34.                 // The using statement also closes the StreamReader.
  35.                 using (StreamReader sr = new StreamReader("D:\\CStutorial\\Quiz\\q1.txt"))
  36.                 {
  37.                     string line;
  38.                     // Read and display lines from the file until the end of
  39.                     // the file is reached.
  40.                     while ((line = sr.ReadLine()) != null)
  41.                     {
  42.                         saQuestions[iTotalQ] = line;
  43.                         iTotalQ = iTotalQ + 1;
  44.                     }
  45.                 }
  46.             }
  47.             catch (Exception ex)
  48.             {
  49.                MessageBox.Show (ex.Message);
  50.             }
  51.  
  52.             saCurrentQuestion  =saQuestions[iCurrentQno].Split('#');
  53.             txtQuestion.Text = saCurrentQuestion[1];
  54.             rdo1 .Text =saCurrentQuestion [2];
  55.             rdo2.Text =saCurrentQuestion [3];
  56.             rdo3.Text =saCurrentQuestion [4];
  57.             rdo4.Text =saCurrentQuestion [5];
  58.             sCurrentCorrectAns = saCurrentQuestion [6];
  59.             iCurrentQno++;
  60.             lblQno.Text = iCurrentQno +"/"+iTotalQ ;
  61.             txtScores.Text = "0";
  62.  
  63.         }
  64.  
  65.         private void funMarking()
  66.         {
  67.             if( rdo1.Checked == false && rdo2.Checked == false && rdo3.Checked == false && rdo4.Checked == false )
  68.             {
  69.                 MessageBox.Show("Please Choose One Answer");
  70.             }
  71.             else
  72.             {
  73.                 int iScore = int.Parse(txtScores.Text);
  74.                 if (sCurrentCorrectAns == "1" && rdo1.Checked == true)
  75.                 {
  76.                     iScore = iScore + 10;
  77.                 }
  78.                 else if (sCurrentCorrectAns == "2" && rdo2.Checked == true)
  79.                 {
  80.                     iScore = iScore + 10;
  81.                 }
  82.                 else if (sCurrentCorrectAns == "3" && rdo3.Checked == true)
  83.                 {
  84.                     iScore = iScore + 10;
  85.                 }
  86.                 else if (sCurrentCorrectAns == "4" && rdo4.Checked == true)
  87.                 {
  88.                     iScore = iScore + 10;
  89.                 }
  90.                 txtScores.Text = iScore.ToString();
  91.             }
  92.  
  93.            
  94.            
  95.         }
  96.  
  97.         private void btnSubmit_Click(object sender, EventArgs e)
  98.         {
  99.             funMarking();
  100.         }
  101.     }
  102. }
  103.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement