QuestionAddToTestWindow.xaml.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using System;
  2. using System.Linq;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Windows.Input;
  6. namespace MyTests
  7. {
  8. public partial class QuestionAddToTestWindow : Window
  9. {
  10. Tests test;
  11. public QuestionAddToTestWindow(Tests _test)
  12. {
  13. InitializeComponent();
  14. Owner = Application.Current.MainWindow;
  15. test = _test;
  16. }
  17. private void CloseButton_Click(object sender, RoutedEventArgs e)
  18. {
  19. Close();
  20. }
  21. private void AddQuestionButton_Click(object sender, RoutedEventArgs e)
  22. {
  23. if (QuestionBox.Text != "Вопрос" || AnswerBox.Text != "Ответ")
  24. {
  25. try
  26. {
  27. Questions newQuestion = new Questions
  28. {
  29. IdQuestion = cdb.db.Questions.Select(p => p.IdQuestion).DefaultIfEmpty(0).Max() + 1,
  30. IdTest = test.IdTest,
  31. Content = QuestionBox.Text,
  32. Answer = AnswerBox.Text
  33. };
  34. cdb.db.Questions.Add(newQuestion);
  35. cdb.db.SaveChanges();
  36. Close();
  37. }
  38. catch (Exception ex)
  39. {
  40. new ErrorWindow(ex.Message).ShowDialog();
  41. }
  42. }
  43. else
  44. new ErrorWindow("Измените содержимое вопрос или ответа для сохранения.").ShowDialog();
  45. }
  46. private void QuestionBox_PreviewMouseDown(object sender, MouseButtonEventArgs e)
  47. {
  48. ((TextBox)sender).Text = String.Empty;
  49. }
  50. private void QuestionBox_LostFocus(object sender, RoutedEventArgs e)
  51. {
  52. if (QuestionBox.Text.Trim() == "")
  53. QuestionBox.Text = "Вопрос";
  54. }
  55. private void AnswerBox_PreviewMouseDown(object sender, MouseButtonEventArgs e)
  56. {
  57. ((TextBox)sender).Text = String.Empty;
  58. }
  59. private void AnswerBox_LostFocus(object sender, RoutedEventArgs e)
  60. {
  61. if (AnswerBox.Text.Trim() == "")
  62. AnswerBox.Text = "Ответ";
  63. }
  64. private void Border_MouseDown(object sender, MouseButtonEventArgs e)
  65. {
  66. if (e.LeftButton == MouseButtonState.Pressed)
  67. DragMove();
  68. }
  69. }
  70. }