Click here to Skip to main content
15,885,173 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Currently in my web page I have one button and nearly 20 text boxes. Each text box has its default value(say Textbox1="1" and Textbox2="2"....Textbox20="20"). if i type 1 in Textbox1(ie.the default value),then i have to show that my "Correct answer =1" and "wrong Answer =0). If i typed all answer right then my "Correct Answer=20" and Wrong Answer=0"..Instead of typing 1 in Texbox1 if i type 5,then that is considered as Wrong answer. when i finish typing in all these 20 textboxes i have to click a button where i have to show the status of correct answer and wrong answer.. i already did this by using "if else"..i'm pasting my code below.. i want to know is there any other way to make this possible ....

in my button click event

C#
      int _correct = 0;
        int _wrong = 0;
        
        
        if (text2.Text == "2")
        {
            _correct++;
        }
        else
        {
            _wrong++;
        }
        if (text3.Text == "5")
        {
            _correct++;
        }
        else
        {
            _wrong++;
        }
.
.
.
.
.

C#
lblCorrect.Visible = true;
           lblCorrect.Text = _correct.ToString();
           lblWrong.Visible = true;
           lblWrong.Text = _wrong.ToString();
Posted
Comments
__TR__ 27-Dec-12 6:12am    
One option would be to calculate Wrong answer as
20 - Correct answer
there by avoiding the else block.

1 solution

you can avoid the else block by using the following approach.

C#
int _correct = 0;
        int _wrong = 20;


        if (text2.Text == "2")
        {
            _correct++;
            _wrong--;
        }
        if (text3.Text == "5")
        {
            _correct++;
        _wrong--;
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900