Rop 3K
Rop 3K

Reputation: 23

Comparing string from label and input(textBox) and showing result in label. Plus operation on array

I'm writing a FlashCard app in Windows Form.

Right now I'm trying to do is read word from string array and pass it to label. Then asking user to write the translation of this word. And finally pass the result to label box.

Here is my code:

   public partial class EnglishPolishScreen : Form
{
    //English words array
    string[] words = new string[] { "word1", "word2", "word3", "word4" };
    // meanings words array
    string[] wordB = new string[] { "slowo1", "slowo2", "slowo3", "slowo4" };

    int element = 0;
    Thread thread;


    public EnglishPolishScreen()
    {
        InitializeComponent();
    }

    private void CloseAppEvent(object sender, FormClosingEventArgs e)
    {
        Application.Exit();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        thread = new Thread(Threadd);
        thread.Start();
    }
    private void Threadd()
    {
        englishWord.Text = words[element];
        counterLabel.Text = element + 1 + " of " + words.Length;
        if (answerBox.Text.Equals(wordB[element]))
        {
            resultLabel.Text = "Good";
            element++;
        }
        else
            resultLabel.Text = "Bad";

        if (element == words.Length)
        {
            element = 0;
        }
    }

    private void EnglishPolishScreen_Load(object sender, EventArgs e)
    {
        englishWord.Text = words[element];

    }

Edited
Guys, Why I have to click two times in button to see next item from array? And why I can see "bad" answer straight after I click button? The "Good" answer shows up after second click.

Edited v2.xD Sorted. Anyway is it good way to write code like this? If not how It could look better? Thanks

Regards

Upvotes: 2

Views: 377

Answers (1)

Diode
Diode

Reputation: 68

On button click, it is going through the whole for loop, i.e. the entire list of meanings, you would have to break the for loop as soon as a right match is found. So just use break; after resoultLabel.Text = "Good answer!";. Also as pointed out by @Neil, it is not good to use UI in a separate background thread.

By the way, I am not able to wrap my head around the logic of chances. For giving chances you would have to declare a global variable which would get added/subtracted when the a bad answer is found after Iterating through whole for loop, disallowing any further trial for the word.

Upvotes: 1

Related Questions