Ejemplo n.º 1
0
    public TrainingForm(Form1 form, Button activate_form_button, Action <IncomprehensiblePair> add_incomprehensible) : base(form)
    {
        activate_form_button.Click += (sender, e) =>
        {
            FirstTryAfterEntry(form);
            NextWordOrTextButton.Enabled = false;
            CheckButton.Enabled          = false;
            SetVisibleFormElements();
            if (words_fill_cells.Length > 0 || texts_fill_cells.Length > 0)
            {
                NextWordOrTextButton.Enabled = true;
                CheckButton.Enabled          = true;
                NextWordOrTextButton.PerformClick();
            }
        };
        BackToMainFormButton.Click += (sender, e) => BackToMainForm();

        WordOrTextLabel          = new Label();
        WordOrTextLabel.Text     = word_label;
        WordOrTextLabel.Font     = text_font;
        WordOrTextLabel.Width    = 200;
        WordOrTextLabel.Location = new Point(161, 132);
        WordOrTextLabel.Visible  = false;
        form.Controls.Add(WordOrTextLabel);

        WordOrTextTextBox          = new TextBox();
        WordOrTextTextBox.Font     = text_font;
        WordOrTextTextBox.Width    = 550;
        WordOrTextTextBox.Location = new Point(167, 162);
        WordOrTextTextBox.Visible  = false;
        WordOrTextTextBox.Enabled  = false;
        form.Controls.Add(WordOrTextTextBox);

        TranslationLabel          = new Label();
        TranslationLabel.Text     = translation_label;
        TranslationLabel.Font     = text_font;
        TranslationLabel.Location = new Point(161, 284);
        TranslationLabel.Visible  = false;
        form.Controls.Add(TranslationLabel);

        TranslationTextBox          = new TextBox();
        TranslationTextBox.Font     = text_font;
        TranslationTextBox.Width    = 550;
        TranslationTextBox.Location = new Point(167, 314);
        TranslationTextBox.Visible  = false;
        TranslationTextBox.KeyUp   += (sender, e) =>
        {
            if (CheckButton.Enabled || NextWordOrTextButton.Enabled)
            {
                if (CheckButton.Enabled && e.KeyCode == Keys.Enter)
                {
                    CheckButton.PerformClick();
                }
                else if (NextWordOrTextButton.Enabled && e.KeyCode == Keys.Enter)
                {
                    NextWordOrTextButton.PerformClick();
                }
            }
        };
        form.Controls.Add(TranslationTextBox);

        CheckButton          = new Button();
        CheckButton.Font     = button_font;
        CheckButton.Text     = "Проверить";
        CheckButton.Visible  = false;
        CheckButton.Width    = 150;
        CheckButton.Height   = 50;
        CheckButton.Location = new Point(409, 511);
        CheckButton.Click   += (sender, e) =>
        {
            if (WordOrTextTextBox.Text != string.Empty && TranslationTextBox.Text != string.Empty)
            {
                KeyValuePair <string, LinkedListWithTranslations> pair = new KeyValuePair <string, LinkedListWithTranslations>();
                string word_or_text = null;
                if (type == WordsAndTextsData.WordOrText.Word)
                {
                    word_or_text = language == Language.English ? WordOrTextTextBox.Text : TranslationTextBox.Text;
                }
                else
                {
                    word_or_text = language == Language.English ? WordOrTextTextBox.Text : TranslationTextBox.Text;
                }
                int index = form.Data.GetHash(word_or_text, type);
                // Получение пары слово - переводы
                if (type == WordsAndTextsData.WordOrText.Word && form.Data.Words[index] != null)
                {
                    foreach (var _pair in form.Data.Words[index])
                    {
                        if (_pair.Key.Equals(word_or_text, StringComparison.CurrentCultureIgnoreCase))
                        {
                            pair = _pair;
                            break;
                        }
                    }
                }
                else if (type == WordsAndTextsData.WordOrText.Text && form.Data.Texts[index] != null)
                {
                    foreach (var _pair in form.Data.Texts[index])
                    {
                        if (_pair.Key.Equals(word_or_text, StringComparison.CurrentCultureIgnoreCase))
                        {
                            pair = _pair;
                            break;
                        }
                    }
                }
                // Если пара существует
                if (pair.Key != null && pair.Key != string.Empty)
                {
                    word_or_text     = language == Language.English ? TranslationTextBox.Text : WordOrTextTextBox.Text;
                    ResultLabel.Text = wrong;
                    foreach (var str in pair.Value)
                    {
                        if (str.Equals(word_or_text, StringComparison.CurrentCultureIgnoreCase))
                        {
                            ResultLabel.Text = right;
                            break;
                        }
                    }
                }
                else
                {
                    ResultLabel.Text = wrong;
                }

                // Если дан неправильный ответ, то добавляем слово в список
                if (ResultLabel.Text == wrong)
                {
                    bool have = false;
                    if (form.Incomprehensible != null && form.Incomprehensible.Count > 0)
                    {
                        foreach (var _pair in form.Incomprehensible)
                        {
                            if ((language == Language.English ? WordOrTextTextBox.Text : word_or_text_if_use_russian) == _pair.Key)
                            {
                                have = true;
                                break;
                            }
                        }
                    }
                    if (!have || form.Incomprehensible == null && form.Incomprehensible.Count < 1)
                    {
                        add_incomprehensible(new IncomprehensiblePair(language == Language.English ? WordOrTextTextBox.Text : word_or_text_if_use_russian, type));
                    }
                }
                // Если дан правильный ответ, то удаляем слово из списка
                else
                {
                    if (form.Incomprehensible != null)
                    {
                        foreach (var _pair in form.Incomprehensible)
                        {
                            if ((language == Language.English && _pair.Key == WordOrTextTextBox.Text) ||
                                (language == Language.Russian && _pair.Key == word_or_text_if_use_russian))
                            {
                                form.Incomprehensible.Remove(_pair);
                                break;
                            }
                        }
                    }
                }
            }
            else
            {
                ResultLabel.Text = wrong;
            }
            CheckButton.Enabled = false;
        };
        form.Controls.Add(CheckButton);

        ResultLabel          = new Label();
        ResultLabel.Font     = text_font;
        ResultLabel.Width    = 300;
        ResultLabel.Location = new Point(160, 390);
        ResultLabel.Visible  = false;
        form.Controls.Add(ResultLabel);

        NextWordOrTextButton          = new Button();
        NextWordOrTextButton.Font     = button_font;
        NextWordOrTextButton.Text     = "Далее";
        NextWordOrTextButton.Visible  = false;
        NextWordOrTextButton.Width    = 150;
        NextWordOrTextButton.Height   = 50;
        NextWordOrTextButton.Location = new Point(567, 511);
        NextWordOrTextButton.Click   += (sender, e) =>
        {
            if (words_fill_cells.Length == 0 && texts_fill_cells.Length == 0)
            {
                return;
            }
            Random rand = new Random();
            word_or_text_if_use_russian = null;

            ResultLabel.Text        = string.Empty;
            TranslationTextBox.Text = string.Empty;
            CheckButton.Enabled     = true;

            if (words_fill_cells.Length != 0 || texts_fill_cells.Length != 0 || form.Incomprehensible != null)
            {
                int rand_num = -1;
                if (words_fill_cells.Length != 0 && texts_fill_cells.Length != 0 && (form.Incomprehensible == null || form.Incomprehensible.Count < 1))
                {
                    rand_num = rand.Next(0, 2);
                }
                else if (words_fill_cells.Length != 0 && texts_fill_cells.Length != 0 && form.Incomprehensible != null && form.Incomprehensible.Count > 0)
                {
                    rand_num = rand.Next(0, 4);                                                                                                                                        // повышенный шанс
                }
                else if (form.Incomprehensible != null && form.Incomprehensible.Count > 0)
                {
                    rand_num = 2;
                }
                else if (words_fill_cells.Length != 0)
                {
                    rand_num = 0;
                }
                else if (texts_fill_cells.Length != 0)
                {
                    rand_num = 1;
                }
                // Выбор слова или предложения
                if (rand_num < 2)
                {
                    LinkedList <KeyValuePair <string, LinkedListWithTranslations> > pair_list = null;
                    if (rand_num == 0)
                    {
                        WordOrTextLabel.Text = word_label;
                        type      = WordsAndTextsData.WordOrText.Word;
                        pair_list = form.Data.Words[words_fill_cells[rand.Next(0, words_fill_cells.Length)]];
                    }
                    else
                    {
                        WordOrTextLabel.Text = text_label;
                        type      = WordsAndTextsData.WordOrText.Text;
                        pair_list = form.Data.Texts[texts_fill_cells[rand.Next(0, texts_fill_cells.Length)]];
                    }
                    KeyValuePair <string, LinkedListWithTranslations> pair = pair_list.ElementAt(rand.Next(0, pair_list.Count));

                    // Проверка было ли это слово/предложение ранее
                    if ((rand_num == 0 ? words_was.Count : texts_was.Count) > 0)
                    {
                        foreach (var str in rand_num == 0 ? words_was : texts_was)
                        {
                            if (pair.Key.Equals(str, StringComparison.CurrentCultureIgnoreCase))
                            {
                                NextWordOrTextButton.PerformClick();
                                return;
                            }
                        }
                    }

                    // Устанавливаем в WordOrTextTextBox английское слово/предложение
                    if (rand.Next(0, 2) == 0)
                    {
                        language = Language.English;
                        WordOrTextTextBox.Text = pair.Key;
                    }
                    // Устанавливаем в WordOrTextTextBox русское слово/предложение
                    else
                    {
                        language = Language.Russian;
                        word_or_text_if_use_russian = pair.Key;
                        WordOrTextTextBox.Text      = pair.Value.ElementAt(rand.Next(0, pair.Value.Count));
                    }
                    // Добавление слова/предложения в список исключений
                    if ((rand_num == 0 ? words_was_max_count : texts_was_max_count) > 0)
                    {
                        if (rand_num == 0)
                        {
                            // Если не заполнен, то продолжаем заполнение
                            if (words_was.Count < words_was_max_count)
                            {
                                words_was.AddLast(pair.Key);
                            }
                            // Иначе удаляем первое значение из очереди и добавляем слово в конец списка
                            else
                            {
                                words_was.RemoveFirst();
                                words_was.AddLast(pair.Key);
                            }
                        }
                        else
                        {
                            // Если не заполнен, то продолжаем заполнение
                            if (texts_was.Count < texts_was_max_count)
                            {
                                texts_was.AddLast(pair.Key);
                            }
                            // Иначе удаляем первое значение из очереди и добавляем слово в конец списка
                            else
                            {
                                texts_was.RemoveFirst();
                                texts_was.AddLast(pair.Key);
                            }
                        }
                    }
                }

                // Выбор из списка плохо усвоенных слов или предложений
                else
                {
                    type = form.Incomprehensible.First.Value.Value;
                    WordOrTextLabel.Text = type == WordsAndTextsData.WordOrText.Word ? word_label : text_label;
                    // Устанавливаем в WordOrTextTextBox английское слово/предложение
                    if (rand.Next(0, 2) == 0)
                    {
                        language = Language.English;
                        WordOrTextTextBox.Text = form.Incomprehensible.First.Value.Key;
                    }
                    // Устанавливаем в WordOrTextTextBox русское слово/предложение
                    else
                    {
                        language = Language.Russian;
                        word_or_text_if_use_russian = form.Incomprehensible.First.Value.Key;
                        LinkedListWithTranslations translations_list = form.Data.GetWordOrTextTranslation(word_or_text_if_use_russian, form.Incomprehensible.First.Value.Value);
                        WordOrTextTextBox.Text = translations_list.ElementAt(rand.Next(0, translations_list.Count));
                    }
                }
            }
        };
        form.Controls.Add(NextWordOrTextButton);

        form.DictionaryChangesObserver.WordOrTextAddedEvent += (sender, e) =>
        {
            if (e.Type == TypeChanges.WordAdded)
            {
                was_changes_in_words = true;
            }
            if (e.Type == TypeChanges.TextAdded)
            {
                was_changes_in_texts = true;
            }
        };

        form.DictionaryChangesObserver.WordOrTextDeletedEvent += (sender, e) =>
        {
            if (e.Type == TypeChanges.WordDeleted)
            {
                was_changes_in_words = true;
            }
            if (e.Type == TypeChanges.TextDeleted)
            {
                was_changes_in_texts = true;
            }
        };
    }
    public AddWordsOrTextForms(Form1 form, Button activate_form_button, Action create_data, AddWordsOrTextFormsType form_type) : base(form)
    {
        activate_form_button.Click += (sender, e) => SetVisibleFormElements();
        BackToMainFormButton.Click += (sender, e) => BackToMainForm();

        WordOrTextLabel = new Label();
        if (form_type == AddWordsOrTextFormsType.AddWordForm)
        {
            WordOrTextLabel.Text = word_label;
        }
        else if (form_type == AddWordsOrTextFormsType.AddTextForm)
        {
            WordOrTextLabel.Text = text_label;
        }
        WordOrTextLabel.Font     = text_font;
        WordOrTextLabel.Width    = 200;
        WordOrTextLabel.Location = new Point(161, 132);
        WordOrTextLabel.Visible  = false;
        form.Controls.Add(WordOrTextLabel);

        WordOrTextTextBox          = new TextBox();
        WordOrTextTextBox.Font     = text_font;
        WordOrTextTextBox.Width    = 550;
        WordOrTextTextBox.Location = new Point(167, 162);
        WordOrTextTextBox.Visible  = false;
        form.Controls.Add(WordOrTextTextBox);

        /*==================== TranslationPanel Elements ====================*/

        TranslationPanel            = new Panel();
        TranslationPanel.Height     = 226;
        TranslationPanel.Width      = 885;
        TranslationPanel.Location   = new Point(0, 284);
        TranslationPanel.AutoScroll = true;
        TranslationPanel.Visible    = false;
        form.Controls.Add(TranslationPanel);

        TranslationLabel          = new Label();
        TranslationLabel.Text     = translation_label;
        TranslationLabel.Font     = text_font;
        TranslationLabel.Location = new Point(161, 0);
        TranslationPanel.Controls.Add(TranslationLabel);

        TranslationTextBoxes = new List <TextBox>();
        TranslationTextBoxes.Add(new TextBox());
        TranslationTextBoxes[0].Font     = text_font;
        TranslationTextBoxes[0].Width    = 550;
        TranslationTextBoxes[0].Location = new Point(167, 30);
        TranslationPanel.Controls.Add(TranslationTextBoxes[0]);

        AddTranslation          = new Button();
        AddTranslation.Font     = text_font;
        AddTranslation.Text     = "+";
        AddTranslation.Width    = 34;
        AddTranslation.Height   = 34;
        AddTranslation.Location = new Point(726, 29);
        AddTranslation.Click   += (sender, e) =>
        {
            // Если свободных полей нет, то создаем новое
            if (translation_text_boxes_visible == TranslationTextBoxes.Count)
            {
                int text_box_y_pos = TranslationTextBoxes[translation_text_boxes_visible - 1].Location.Y;
                TranslationTextBoxes.Add(new TextBox());
                TranslationTextBoxes[translation_text_boxes_visible].Font     = text_font;
                TranslationTextBoxes[translation_text_boxes_visible].Width    = 550;
                TranslationTextBoxes[translation_text_boxes_visible].Location = new Point(167, text_box_y_pos + 42);
                TranslationPanel.Controls.Add(TranslationTextBoxes[translation_text_boxes_visible]);
                ++translation_text_boxes_visible;
            }
            // Если имеются свободные поля, но они невидимы, то делаем их видимыми
            else if (translation_text_boxes_visible < TranslationTextBoxes.Count)
            {
                TranslationTextBoxes[translation_text_boxes_visible++].Visible = true;
            }
        };
        TranslationPanel.Controls.Add(AddTranslation);

        /*===================================================================*/

        AddWordOrTextButton          = new Button();
        AddWordOrTextButton.Font     = button_font;
        AddWordOrTextButton.Width    = 220;
        AddWordOrTextButton.Height   = 50;
        AddWordOrTextButton.Location = new Point(497, 511);
        if (form_type == AddWordsOrTextFormsType.AddWordForm)
        {
            AddWordOrTextButton.Text = "Добавить слово";
        }
        else if (form_type == AddWordsOrTextFormsType.AddTextForm)
        {
            AddWordOrTextButton.Text = "Добавить текст";
        }
        AddWordOrTextButton.Visible = false;
        AddWordOrTextButton.Click  += (sender, e) =>
        {
            if (WordOrTextTextBox.Text != string.Empty)
            {
                WordsAndTextsData.WordOrText type = form_type == AddWordsOrTextFormsType.AddWordForm ? WordsAndTextsData.WordOrText.Word : WordsAndTextsData.WordOrText.Text;

                // Проверка, содержат ли поля переводы
                int count_text_boxes_with_text = 0;
                for (int i = 0; i < translation_text_boxes_visible; ++i)
                {
                    if (TranslationTextBoxes[i].Text != string.Empty)
                    {
                        ++count_text_boxes_with_text;
                    }
                }

                bool was_added_new_word_or_text = false;

                if (count_text_boxes_with_text > 0)
                {
                    WordOrTextTextBox.Text = WordOrTextTextBox.Text.Replace('’', '\'');
                    if (form.Data == null)
                    {
                        create_data();
                    }
                    if (translation_text_boxes_visible == 1)
                    {
                        was_added_new_word_or_text = form.Data.AddWordOrText(type, WordOrTextTextBox.Text, TranslationTextBoxes[0].Text);
                    }
                    else if (translation_text_boxes_visible == 2)
                    {
                        was_added_new_word_or_text = form.Data.AddWordOrText(type, WordOrTextTextBox.Text,
                                                                             TranslationTextBoxes[0].Text, TranslationTextBoxes[1].Text);
                    }
                    else if (translation_text_boxes_visible == 3)
                    {
                        was_added_new_word_or_text = form.Data.AddWordOrText(type, WordOrTextTextBox.Text,
                                                                             TranslationTextBoxes[0].Text, TranslationTextBoxes[1].Text,
                                                                             TranslationTextBoxes[2].Text);
                    }
                    else if (translation_text_boxes_visible == 4)
                    {
                        was_added_new_word_or_text = form.Data.AddWordOrText(type, WordOrTextTextBox.Text,
                                                                             TranslationTextBoxes[0].Text, TranslationTextBoxes[1].Text,
                                                                             TranslationTextBoxes[2].Text, TranslationTextBoxes[3].Text);
                    }
                    else if (translation_text_boxes_visible == 5)
                    {
                        was_added_new_word_or_text = form.Data.AddWordOrText(type, WordOrTextTextBox.Text,
                                                                             TranslationTextBoxes[0].Text, TranslationTextBoxes[1].Text, TranslationTextBoxes[2].Text,
                                                                             TranslationTextBoxes[3].Text, TranslationTextBoxes[4].Text);
                    }
                    else
                    {
                        string[] translations = new string[translation_text_boxes_visible];
                        for (int i = 0; i < translation_text_boxes_visible; ++i)
                        {
                            translations[i] = TranslationTextBoxes[i].Text;
                        }
                        was_added_new_word_or_text = form.Data.AddWordOrText(type, WordOrTextTextBox.Text, translations);
                    }

                    if (was_added_new_word_or_text)
                    {
                        AddWordOrTextEvent(this, new Changes(WordOrTextTextBox.Text, type == WordsAndTextsData.WordOrText.Word ? TypeChanges.WordAdded : TypeChanges.TextAdded));
                    }

                    // Очистка полей
                    ClearButton.PerformClick();
                    MessageBox.Show(type == WordsAndTextsData.WordOrText.Word ? word_added_to_dictionary : text_added_to_dictionary);
                }

                else
                {
                    MessageBox.Show(translation_text_boxes_visible > 1 ? fields_dont_have_translation : field_dont_have_translation, error);
                }
            }
            else
            {
                MessageBox.Show(form_type == AddWordsOrTextFormsType.AddWordForm ? field_dont_have_word : field_dont_have_text, error);
            }
        };
        form.Controls.Add(AddWordOrTextButton);

        ClearButton          = new Button();
        ClearButton.Font     = button_font;
        ClearButton.Width    = 150;
        ClearButton.Height   = 50;
        ClearButton.Location = new Point(339, 511);
        ClearButton.Text     = "Очистить";
        ClearButton.Visible  = false;
        ClearButton.Click   += (sender, e) =>
        {
            WordOrTextTextBox.Text       = string.Empty;
            TranslationTextBoxes[0].Text = string.Empty;
            for (int i = 1; i < translation_text_boxes_visible; ++i)
            {
                TranslationTextBoxes[i].Text    = string.Empty;
                TranslationTextBoxes[i].Visible = false;
            }
            translation_text_boxes_visible = 1;
        };
        form.Controls.Add(ClearButton);
    }