87192

Change back color of buttons based on correct/incorrect answer

Question:

I'm having trouble figuring out something.

I want the background color of the buttons to change based on whether the button clicked is the right answer or the wrong answer i.e. if the clicked button is the right answer, the background color changes to YellowGreen and if it is the wrong answer, the background color of all the wrong answers chages to red red and the right answer has a background color of YellowGreen so that the user is able to see if they got the question right or if they got it wrong, and which answer is the correct one.

It's like the first question comes up, there's four buttons, I click on the correct answer, it goes green and the wrong answers go red but then when I click Next and go to the next question, it no longer changes colors. The colors also don't change for some reason if I click one of the incorrect answers, only if I click the correct one first.

The four buttons are named button4, button5, button6 and button7. Example code I have (Have this for all four buttons):

if (qCorrect == 1) { button4.BackColor = Color.YellowGreen; button5.BackColor = Color.Red; button6.BackColor = Color.Red; button7.BackColor = Color.Red; }

On the btnNext_Click I have:

private void btnNext_Click(object sender, EventArgs e) { // swap panels dPanel.Visible = false; qPanel.Visible = true; button4.BackColor = SystemColors.Control; button5.BackColor = SystemColors.Control; button6.BackColor = SystemColors.Control; button7.BackColor = SystemColors.Control; button4.Font = new Font(button4.Font.Name, button4.Font.Size, FontStyle.Regular); button5.Font = new Font(button5.Font.Name, button5.Font.Size, FontStyle.Regular); button6.Font = new Font(button6.Font.Name, button6.Font.Size, FontStyle.Regular); button7.Font = new Font(button7.Font.Name, button7.Font.Size, FontStyle.Regular); if ( qCounter != qSection ) { PickQuestion(); label5.Text = "Question " + qCounter.ToString() + " of " + qSection.ToString(); button4.Enabled = true; button5.Enabled = true; button6.Enabled = true; button7.Enabled = true; } else { btnNext.Enabled = false; label5.Text = "You answered " + qCorrect.ToString() + " questions correctly out of a possible " + qSection.ToString(); } }

Any help with the problem would be much much appreciated.

Answer1:

Okay, I got it! In your event-handlers for your answer-buttons, you are always incrementing qCorrect, but you only check for qCorrect == 1. If you have clicked the first answer, qCorrect will be 1 and when you answer the second time, qCorrect will be 2, but you check for qCorrect == 1. And so on...

When you set breakpoints in the button4_click and button6_click for easy-mode, you will see what I mean! Hope this helps you further.

Recommend

  • VB.NET XML Serialization Repeat third child Element
  • Add image to JPanel within JLabel
  • wx widget won't work (python)
  • Adapt view for screens with lower resolution
  • Call custom dialog in Adapter class
  • change label values when an entry value is changed
  • Android button navigation with onclick listeners
  • Why wont My Variable Appear In The TextBox Using Jquery?
  • Android multiple Button color changes
  • Assign button text dynamically via a loop
  • Vertically stack an array of buttons using auto layout VFL
  • Visual Studio 2012 Not Recognizing Existing Unit Tests
  • Not able to add wp8 platform in cordova app
  • format exception was unhandled input string was not in a correct formats
  • Why do I get wrong results for hmac in Python but not Perl?
  • How to use app.selection[0] for scripts in Adobe InDesign
  • Can we reuse allocated memory
  • Eclipse won't start
  • Why shared pointer assignment does 'swap'?
  • Oops! Google Chrome could not connect to localhost:8085
  • numpy 64bit support in PTVS and numpy System.Int64 casting
  • ng-repeat not working with table but works with list
  • Cordova Apache wrong module path
  • Color time-series based on column values in pandas
  • Geom_jitter colour based on values
  • Correct implementation of List Iterator methods
  • Wrong labels when plotting a time series pandas dataframe with matplotlib
  • CERN ROOT exporting data to plain text
  • Using Sax parsing to edit and write XML in VB6
  • Django simple Captcha “No module named fields” error
  • What and where is mdimport
  • Debug.DrawLine not showing in the GameView
  • Alternative To body {overflow:scroll;} That Will Prevent Page Jostling/Wriggling?
  • Yii2: Config params vs. const/define
  • Why does access(2) check for real and not effective UID?
  • trying to dynamically update Highchart column chart but series undefined
  • Acquiring multiple attributes from .xml file in c#
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • How can I remove ASP.NET Designer.cs files?
  • java string with new operator and a literal