示例#1
0
        private void OnEdit(object sender, EventArgs e)
        {
            // Check whether there is at least one selected item in the list
            if (View.ListView.SelectedItems.Count == 0)
            {
                return;
            }

            // If there are several selected items, take the first
            var  selectedIndex = View.ListView.SelectedIndices[0];
            Word word          = (Word)View.ListView.Items[selectedIndex].Tag;

            var wordPresenter = new WordPresenter(Model, new WordForm());

            wordPresenter.View.EnglishWord = word.EnglishWord;
            wordPresenter.View.Translation = word.Translation;

            IEnumerable <Word> words = Model.WordRepository.GetAll();

            if (((Form)wordPresenter.View).ShowDialog() == DialogResult.OK)
            {
                if (words.Any(w => w.EnglishWord.ToUpper() == wordPresenter.View.EnglishWord.ToUpper() &&
                              wordPresenter.View.EnglishWord.ToUpper() != word.EnglishWord.ToUpper()))
                {
                    MessageBox.Show("Such word is already in the vocabulary.", "Something is wrong", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                try
                {
                    word.EnglishWord = wordPresenter.View.EnglishWord;
                    word.Translation = wordPresenter.View.Translation;
                    word.Known       = false;

                    Model.WordRepository.Update(word);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.InnerException.Message);
                    MessageBox.Show(" There is an error by editing a word.", "Something is wrong", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

                UpdateView();
            }
        }
示例#2
0
        private void OnAdd(object sender, EventArgs e)
        {
            IEnumerable <Word> words = Model.WordRepository.GetAll();
            int count = Model.WordRepository.GetAll().ToList().Count;

            var wordPresenter = new WordPresenter(Model, new WordForm());

            wordPresenter.View.EnglishWord = View.EnglishWord;

            if (((Form)wordPresenter.View).ShowDialog() == DialogResult.OK)
            {
                if (words.Any(w => w.EnglishWord.ToUpper() == wordPresenter.View.EnglishWord.ToUpper()))
                {
                    MessageBox.Show("Such word is already in the vocabulary.", "Something is wrong", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                try
                {
                    Model.WordRepository.Add(new Word()
                    {
                        Id          = 0,
                        EnglishWord = wordPresenter.View.EnglishWord,
                        Translation = wordPresenter.View.Translation,
                        Known       = false
                    });

                    count++;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.InnerException.Message);
                    MessageBox.Show("There is an error by adding new word.", "Something is wrong", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

                if (count == 6000)
                {
                    MessageBox.Show("There are 6000 words in a dictionary");
                }

                UpdateView();
            }
        }