MainWindow.xaml.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Data;
  10. using System.Windows.Documents;
  11. using System.Windows.Input;
  12. using System.Windows.Media;
  13. using System.Windows.Media.Imaging;
  14. using System.Windows.Navigation;
  15. using System.Windows.Shapes;
  16. using System.Data.SqlClient;
  17. using System.Data;
  18. namespace HotelCalifornia
  19. {
  20. /// <summary>
  21. /// Логика взаимодействия для MainWindow.xaml
  22. /// </summary>
  23. public partial class MainWindow : Window
  24. {
  25. public MainWindow()
  26. {
  27. InitializeComponent();
  28. }
  29. SqlConnection con = new SqlConnection("Data Source=localhost;Initial Catalog=kursah;Integrated Security=True");
  30. //Перетаскивание окна
  31. private void Grid_MouseDown(object sender, MouseButtonEventArgs e)
  32. {
  33. DragMove();
  34. }
  35. //Выход из приложения
  36. private void Close(object sender, RoutedEventArgs e)
  37. {
  38. MessageBoxResult result = MessageBox.Show("Вы хотите выйти из приложения?", "Предупреждение", MessageBoxButton.YesNo, MessageBoxImage.Question);
  39. switch (result)
  40. {
  41. case MessageBoxResult.Yes:
  42. Application.Current.Shutdown();
  43. break;
  44. case MessageBoxResult.No:
  45. break;
  46. }
  47. }
  48. //Авторизация пользователя
  49. private void Vhod(object sender, RoutedEventArgs e)
  50. {
  51. try
  52. {
  53. if (logintxt.Text == "" || passwordtxt.Password == "")
  54. {
  55. MessageBox.Show("Поля не могут быть пустыми!", "Предупреждение", MessageBoxButton.OK, MessageBoxImage.Information);
  56. }
  57. else
  58. {
  59. con.Open();
  60. SqlCommand cmd = new SqlCommand("Select * from Administrator where Login ='" + logintxt.Text + "' and Password ='" + passwordtxt.Password + "'", con);
  61. cmd.CommandType = CommandType.Text;
  62. SqlDataAdapter adapter = new SqlDataAdapter();
  63. adapter.SelectCommand = cmd;
  64. DataSet dataSet = new DataSet();
  65. adapter.Fill(dataSet);
  66. if (dataSet.Tables[0].Rows.Count > 0)
  67. {
  68. Variant variant = new Variant();
  69. string username = dataSet.Tables[0].Rows[0]["ID_Administrator"].ToString();
  70. variant.idadmintxt.Text = username;
  71. con.Close();
  72. MessageBox.Show("Добро пожаловать!", "Вход", MessageBoxButton.OK, MessageBoxImage.Information);
  73. variant.Show();
  74. this.Close();
  75. }
  76. else
  77. {
  78. con.Close();
  79. MessageBox.Show("Такого Администратора нет в системе!", "Вход", MessageBoxButton.OK, MessageBoxImage.Information);
  80. }
  81. }
  82. }
  83. catch (Exception ex)
  84. {
  85. MessageBox.Show("Возникла ошибка! " + ex.ToString(),"Ошибка",MessageBoxButton.OK, MessageBoxImage.Error);
  86. }
  87. }
  88. //Запрет пробела
  89. private void logintxt_PreviewKeyDown(object sender, KeyEventArgs e)
  90. {
  91. if (e.Key == Key.Space)
  92. {
  93. e.Handled = true;
  94. }
  95. }
  96. //Запрет пробела
  97. private void passwordtxt_PreviewKeyDown(object sender, KeyEventArgs e)
  98. {
  99. if (e.Key == Key.Space)
  100. {
  101. e.Handled = true;
  102. }
  103. }
  104. //Определенные символы
  105. private void logintxt_TextChanged(object sender, TextChangedEventArgs e)
  106. {
  107. if (sender is TextBox textBox)
  108. {
  109. textBox.Text = new string
  110. (textBox.Text.Where(ch => (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9')).ToArray());
  111. }
  112. }
  113. //Определенные символы
  114. private void passwordtxt_PreviewTextInput(object sender, TextCompositionEventArgs e)
  115. {
  116. bool a = new Regex("[^A-Z]+").IsMatch(e.Text);
  117. bool b = new Regex("[^a-z]+").IsMatch(e.Text);
  118. bool c = new Regex("[^0-9]+").IsMatch(e.Text);
  119. e.Handled = a && b && c;
  120. }
  121. private void WindMin_Click(object sender, RoutedEventArgs e)
  122. {
  123. this.WindowState = WindowState.Minimized;
  124. }
  125. }
  126. }