コード例 #1
0
ファイル: Presenter.cs プロジェクト: HladkyiIvan/VetLearn
        private void AddCategoryDialog(object sender, EventArgs e)
        {
            Form prompt = new Form()
            {
                Width           = 250,
                Height          = 150,
                FormBorderStyle = FormBorderStyle.FixedDialog,
                Text            = "Добавление категории",
                StartPosition   = FormStartPosition.CenterScreen
            };
            Label textLabel = new Label()
            {
                Left = 25, Top = 20, Text = "Введи название категории"
            };
            TextBox textBox = new TextBox()
            {
                Left = 25, Top = 50, Width = 200
            };
            Button confirmation = new Button()
            {
                Text = "Ок", Left = 175, Width = 50, Top = 80, DialogResult = DialogResult.OK
            };

            confirmation.Click += (s, ev) => { prompt.Close(); };
            prompt.Controls.Add(textBox);
            prompt.Controls.Add(confirmation);
            prompt.Controls.Add(textLabel);
            prompt.AcceptButton = confirmation;

            if (prompt.ShowDialog() == DialogResult.OK)
            {
                _categories.Add(new Category(textBox.Text));
                File.WriteAllText(dataPath, JsonConvert.SerializeObject(_categories));
                _startScreen.AdjustCategoriesListToCombobox(textBox.Text, true);
                _addQuestionScreen.AdjustCategoriesListToCombobox(textBox.Text, true);

                MessageBox.Show($"Категория была успешно добавленна");
            }
            else
            {
                MessageBox.Show($"Шото пошло не так, трай эгейн");
                return;
            }
        }
コード例 #2
0
ファイル: Presenter.cs プロジェクト: HladkyiIvan/VetLearn
        public Presenter(IStartScreen startscreen, ILearnScreen learnScreen,
                         IAnswerScreen answerScreen, IAddQuestionScreen addQuestionScreen)
        {
            // Configuration
            _startScreen       = startscreen;
            _learnScreen       = learnScreen;
            _answerScreen      = answerScreen;
            _addQuestionScreen = addQuestionScreen;

            _startScreen.CategoryIsChosen            += ChooseCategory;
            _startScreen.GotoAddCategoryScreen       += AddCategoryDialog;
            _startScreen.GotoDeleteCategoryScreen    += DeleteCategoryDialog;
            _startScreen.GotoAddQuestionScreen       += GotoAddQuestionFromMenuScreen;
            _learnScreen.AnswerBtnClicked            += OpenAnswerScreen;
            _learnScreen.GotoMenuBtnClicked          += GotoMenuFromLearnScreen;
            _learnScreen.GotoNextBtnClicked          += GotoNextQuestion;
            _answerScreen.GotoLearnScreenBtnClicked  += CloseAnswerScreen;
            _addQuestionScreen.GotoMenuBtnClicked    += GotoMenuFromAddQuestionScreen;
            _addQuestionScreen.AddQuestionBtnClicked += AddQuestion;

            // Load resources
            _currentCategory = new Category();
            //
            // Encoding.GetEncoding(1251)
            if (!File.Exists(dataPath))
            {
                var sw = File.CreateText(dataPath);
                sw.Close();
                _categories = new List <Category> ();
            }
            else
            {
                _categories = JsonConvert.DeserializeObject <List <Category> >(File.ReadAllText(dataPath, Encoding.UTF8));
            }

            // Get images from byte arrays
            if (_categories.Any())
            {
                foreach (var category in _categories)
                {
                    _startScreen.AdjustCategoriesListToCombobox(category.Id, true);
                    _addQuestionScreen.AdjustCategoriesListToCombobox(category.Id, true);

                    if (category.Questions.Any())
                    {
                        foreach (var question in category.Questions)
                        {
                            if (string.IsNullOrEmpty(question.ImagePath))
                            {
                                question.Image = Image.FromStream(new MemoryStream(File.ReadAllBytes(defaultPicturePath)));
                                continue;
                            }

                            using (var ms = new MemoryStream(File.ReadAllBytes(question.ImagePath)))
                            {
                                question.Image = Image.FromStream(ms);
                            }
                        }
                    }
                }
            }
        }