Example:
Quiz Engine Requirements
User is greeted by splash page.
Quizzes are multiple choice. One question per screen.
User can see the results on the screen.
Example:
Welcome PageQuestion PageSQL Server Database
Create a new project in Visual C# and add new forms to the project. Create a new SQL Server database for your project and add some test data.
-Welcome Page
--button to navigate to question page
-Question page
--question description
--3 radio buttons with 3 descriptions
--submit button
-Quiz Database
--question id
--question description
--question answer1 ID
--question answer1 Description
--question answer2 ID
--question answer2 Description
--question answer3 ID
--question answer3 Description
--question correct answer ID
Create a class to hold the questions and answers from the database.
Example:
class Question { public int intQuestionID { get; set; } public string strQuestionDescription { get; set; } public int intAnswer1ID { get; set; } public string strAnswer1Description { get; set; } public int intAnswer2ID { get; set; } public string strAnswer2Description { get; set; } public int intAnswer3ID { get; set; } public string strAnswer3Description { get; set; } public int intCorrectAnswerID { get; set; } public string strCorrectAnswerDescription { get; set; } }Start by adding code to the "Welcome Page". The button in the "Welcome Page" should open up a new "Question Page".
Example:
private void button1_Click(object sender, EventArgs e) { QuestionPage qp = new QuestionPage(); qp.Visible = true; this.Hide(); }Once the "Welcome Page" logic is complete, add the logic for "Question Page". The labels that describe the radio button must be populated with the "question answer description" fields for the first row in the database as well as the "question description" for the upper label control. This is done by reading the first row in the database and adding the fields to an object in a list.
Example:
System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(); conn.ConnectionString = @"integrated security=SSPI;data source=KEN-HP\SQLSERVER2008R2;" + "persist security info=False;initial catalog=quiz1"; try { conn.Open(); // Insert code to process data. SqlCommand command = new SqlCommand( "SELECT * FROM Table_1;", conn); SqlDataReader reader = command.ExecuteReader(); if (reader.HasRows) { while (reader.Read()) { Console.WriteLine("{0}\t{1}", reader.GetInt32(0), reader.GetString(1)); Question qr = new Question(); qr.intQuestionID = (int)reader[0]; qr.strQuestionDescription = (string)reader[1]; qr.intAnswer1ID = (int)reader[2]; qr.strAnswer1Description = (string)reader[3]; qr.intAnswer2ID = (int)reader[4]; qr.strAnswer2Description = (string)reader[5]; qr.intAnswer3ID = (int)reader[6]; qr.strAnswer3Description = (string)reader[7]; qr.intCorrectAnswerID = (int)reader[8]; QuestionRowList.Add(qr); } }// end if (reader.HasRows) else { Console.WriteLine("No rows found."); } reader.Close(); }// end try catch (Exception ex) { MessageBox.Show("Failed to connect to data source"); } finally { conn.Close(); } foreach (Question objQuestion in QuestionRowList) { if (objQuestion.intQuestionID == intTestQuestionNumber) { lblQuestion.Text = objQuestion.strQuestionDescription; radioButton1.Text = objQuestion.strAnswer1Description; radioButton2.Text = objQuestion.strAnswer2Description; radioButton3.Text = objQuestion.strAnswer3Description; } }// end foreach }// end QuestionPage()
Once the radio button is selected by the user, the code checks to determine which radio button is selected and gives it a value from 1 to 3. This value is checked against the list to determine the correct answer. In this case, I hard-coded "intTestQuestionNumber" to make this example easier to follow.
Example:
if (radioButton1.Checked) { Console.WriteLine("radio button1 checked"); intUserAnswer = 1; } if (radioButton2.Checked) { Console.WriteLine("radio button2 checked"); intUserAnswer = 2; } if (radioButton3.Checked) { Console.WriteLine("radio button3 checked"); intUserAnswer = 3; } foreach (Question objQuestion in QuestionRowList) { if (objQuestion.intQuestionID == intTestQuestionNumber) { if (objQuestion.intCorrectAnswerID == intUserAnswer) { lblResult.Text = "Your answer is correct!"; lblResult.Visible = true; } else { lblResult.Text = "Your answer is NOT correct."; lblResult.Visible = true; } } }// end foreach
Click here for the project code. Just create a SQL Server database with test data, edit the connection string and run it.
sorry but this code is crap... you are definitely not a tech geek!
ReplyDelete