You are here:

C#/examining multiple conditions using a single button

Advertisement


Question
Hello,

I am writing a very simplistic spelling test program for my son in Visual C# 2005 Express Edition beta 2.

I have the program working with this functionality:

There are 20 words in the spelling test.

I have a form with one textbox and one button (named Grade Me).

A message box is displayed asking the user to spell a word. Once the word is typed in and the user clicks the Grade Me button, another message box is displayed telling the user if the word is correct. If the word is not correct, a message box displays saying 'sorry, try again.'

This fuctionality is okay, but if the word is wrong, I would like to tell the user the word has not been spelled correctly and to spell that particular word again.

Here's a sample of code with the way the program works right now: I'll only include code the check two words:

These variables are declared in the Form1 class:

string word1 = "cat";
string word2 = "dog";

The coditional statements are in the button click event:

private void button1_Click(object sender, EventArgs e)//Grade Me button
       {
           
           if (textBox1.Text == word1)
           {
               MessageBox.Show("that's right. spell:   " + word2);
               textBox1.Text = "";
               textBox1.TabStop = true;
               textBox1.TabIndex = 0;
               textBox1.Select();

               
              

               
               
           }
           else if (textBox1.Text == word2)
           {
               MessageBox.Show("that's right. spell:   " + word3);
               textBox1.Text = "";
               textBox1.TabStop = true;
               textBox1.TabIndex = 0;
               textBox1.Select();

             else
           {
               MessageBox.Show("sorry, try again.");
               textBox1.Text = "";
               textBox1.TabStop = true;
               textBox1.TabIndex = 0;
               textBox1.Select();
           }
}


What I would like to do is if the user spelled 'cat' incorrectly, come back and say 'sorry, spell: cat', come back to the textbox and wait for the word to be spelled again. If the word is spelled correctly, then go on to word two and repeat this for the remaining words. But if I try to incorporate this functionality into the button click event, the 'if' statements fire off sequentially and I can't have the user get the message to spell the particular word again, type it and try again. I'm missing something here.

I am a beginning programmer writing this as a learning experiment. I'm sorry this question seemed to drag out but people really get confused when I ask this and I hope I've made the question clear.

Any help you could provide me would be greatly appreciated.

Thank you very much for your time.

Rick Bunnell  

Answer
Hi

Sorry for the delay in my reply!!

I suggest you can store the right answers into array, once the user typed the answers you can do a FOR loop and check, If the type answer not found you can display error message.

Do you want to accept 20 questions first and grade each question? if so, you can store all the typed answer also into a multi dimension array and after clicking the "grade me" button you can compare two and display the errored one.

Hope my answer is clear

Happy Programming!!

-srini

C#

All Answers


Answers by Expert:


Ask Experts

Volunteer


Srini Nagarajan

Expertise

can answer any kind of questions in ASP.NET, C#, VB.NET, ASP, SharePoint 2007, Coldfusion, Powerbuilder 7.00 / 8.00, JAVA servlets, MS SQL 2000 / MSSQL7, Sybase

Experience

Contact me if you need any custom development on ASP.NET, ASP, Coldfusion, Powerbuilder

©2012 About.com, a part of The New York Times Company. All rights reserved.