T
T
turchik28082020-02-13 20:31:58
C++ / C#
turchik2808, 2020-02-13 20:31:58

How to add a word to the dictionary using button1?

Tell me, I am looking for anagrams in the dictionary, there are a certain number of words in this dictionary. When the program does not find this word, the text "The word was not found in the dictionary" is displayed, how to do so, when this word was not found, it could be added by pressing the "Add" button - button1_Click? Below is the anagram code if needed

private void FindWord_Click(object sender, EventArgs e)
        {
            var dict = new Dictionary<string, string>();
            foreach (string line in File.ReadLines(@"1.txt"))
            {
                dict.Add(line, string.Concat(line.ToLower().OrderBy(c => c))); // Каждое слово из файла приводится к нижнему регистру и сортируется (по буквам) в алфавитном порядке
            }
            string word = string.Concat(textBox1.Text.ToLower().OrderBy(c => c));
            var valStrings = dict.Where(w => w.Value == word).Select(x => x.Key);
            richTextBox1.Lines = valStrings.ToArray();

            if (valStrings == null || !valStrings.Any()) // проверка слов
            {
                richTextBox1.Text = richTextBox1.Text + "Слово не было найдено в словаре.";
            }
        }
private void button1_Click(object sender, EventArgs e)
        {

        }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Ananiev, 2020-02-13
@SaNNy32

The dict variable must be made a private member of the class and in button1_Click it will be possible to add it to the dictionary.

F
fan92rus, 2020-02-14
@fan92rus

Dictionary<string,string> dict = new Dictionary<string, string>();

And this must be taken out of the method.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question