Four Columns in a DataTable need  to be randomly assigned to four labels VB.NET

Posted on 2008-11-06
Last Modified: 2012-05-05

My problem is probably really simple and is as follows.

I have a data table full of questions.
They are in the following columns: ID, Question, CorrectAnswer, Incorrect1, Incorrect2, Incorrect3

It is for a multiple choice quiz.

I have four labels in a form: lblQ1, lblQ2, etc.

I need to randomly assign the location of my correct answer and then my incorrect answers.

I then need to be able to validate if the selected answer was the correct answer.

Any help much appreciated.

Question by:olifarago
    LVL 4

    Accepted Solution

    This is somethign quick and dirty that I just knocked up that may give you a few ideas.
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    	Dim Controls() As Control = {lblQ1, lblQ2, lblQ3, lblQ4}
    	Dim Answers() As String = {CorrectAnswer, Incorrect1, Incorrect2, Incorrect3}
    	Dim MuddledControls As New Generic.SortedList(Of Guid, Control)(Controls.Count)
    	For Each Control In Controls
    		 MuddledControls.Add(Guid.NewGuid, Control)
    	For I As Integer = 0 To Answers.Count - 1
    		 Dim Control = MuddledControls.Values(I)
    		 Control.Text = Answers(I)
    		 Control.Tag = If(I = 0, "Correct", "Incorrect")
    	Next I
    End Sub
    Private Sub CheckAnswer(ByVal sender As Control, ByVal e As System.EventArgs) Handles lblQ1.Click, lblQ2.Click, lblQ3.Click, lblQ4.Click
    	If sender.Tag = "Correct" Then
    		MsgBox("Correct Answer!")
    		MsgBox("Incorrect Answer!")
    	End If
    End Sub

    Open in new window

    LVL 9

    Author Closing Comment

    Perfect thanks

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
    A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now