PageUpdADD.xaml.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using practica12.Classs;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Data;
  11. using System.Windows.Documents;
  12. using System.Windows.Input;
  13. using System.Windows.Media;
  14. using System.Windows.Media.Imaging;
  15. using System.Windows.Navigation;
  16. using System.Windows.Shapes;
  17. namespace practica12.Pages
  18. {
  19. /// <summary>
  20. /// Логика взаимодействия для PageUpdADD.xaml
  21. /// </summary>
  22. public partial class PageUpdADD : Page
  23. {
  24. Hotel hotel;
  25. bool a;
  26. public PageUpdADD()
  27. {
  28. InitializeComponent();
  29. a = true;
  30. cbCountry.ItemsSource = Base.ep.Country.ToList();
  31. cbCountry.SelectedValuePath = "Code";
  32. cbCountry.DisplayMemberPath = "Name";
  33. cbCountry.SelectedIndex = 0;
  34. }
  35. public PageUpdADD(Hotel hotel)
  36. {
  37. InitializeComponent();
  38. this.hotel = hotel;
  39. tbName.Text = hotel.Name;
  40. cbCountry.ItemsSource = Base.ep.Country.ToList();
  41. cbCountry.SelectedValuePath = "Code";
  42. cbCountry.DisplayMemberPath = "Name";
  43. cbCountry.SelectedIndex = 0;
  44. tbCountOfStars.Text = Convert.ToString(hotel.CountOfStars);
  45. cbCountry.SelectedValue = hotel.CountryCode;
  46. tbDescription.Text = hotel.Description;
  47. a = false;
  48. }
  49. private void btnAddUpd_Click(object sender, RoutedEventArgs e)
  50. {
  51. if (tbName.Text != "" && tbCountOfStars.Text != "" && tbDescription.Text != "" && cbCountry.SelectedIndex != -1)
  52. {
  53. if (a == true)
  54. {
  55. hotel = new Hotel();
  56. }
  57. hotel.Name = tbName.Text;
  58. Regex r1 = new Regex("^[0-5]{1}$");
  59. if (r1.IsMatch(tbCountOfStars.Text) == true)
  60. {
  61. hotel.CountOfStars = Convert.ToInt32(tbCountOfStars.Text);
  62. hotel.CountryCode = Convert.ToString(cbCountry.SelectedValue);
  63. hotel.Description = tbDescription.Text;
  64. if (a == true)
  65. {
  66. Base.ep.Hotel.Add(hotel);
  67. }
  68. Base.ep.SaveChanges();
  69. if (a == true)
  70. {
  71. MessageBox.Show("Запись добавлена");
  72. }
  73. else
  74. {
  75. MessageBox.Show("Запись изменена");
  76. }
  77. FrameClass.MainFrame.Navigate(new HotelPage());
  78. }
  79. else
  80. {
  81. MessageBox.Show("Звезды должны находится в диапазоне от 0 до 5");
  82. }
  83. }
  84. else
  85. {
  86. MessageBox.Show("Все поля должны быть заполнены ");
  87. }
  88. }
  89. private void btnBack_Click(object sender, RoutedEventArgs e)
  90. {
  91. FrameClass.MainFrame.Navigate(new HotelPage());
  92. }
  93. }
  94. }