Пример #1
0
        private void button4_Click(object sender, EventArgs e) //New list - button
        {
            string localPath     = WordList.localPath;
            string fileNameInput = Interaction.InputBox("Enter the name of the new file.\n " + "Exclude the file type.", "New list", "", -1, -1);

            if (fileNameInput == "" || fileNameInput == " ")
            {
                MessageBox.Show("Invalid input. Filename can't be empty.", "Invalid input", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                string languagesInput = Interaction.InputBox($"Enter which languages to be in \"{fileNameInput}\"." +
                                                             $"\nSeparate the languages with semicolon.", "Languages", "", -1, -1);
                if (languagesInput == "" || languagesInput == " ")
                {
                    MessageBox.Show("Invalid input. Languages needs to be added.", "Invalid input", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    try
                    {
                        var fileCreated = File.Create(Path.Combine(localPath, fileNameInput + ".dat"));
                        fileCreated.Close();
                        MessageBox.Show(fileNameInput + " created successfully.", "New list created");

                        File.WriteAllText(Path.Combine(localPath, fileNameInput + ".dat"), languagesInput);
                    }
                    catch (Exception ee)
                    {
                        MessageBox.Show(ee.Message, "Oops!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }

                LoadWordList wordList      = new LoadWordList(WordList.LoadList);
                WordList     wordList1     = wordList.Invoke(fileNameInput);
                string[]     languages     = wordList1.Languages;
                bool         areWordsAdded = false;

                while (true)
                {
                    string[] translations     = new string[wordList1.Languages.Length];
                    string   firstTranslation = translations[0] = Interaction.InputBox($"Type a word in {wordList1.Languages[0]}");

                    if (firstTranslation == string.Empty)
                    {
                        break;
                    }

                    for (int i = 1; i < translations.Length; i++)
                    {
                        string wordsToTranslate;
                        do
                        {
                            wordsToTranslate = Interaction.InputBox($"Translate {firstTranslation} to {wordList1.Languages[i]}");
                        } while (wordsToTranslate == string.Empty);

                        translations[i] = wordsToTranslate;
                    }
                    wordList1.Add(translations);
                    areWordsAdded = true;
                }
                if (areWordsAdded)
                {
                    wordList1.Save();
                }
                label1.Text = "Number of words: " + wordList1.Count().ToString();
            }
        }