Randomly access string from array list
<p>I am working on a quiz project which also allows user to add new questions.</p>

<p>I have an array list in which questions are stored and retrieved for displaying. Each object saved in the array list contains 5 strings.</p>

<ul>
<li>Question</li>
<li>Right answer</li>
<li>Wrong answer 1</li>
<li>Wrong answer 2</li>
<li>Wrong answer 3</li>
</ul>

<p>How can I randomly select objects from the array list to be displayed on the screen? And how can I shuffle the 4 answers (as radio buttons) so that the right answer appears at different positions each time?</p>

<pre><code>    namespace quiz
{
    public partial class Quiz : Form
    {
        private ArrayList Questionslist;

        public Quiz(ArrayList list)
        {
            InitializeComponent();
            Questionslist = list;
        }

        int index = 0;
        private void Quiz_Load(object sender, EventArgs e)
        {
            //creating an object of class Question and copying the object at index1 from arraylist into it  
            Question q = (Question)Questionslist[index];
            //to display the contents
            lblQs.Text = q.Quest;
            radioButtonA1.Text = q.RightAnswer;
            radioButtonA2.Text = q.WrongAnswer1;
            radioButtonA3.Text = q.WrongAnswer2;
            radioButtonA4.Text = q.WrongAnswer3;            
        }

        private int Score = 0;

        private void radioButtonA1_CheckedChanged(object sender, EventArgs e)
        {
            //if checkbox is checked
            //displaying text in separate two lines on messagebox
            if (radioButtonA1.Checked == true)
            {
                MessageBox.Show("Well Done" + Environment.NewLine + "You Are Right");
                Score++;
                index++;

                if (index &lt; Questionslist.Count)
                {
                    radioButtonA1.Checked = false;
                    radioButtonA2.Checked = false;
                    radioButtonA3.Checked = false;
                    radioButtonA4.Checked = false;
                    Quiz_Load(sender, e);
                }
                else
                {
                    index--;
                    MessageBox.Show("Quiz Finished" + Environment.NewLine + "your Score is" + Score);
                    Close();
                }
            }
        }

        private void radioButtonA2_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButtonA2.Checked == true)
            {
                MessageBox.Show("Sorry" + Environment.NewLine + "You Are Wrong");
                Close();
            }
        }

        private void radioButtonA3_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButtonA3.Checked == true)
            {
                MessageBox.Show("Sorry" + Environment.NewLine + "You Are Wrong");
                Close();
            }
        }

        private void radioButtonA4_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButtonA4.Checked == true)
            {
                MessageBox.Show("Sorry" + Environment.NewLine + "You Are Wrong");
                Close();
            }
        }
    }
}
</code></pre>

<p>this is the code for class question</p>

<pre><code>namespace quiz
{
    [Serializable()]
    public class Question : ISerializable
    {
        public String Quest;
        public String RightAnswer;
        public String WrongAnswer1;
        public String WrongAnswer2;
        public String WrongAnswer3;

        public Question()
        {
           Quest = null;
           RightAnswer=null;
           WrongAnswer1=null;
           WrongAnswer2=null;
           WrongAnswer3=null;
        }

        //serialization function
        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("Question", Quest);
            info.AddValue("Right Answer", RightAnswer);
            info.AddValue("WrongAnswer1",WrongAnswer1);
            info.AddValue("WrongAnswer2",WrongAnswer2);
            info.AddValue("WrongAnswer3",WrongAnswer3);
        }

        //deserializaton constructor
        public Question(SerializationInfo info, StreamingContext context)
        {
            Quest = (String)info.GetValue("Question", typeof(String));
            RightAnswer=(String)info.GetValue("Right Answer",typeof(String));
            WrongAnswer1=(String)info.GetValue("WrongAnswer1",typeof(String));
            WrongAnswer2=(String)info.GetValue("WrongAnswer2",typeof(String));
            WrongAnswer3=(String)info.GetValue("WrongAnswer3",typeof(String));
        }     
    }
}
</code></pre>
