6 Replies - 10944 Views - Last Post: 18 October 2007 - 08:21 AM Rate Topic: -----

#1 needcode  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 17-October 07

VB Quiz using select case and nested if then else

Posted 17 October 2007 - 01:28 PM

Hi i am making a quiz in my basic 1 class using select case and nested if then else. the program has a combo box showing the questions and another combo box showing the answers after you select a question and answer and click the button a message pops up and says correct or incorrect. I need the code for nesting a if then else and select case.

Thanks!!!!!
Is This A Good Question/Topic? 0
  • +

Replies To: VB Quiz using select case and nested if then else

#2 tody4me  Icon User is offline

  • Banned
  • member icon

Reputation: 13
  • View blog
  • Posts: 1,398
  • Joined: 12-April 06

Re: VB Quiz using select case and nested if then else

Posted 17 October 2007 - 02:09 PM

Answer to your question contained below:
http://www.dreaminco...wtopic13120.htm
Was This Post Helpful? 0
  • +
  • -

#3 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 421
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: VB Quiz using select case and nested if then else

Posted 17 October 2007 - 02:20 PM

Nesting only implies that the If/Else statement will be inside of the Select Case statement. Have you used either of those two statements on their own yet?

Please post the code that you have completed in an attempt to fulfill your assignment.
Was This Post Helpful? 0
  • +
  • -

#4 needcode  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 17-October 07

Re: VB Quiz using select case and nested if then else

Posted 17 October 2007 - 03:11 PM

Oh sorry, well this is my code
Select Case 
Case 0
If cmbAnswer.SelectedIndex = 0 Then
   MessageBox.Show("Correct","Quiz")
Else
   MessageBox.Show("Incorrect","Quiz")
etc.

Nested If Then Else
If cmbQuestions.SelectedIndex = 0 then
  If cmbAnswer.SelectedIndex = 0 then
   MessageBox.Show("Correct", "Quiz")
Else
   MessageBox.Show("Incorrect","Quiz")
If cmbQuestions.SelectedIndex = 1 then
  If cmbAnswer.SelectedIndex = 1 then
   MessageBox.Show("Correct", "Quiz")
Else
   MessageBox.Show("Incorrect","Quiz")
End If x 4

This post has been edited by jayman9: 17 October 2007 - 06:34 PM

Was This Post Helpful? 0
  • +
  • -

#5 aceofspades686  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 6
  • View blog
  • Posts: 334
  • Joined: 08-October 07

Re: VB Quiz using select case and nested if then else

Posted 17 October 2007 - 03:31 PM

Please use code tags to wrap your code in.

First off, where is your case? In a Select Case statement, you need something for it to test, not just the cases to test it with.

Other than that, I can't tell what's going on for two reasons:
  • You cut out so much of your code, I don't know what your Select Case nor the If... Then...Else statements are doing.
  • The lack of indentation is making this incredibly hard to read.

Was This Post Helpful? 0
  • +
  • -

#6 needcode  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 17-October 07

Re: VB Quiz using select case and nested if then else

Posted 17 October 2007 - 06:06 PM

Select Case
Case 0
If cmbAnswer.SelectedIndex = 0 Then
   MessageBox.Show("Correct","Quiz")
Else
   MessageBox.Show("Incorrect","Quiz")
Case 1
If cmbAnswer.SelectedIndex = 1 Then
   MessageBox.Show("Correct","Quiz")
Else
   MessageBox.Show("Incorrect","Quiz")
etc.

Nested If Then Else
If cmbQuestions.SelectedIndex = 0 then
If cmbAnswer.SelectedIndex = 0 then
   MessageBox.Show("Correct", "Quiz")
Else
   MessageBox.Show("Incorrect","Quiz")
If cmbQuestions.SelectedIndex = 1 then
If cmbAnswer.SelectedIndex = 1 then
   MessageBox.Show("Correct", "Quiz")
Else
   MessageBox.Show("Incorrect","Quiz")
End If x 4

This post has been edited by jayman9: 17 October 2007 - 06:36 PM

Was This Post Helpful? 0
  • +
  • -

#7 aceofspades686  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 6
  • View blog
  • Posts: 334
  • Joined: 08-October 07

Re: VB Quiz using select case and nested if then else

Posted 18 October 2007 - 08:21 AM

Okay, now I can sort of see what's going on I think, looking at the nesting was making my head spin with no indentation.

First off, this (which is a piece of what I think you may have been trying for):
Select Case cmbQuestions.SelectedIndex
	Case 0
		If cmbAnswer.SelectedIndex = 0 Then
		   MessageBox.Show("Correct","Quiz")
		Else
		   MessageBox.Show("Incorrect","Quiz")
		End If
End Select

Is effectively the same as this:
If cmbQuestions.SelectedIndex = 0 then
	If cmbAnswer.SelectedIndex = 0 then
		MessageBox.Show("Correct", "Quiz")
	Else
		MessageBox.Show("Incorrect","Quiz")
	End If
End If

So if you have both of these in the same code, then you'll get the messagebox popping up twice each time.

Secondly, at the bottom:
If cmbQuestions.SelectedIndex = 0 then
   If cmbAnswer.SelectedIndex = 0 then
	  MessageBox.Show("Correct", "Quiz")
   Else
	  MessageBox.Show("Incorrect","Quiz")
		 If cmbQuestions.SelectedIndex = 1 then
			If cmbAnswer.SelectedIndex = 1 then
			   MessageBox.Show("Correct", "Quiz")
			Else
			   MessageBox.Show("Incorrect","Quiz")
			End If
		 End If
	  End If
   End If
End If

I have to say that that is honestly the craziest nested If.. Then.... Else cluster I have ever seen, not to mention it won't work. I'll explain it this way. As it stands, this layered conditional will only start cycling through the options if (cmbQuestions.SelectedIndex = 0), well assuming this is true, (cmbQuestions.SelectedIndex = 1) can't be true a few lines down. What you may have been going for was something like this:
If cmbQuestions.SelectedIndex = 0 then
   If cmbAnswer.SelectedIndex = 0 then
	  MessageBox.Show("Correct", "Quiz")
   Else
	  MessageBox.Show("Incorrect","Quiz")
   End If
ElseIf cmbQuestions.SelectedIndex = 1 then
   If cmbAnswer.SelectedIndex = 1 then
	  MessageBox.Show("Correct", "Quiz")
   Else
	  MessageBox.Show("Incorrect","Quiz")
   End If
End If

In this case, if the (cmbQuestion.SelectedIndex = 0) conditional isn't true, then it will check to see if its equal to 1. You could continue in the same manner to add more questions as well. Hope this was of some help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1