MainWindow.xaml.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Navigation;
  14. using System.Windows.Shapes;
  15. using static System.Collections.Specialized.BitVector32;
  16. namespace Control_DB_Railway
  17. {
  18. /// <summary>
  19. /// Логика взаимодействия для MainWindow.xaml
  20. /// </summary>
  21. public partial class MainWindow : Window
  22. {
  23. Entities entities = new Entities();
  24. public MainWindow()
  25. {
  26. InitializeComponent();
  27. // Заполнение ListBox "Станция"
  28. foreach (var station in entities.Stations)
  29. listBox_Station.Items.Add(station);
  30. // Заполнение ListBox "Тип станции"
  31. foreach (var typeStation in entities.TypeStation)
  32. listBox_TypeStation.Items.Add(typeStation);
  33. // Заполнение ListBox "Дорога"
  34. foreach (var road in entities.Roads)
  35. listBox_Road.Items.Add(road);
  36. // Заполнение comboBox_StationLocation "Станция"
  37. foreach (var location in entities.Stations)
  38. comboBox_StationLocation.Items.Add(location.location);
  39. // Заполнение comboBox_TypeRoad "Дорога"
  40. foreach (var typeRoad in entities.Roads)
  41. comboBox_TypeRoad.Items.Add(typeRoad.typeRoad);
  42. // Заполнение comboBox_CategoryQuality "Дорога"
  43. foreach (var categoryQuality in entities.Roads)
  44. comboBox_CategoryQuality.Items.Add(categoryQuality.categoryQuality);
  45. }
  46. /* Отображение информации в ListBox "Станция" */
  47. private void listBox_Station_SelectionChanged(object sender, SelectionChangedEventArgs e)
  48. {
  49. var selectStation = listBox_Station.SelectedItem as Stations;
  50. if (selectStation != null)
  51. {
  52. textBox_Station.Text = selectStation.nameStation;
  53. checkBox_overheadTransition.IsChecked = selectStation.overheadTransition;
  54. comboBox_StationLocation.SelectedItem = (from location in entities.Stations
  55. where location.stationID ==
  56. selectStation.stationID
  57. select location).Single<Stations>();
  58. }
  59. else
  60. {
  61. textBox_Station.Text = "";
  62. checkBox_overheadTransition.IsChecked = false;
  63. comboBox_StationLocation.SelectedIndex = -1;
  64. }
  65. }
  66. // ИСПРАВЛЯТЬ! //
  67. /* Нажатие по кнопке "Добавить/изменить станцию" */
  68. private void button_AddRecord_Station_Click(object sender, RoutedEventArgs e)
  69. {
  70. var station = listBox_Station.SelectedItem as Stations;
  71. if (textBox_Station.Text == "" || comboBox_StationLocation.SelectedIndex == -1)
  72. MessageBox.Show("Заполните все поля!", "Ошибка!", MessageBoxButton.OK,
  73. MessageBoxImage.Error);
  74. else
  75. {
  76. if (station == null)
  77. {
  78. station = new Stations();
  79. entities.Stations.Add(station); // добавляем запись в БД
  80. listBox_Station.Items.Add(station); // добавляем запись в ListBox
  81. }
  82. station.nameStation = textBox_Station.Text;
  83. station.overheadTransition = (bool)checkBox_overheadTransition.IsChecked;
  84. station.location = (comboBox_StationLocation.SelectedItem as Stations).location;
  85. entities.SaveChanges();
  86. listBox_Station.Items.Refresh();
  87. MessageBox.Show("Успешное сохранение/добавление записи!");
  88. }
  89. }
  90. /* Удаление записи "Станции" */
  91. private void button_DeleteRecord_Station_Click(object sender, RoutedEventArgs e)
  92. {
  93. var rezult = MessageBox.Show("Вы уверены, что хотите удалить запись?", "Удаление",
  94. MessageBoxButton.YesNo, MessageBoxImage.Question);
  95. if (rezult == MessageBoxResult.No)
  96. return;
  97. var delete_station = listBox_Station.SelectedItem as Stations;
  98. if (delete_station != null)
  99. {
  100. entities.Stations.Remove(delete_station);
  101. entities.SaveChanges();
  102. textBox_Station.Clear();
  103. listBox_Station.Items.Remove(delete_station);
  104. checkBox_overheadTransition.IsEnabled = false;
  105. comboBox_StationLocation.SelectedIndex = -1;
  106. }
  107. else
  108. MessageBox.Show("Нет удаляемых объектов!", "Ошибка", MessageBoxButton.OK,
  109. MessageBoxImage.Warning);
  110. }
  111. /* Очистка полей "Станции" */
  112. private void button_ClearStation_Click(object sender, RoutedEventArgs e)
  113. {
  114. textBox_Station.Text = "";
  115. checkBox_overheadTransition.IsEnabled = false;
  116. comboBox_StationLocation.SelectedIndex = -1;
  117. listBox_Station.SelectedIndex = -1;
  118. textBox_Station.Focus();
  119. }
  120. /* Отображение информации в ListBox "Тип станции" */
  121. private void listBox_TypeStation_SelectionChanged(object sender, SelectionChangedEventArgs e)
  122. {
  123. var selectTypeStation = listBox_TypeStation.SelectedItem as TypeStation;
  124. if (selectTypeStation != null)
  125. {
  126. textBox_TypeStation.Text = selectTypeStation.nameTypeStation;
  127. checkBox_passagerService.IsChecked = selectTypeStation.passagerService;
  128. }
  129. else
  130. {
  131. textBox_TypeStation.Text = "";
  132. checkBox_passagerService.IsChecked = false;
  133. }
  134. }
  135. /* Добавление/сохранение записи в "Типе станции" */
  136. private void button_AddRecord_TypeStation_Click(object sender, RoutedEventArgs e)
  137. {
  138. var typeStation = listBox_TypeStation.SelectedItem as TypeStation;
  139. if (textBox_TypeStation.Text == "")
  140. MessageBox.Show("Напишите название!", "Ошибка!", MessageBoxButton.OK,
  141. MessageBoxImage.Error);
  142. else
  143. {
  144. if (typeStation == null)
  145. {
  146. typeStation = new TypeStation();
  147. entities.TypeStation.Add(typeStation); // добавляем запись в БД
  148. listBox_TypeStation.Items.Add(typeStation); // добавляем запись в ListBox
  149. }
  150. typeStation.nameTypeStation = textBox_TypeStation.Text;
  151. typeStation.passagerService = (bool)checkBox_passagerService.IsChecked;
  152. entities.SaveChanges();
  153. listBox_TypeStation.Items.Refresh();
  154. MessageBox.Show("Успешное сохранение/добавление записи!");
  155. }
  156. }
  157. /* Удаление записи "Тип станции" */
  158. private void button_DeleteRecord_TypeStation_Click(object sender, RoutedEventArgs e)
  159. {
  160. var rezult = MessageBox.Show("Вы уверены, что хотите удалить запись?", "Удаление",
  161. MessageBoxButton.YesNo, MessageBoxImage.Question);
  162. if (rezult == MessageBoxResult.No)
  163. return;
  164. var delete_typeStation = listBox_TypeStation.SelectedItem as TypeStation;
  165. if (delete_typeStation != null)
  166. {
  167. entities.TypeStation.Remove(delete_typeStation);
  168. entities.SaveChanges();
  169. textBox_TypeStation.Clear();
  170. listBox_TypeStation.Items.Remove(delete_typeStation);
  171. checkBox_passagerService.IsEnabled = false;
  172. }
  173. else
  174. MessageBox.Show("Нет удаляемых объектов!", "Ошибка", MessageBoxButton.OK,
  175. MessageBoxImage.Warning);
  176. }
  177. /* Очистка полей "Станции" */
  178. private void button_ClearTypeStation_Click(object sender, RoutedEventArgs e)
  179. {
  180. textBox_TypeStation.Text = "";
  181. checkBox_passagerService.IsEnabled = false;
  182. listBox_TypeStation.SelectedIndex = -1;
  183. textBox_TypeStation.Focus();
  184. }
  185. /* Отображение данных в ListBox "Дороги" */
  186. private void listBox_Road_SelectionChanged(object sender, SelectionChangedEventArgs e)
  187. {
  188. var selectRoad = listBox_Road.SelectedItem as Roads;
  189. if (selectRoad != null)
  190. {
  191. textBox_NameRoad.Text = selectRoad.nameRoad;
  192. comboBox_TypeRoad.SelectedItem = (from typeRoad in entities.Roads
  193. where typeRoad.roadID ==
  194. selectRoad.roadID
  195. select typeRoad).Single<Roads>();
  196. comboBox_CategoryQuality.SelectedItem = (from categoryQuality in entities.Roads
  197. where categoryQuality.roadID ==
  198. selectRoad.roadID
  199. select categoryQuality).Single<Roads>();
  200. }
  201. else
  202. {
  203. textBox_TypeStation.Text = "";
  204. checkBox_passagerService.IsChecked = false;
  205. }
  206. }
  207. /* Добавление/сохранение записи в "Дорогу" */
  208. private void button_AddRecord_Road_Click(object sender, RoutedEventArgs e)
  209. {
  210. var Road = listBox_Road.SelectedItem as Roads;
  211. if (textBox_NameRoad.Text == "" || comboBox_TypeRoad.SelectedIndex == -1 ||
  212. comboBox_CategoryQuality.SelectedIndex == -1)
  213. MessageBox.Show("Заполните все поля!", "Ошибка!", MessageBoxButton.OK,
  214. MessageBoxImage.Error);
  215. else
  216. {
  217. if (Road == null)
  218. {
  219. Road = new Roads();
  220. entities.Roads.Add(Road); // добавляем запись в БД
  221. listBox_Road.Items.Add(Road); // добавляем запись в ListBox
  222. }
  223. Road.nameRoad = textBox_NameRoad.Text;
  224. Road.typeRoad = (comboBox_TypeRoad.SelectedItem as Roads).typeRoad;
  225. Road.categoryQuality = (comboBox_CategoryQuality.SelectedItem as Roads).categoryQuality;
  226. entities.SaveChanges();
  227. listBox_TypeStation.Items.Refresh();
  228. MessageBox.Show("Успешное сохранение/добавление записи!");
  229. }
  230. }
  231. /* Удаление записи в "Дороге" */
  232. private void button_DeleteRecord_Road_Click(object sender, RoutedEventArgs e)
  233. {
  234. var rezult = MessageBox.Show("Вы уверены, что хотите удалить запись?", "Удаление",
  235. MessageBoxButton.YesNo, MessageBoxImage.Question);
  236. if (rezult == MessageBoxResult.No)
  237. return;
  238. var delete_road = listBox_Road.SelectedItem as Roads;
  239. if (delete_road != null)
  240. {
  241. entities.Roads.Remove(delete_road);
  242. entities.SaveChanges();
  243. textBox_Station.Clear();
  244. listBox_Road.Items.Remove(delete_road);
  245. comboBox_TypeRoad.SelectedIndex = -1;
  246. comboBox_CategoryQuality.SelectedIndex = -1;
  247. }
  248. else
  249. MessageBox.Show("Нет удаляемых объектов!", "Ошибка", MessageBoxButton.OK,
  250. MessageBoxImage.Warning);
  251. }
  252. /* Очистка полей "Дороги" */
  253. private void button_ClearRoad_Click(object sender, RoutedEventArgs e)
  254. {
  255. textBox_NameRoad.Text = "";
  256. comboBox_CategoryQuality.SelectedIndex = -1;
  257. comboBox_TypeRoad.SelectedIndex = -1;
  258. textBox_NameRoad.Focus();
  259. }
  260. }
  261. }