Hi

I am creating a quiz of 10 questions, using a string array. When the user clicks on the next button the next question follows. How do i set it up that when the user comes towards the end of the array the previous button changes to finish.

I know how to change the name and the event handler for that button, but just cant work out how to get the code in to say its the end of the array so lets change the function of that button

Thanks for any help

You don't need to change the event handler associated with the button click, just change the text of the button. In the event handler compare the button's text with "Next" and do whatever Next is supposed to do. When near the end of the array change the text to "Finish". If the button's text is "Finish" do whatever Finish is supposed to do. It's just a simple if statement inside the button's click event handler.

if button.text == "Next"
then
    // do whatever Next is supposed to do
    if counter == array-size then
       change button.text to "Finish"
else
// Button.txt == "Finish"
   // do whatever Finish is supposed to do
end if

ok here is the code i have done so far this quiz. I have created 4 arrays. One that stores the questions, one with the answers, and the other with the correct answers, the other with the users answers.

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 WindowsFormsApplication1
{

    public partial class Form1 : Form
    {
        int i = 0;

        //Array of questions
        string[] Question = new string[] 
        { 
            " What component inside a computer is non volatile memory?", 
            "What component contains the ALU? ", 
            "What component can be static or dynamic? ", 
            "What component has the northbridge and southbridge?", 
            "Starting from the lowest put the computer file sizes in order?", 
            "Which of the following is a computer operating system?", 
            "What is the job of the kernel in a operating system?", 
            "Which of the following is the BEST solution for a home user who needs to backup multiple PCs?", 
            "Paging is a term used with what?" ,
            "Which hard drive format, restricts single file sizes to 4GB or less?"
        };

        //Array if answers
        string[] option1 = new string[] { "ROM", "HDD", "SSD","Graphics Card", "10KB  10GBs  10MBs  10TBs", "Xerox", "Manages  requests from input/output devices","Server", "CMOS", "FAT 16" };
        string[] option2 = new string[] { "RAM", "SSD", "RAM","Case", "10KB 10MBs 10GBs 10TBs", "Borax", "Used to run office packages","Desktop", "BIOS", "NTFS" };
        string[] option3 = new string[] { "SDRAM", "CPU", "USB","Hard drive", "10KB 10GBs 10TBs 10MBs", "Linux", "Allows multi tasking of multiple user windows","Laptop", "IP Addressing", "FAT 32" };
        string[] option4 = new string[] { "DRAM", "PSU", "PCI", "Motherboard", "10MBs  10GBs 10KBs 10TBs", "Xenon", "Allows connection to networks through wireless NIC","Tablet", "Virtual Memory","OSx Journaled" };

        //Array of correct anwesers

        int[] correct_answers = new int[] { 1, 3, 2, 4, 2, 3, 1, 1, 4 , 3};

        //Array of user answers
        int[] User_Answer = new int[10];

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)// Events at run time (Before user interaction)
        {
            Previous.Visible = false; //Previous Button Not displayed
            Next.Visible = false; // Next button Not displayed
        }

        private void MainImage_Click(object sender, EventArgs e)
        {

        }

        private void Exit_Click(object sender, EventArgs e)//Event handler for "Exit button"
        {
            DialogResult dlgresult;
            dlgresult = MessageBox.Show(
                "Are you sure you want Exit the program",
                "Quit Program?",
                MessageBoxButtons.YesNo,
                MessageBoxIcon.Question,
                MessageBoxDefaultButton.Button1);

            if (dlgresult == DialogResult.Yes)//
            {
                Application.Exit();
            }

        }

        private void Instructions_Click(object sender, EventArgs e)
        {
            DialogResult dlgResult;
            dlgResult = MessageBox.Show
                (

                "You will be asked 10 multiple choice questions of computer systems. You can only select ONE anwser from the 4 options. After the quiz you will be given your total score, and have the option to review incorrect questions",
                "Computer Systems Quiz Instructions",
                MessageBoxButtons.OK,
                MessageBoxIcon.Information,
                MessageBoxDefaultButton.Button2
                );
        }

        private void StartQuiz_Click(object sender, EventArgs e)//"Event handler for Start Quiz button"
        {
           // MainImage.Visible = false; //Hide main image
            Previous.Visible = true; //Show Previous Button
            Next.Visible = true; //Show next Button
            this.Instructions.Text = "Flag";   

            //Change name of start quiz button and create new event handler for button
            StartQuiz.Text = "Restart" ; //changes name of start quiz button to restart quiz
            StartQuiz.Click -= this.StartQuiz_Click; //unsubcribed orginal event handler of "start quiz"
            StartQuiz.Click += this.Restart_Click; //create new event handler renamed button "restart" 

            //change name of instructions button and create new event handler for button
            Instructions.Text = "Flag";
            Instructions.Click -= this.Instructions_Click;
            Instructions.Click += this.Flag_Click;

            //Start Question 1 of quiz

                        i = 0;
                        QuestionLabel.Text = Question[i]; //print first question of array
                        RadioAnswer1.Text = option1[i]; //print first option of array
                        RadioAnswer2.Text = option2[i]; //print second option of array
                        RadioAnswer3.Text = option3[i]; //print thrid option of array
                        RadioAnswer4.Text = option4[i]; //print fourth option of array
        }

        private void Restart_Click(Object sender, EventArgs e)// Event handler for "Restart" Button
        {
            DialogResult dlgresult;
            dlgresult= MessageBox.Show(
                "Are you sure you want to Restart the program",
                "Restart program?",   //name of message box
                MessageBoxButtons.YesNo, //message box options
                MessageBoxIcon.Question, //Message box icon question mark
                MessageBoxDefaultButton.Button1);

            if (dlgresult == DialogResult.Yes)//
            {
                Application.Restart();
            }
        }

        private void Flag_Click(Object sender, EventArgs e)// Event handler for flag button
        {
            //flag instructions to go here
        }

        private void Line1_Click(object sender, EventArgs e)
        {

        }

        private void Line3_Click(object sender, EventArgs e)
        {

        }

        private void Next_Click(object sender, EventArgs e)
        {

                if (RadioAnswer1.Checked)
                {
                    User_Answer[i] = 1;
                }
                else if (RadioAnswer2.Checked)
                {
                    User_Answer[i] = 2;
                }
                else if (RadioAnswer3.Checked)
                {
                    User_Answer[i] = 3;
                }
                else if (RadioAnswer4.Checked)
                {
                    User_Answer[i] = 4;
                }


                QuestionLabel.Text = Question[i]; //print first question of array
                RadioAnswer1.Text = option1[i]; //print first option of array
                RadioAnswer2.Text = option2[i]; //print second option of array
                RadioAnswer3.Text = option3[i]; //print thrid option of array
                RadioAnswer4.Text = option4[i]; //print fourth option of array

                i++;



            //Change name of start quiz button and create new event handler for button
            Next.Text = "Finish"; //changes name of Previous button to Finish
            Next.Click -= this.StartQuiz_Click; //unsubcribed orginal event handler of "Next"
            Next.Click += this.Finish_Click; //create new event handler renamed button "Finish" 

        }  




        private void Finish_Click (Object sender, EventArgs e)
        {

        }

        private void Previous_Click(object sender, EventArgs e)
        {

        }
    }
}

sorry for all the comments on the code but using comments helps me understand what i am doing when i take a period of absence from the program

on line 178 test the value of i with the number of rows in the array. If i == number of rows then execute lines 183 and 184.

Sounds stupid, now what would the code be for testing it. I know it's an if statement but I can't work it.

Thanks for your help so far

ok so i have tried this

if (Question[i] == "Which hard drive format, restricts single file sizes to 4GB or less?")
            {
                //Change name of start quiz button and create new event handler for button
                Next.Text = "Finish"; //changes name of Previous button to Finish
                Next.Click -= this.StartQuiz_Click; //unsubcribed orginal event handler of "Next"
                Next.Click += this.Finish_Click; //create new event handler renamed button "Finish" 
            }

but i get an error message when i get to 9th question "An unhandled exception of type 'System.IndexOutOfRangeException' occurred in Computer System Quiz.exe"

Whats the problem or what have i done wrong

Thanks

Change line 18 to this:

            const int NumQuestions = 10;
            string[] Question = new string[NumQuestions] 

Now the program will know how many rows are in the array, so

if (i == NumQuestions)
            {
                //Change name of start quiz button and create new event handler for button
                Next.Text = "Finish"; //changes name of Previous button to Finish
                Next.Click -= this.StartQuiz_Click; //unsubcribed orginal event handler of "Next"
                Next.Click += this.Finish_Click; //create new event handler renamed button "Finish" 
            }

Thats it, thank you so much you are a legend :)

Hi thanks for your reply.

This is the code i have done so far for the quiz

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 WindowsFormsApplication1
{

    public partial class Form1 : Form
    {
        int i = 0;
        int score = 0;

        //Array of questions
        const int NumQuestions = 10; //variable stroing number of questions
        string[] Question = new string[NumQuestions] // variable passed into the array
        { 
            " What component inside a computer is non volatile memory?", 
            "What component contains the ALU? ", 
            "What component can be static or dynamic? ", 
            "What component has the northbridge and southbridge?", 
            "Starting from the lowest put the computer file sizes in order?", 
            "Which of the following is a computer operating system?", 
            "What is the job of the kernel in a operating system?", 
            "What should be used to backup mutiple PCs?", 
            "Paging is a term used with what?" ,
            "Which hard drive format, restricts single file sizes to 4GB or less?"
        };

        //Array if answers
        string[] option1 = new string[] { "ROM", "HDD", "SSD","Graphics Card", "10KB  10GBs  10MBs  10TBs", "Xerox", "Manages  requests from input/output devices","Server", "CMOS", "FAT 16" };
        string[] option2 = new string[] { "RAM", "SSD", "RAM","Case", "10KB 10MBs 10GBs 10TBs", "Borax", "Used to run office packages","Desktop", "BIOS", "NTFS" };
        string[] option3 = new string[] { "SDRAM", "CPU", "USB","Hard drive", "10KB 10GBs 10TBs 10MBs", "Linux", "Allows multi tasking of multiple user windows","Laptop", "IP Addressing", "FAT 32" };
        string[] option4 = new string[] { "DRAM", "PSU", "PCI", "Motherboard", "10MBs  10GBs 10KBs 10TBs", "Xenon", "Allows connection to networks through wireless NIC","Tablet", "Virtual Memory","OSx Journaled" };

        //Array of correct anwesers

        int[] correct_answers = new int[] { 1, 3, 2, 4, 2, 3, 1, 1, 4 , 3};

        //Array of user answers
        int[] User_Answer = new int[10];

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)// Events at run time (Before user interaction)
        {
            Previous.Visible = false; //Previous Button Not displayed
            RadioAnswer1.Visible = false;
            RadioAnswer2.Visible = false;
            RadioAnswer3.Visible = false;
            RadioAnswer4.Visible = false;
            Finish.Visible = false;
            Review.Visible = false;
            Previous.Enabled = false;
            A1.Visible = false;
            A2.Visible = false;
            A3.Visible = false;
            A4.Visible = false;

        }

        private void MainImage_Click(object sender, EventArgs e)
        {

        }

        private void Exit_Click(object sender, EventArgs e)//Event handler for "Exit button"
        {
            DialogResult dlgresult;
            dlgresult = MessageBox.Show(
                "Are you sure you want Exit the program",
                "Quit Program?",
                MessageBoxButtons.YesNo,
                MessageBoxIcon.Question,
                MessageBoxDefaultButton.Button1);

            if (dlgresult == DialogResult.Yes)//
            {
                Application.Exit();
            }

        }

        private void Instructions_Click(object sender, EventArgs e)
        {
            DialogResult dlgResult;
            dlgResult = MessageBox.Show
                (

                "You will be asked 10 multiple choice questions of computer systems. You can only select ONE anwser from the 4 options. After the quiz you will be given your total score, and have the option to review incorrect questions",
                "Computer Systems Quiz Instructions",
                MessageBoxButtons.OK,
                MessageBoxIcon.Information,
                MessageBoxDefaultButton.Button2
                );
        }

        private void StartQuiz_Click(object sender, EventArgs e)//"Event handler for Start Quiz button"
        {
           MainImage.Visible = false; //Hide main image
            Previous.Visible = true; //Show Previous Button

            //Change name of start quiz button and create new event handler for button
            StartQuiz.Text = "Next >>" ; //changes name of start quiz button to restart quiz
            StartQuiz.Click -= this.StartQuiz_Click; //unsubcribed orginal event handler of "start quiz"
            StartQuiz.Click += this.Next_Click; //create new event handler renamed button "restart" 

            //change name of instructions button and create new event handler for button
            Instructions.Text = "Flag";
            Instructions.Click -= this.Instructions_Click;
            Instructions.Click += this.Flag_Click;

        }

        private void Flag_Click(Object sender, EventArgs e)// Event handler for flag button
        {
            //flag instructions to go here
        }

        private void Line1_Click(object sender, EventArgs e)
        {

        }

        private void Line3_Click(object sender, EventArgs e)
        {

        }

        private void Next_Click(object sender, EventArgs e)
        {


            Previous.Enabled = true;//Enables the previous button
            RadioAnswer1.Visible = true;
            RadioAnswer2.Visible = true; //Quiz answers now visible to user
            RadioAnswer3.Visible = true;
            RadioAnswer4.Visible = true;
            A1.Visible = true; //answer numbers now visible to the user
            A2.Visible = true;
            A3.Visible = true;
            A4.Visible = true;
            RadioAnswer1.Checked = false;
            RadioAnswer2.Checked = false;
            RadioAnswer3.Checked = false;
            RadioAnswer4.Checked = false;

                QuestionLabel.Text = Question[i]; //print first question of array
                RadioAnswer1.Text = option1[i]; //print first option of array
                RadioAnswer2.Text = option2[i]; //print second option of array
                RadioAnswer3.Text = option3[i]; //print thrid option of array
                RadioAnswer4.Text = option4[i]; //print fourth option of array

                    if ((RadioAnswer1.Checked) || (RadioAnswer2.Checked) || (RadioAnswer3.Checked) || (RadioAnswer4.Checked))
                    {
                        if (RadioAnswer1.Checked)
                        {
                            User_Answer[i] = 1;
                        }
                        else if (RadioAnswer2.Checked)
                        {
                            User_Answer[i] = 2;
                        }
                        else if (RadioAnswer3.Checked)
                        {
                            User_Answer[i] = 3;
                        }
                        else if (RadioAnswer4.Checked)
                        {
                            User_Answer[i] = 4;
                        }


                    else
                    {
                        MessageBox.Show("You have not not answered the question. \n Please answer the question before proceeding");
                    }
                }

            i++;

                if (i==NumQuestions)
            {

                Finish.Visible = true;
                StartQuiz.Enabled = false;
                Previous.Enabled = false;
            }
        }  

        private void Previous_Click(object sender, EventArgs e)
        {

        }

        private void MainImage_Click_1(object sender, EventArgs e)
        {

        }

        private void QuestionLabel_Click(object sender, EventArgs e)
        {

        }

        private void Finish_Click(object sender, EventArgs e)
        {

            Review.Visible = true;

            for (int i = 0; i < 9; i++)
            {
                if (User_Answer[i] == correct_answers[i])
                score = score + 1;
                MessageBox.Show("Well done you score" +score);//Prints User score
            }
        }

    }
}

sorry for all the comments as i like to annotate my code as i go. Any help based on what i have done so far will be great

Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.