Registration.xaml.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 System.Text.RegularExpressions;
  16. namespace MyTests
  17. {
  18. public partial class Registration : Page
  19. {
  20. public Registration()
  21. {
  22. InitializeComponent();
  23. }
  24. private void Save(object sender, RoutedEventArgs e)
  25. {
  26. try
  27. {
  28. if (Login.Text == "" || Password.Text == "")
  29. MessageBox.Show("Поля не могут быть пустыми.");
  30. else if (RegistrLogin(Login.Text))
  31. MessageBox.Show("Данный логин уже занят");
  32. else
  33. {
  34. try
  35. {
  36. Users newUser = new Users()
  37. {
  38. IdUsers = Connection.db.Users.Select(p => p.IdUsers).DefaultIfEmpty(0).Max() + 1,
  39. Login = Login.Text,
  40. Password = Password.Text,
  41. Email = Email.Text,
  42. Info = Info.Text
  43. };
  44. Connection.db.Users.Add(newUser);
  45. Connection.db.SaveChanges();
  46. MessageBox.Show("Вы успешно зарегистрировались");
  47. Session.UserId = Connection.db.Users.Select(item => item.IdUsers).Max();
  48. MainWindow RegAuth = new MainWindow();
  49. RegAuth.Show();
  50. }
  51. catch (Exception ex)
  52. {
  53. MessageBox.Show(ex.ToString());
  54. }
  55. }
  56. }
  57. catch (Exception ex)
  58. {
  59. MessageBox.Show(ex.ToString());
  60. }
  61. }
  62. public bool RegistrLogin(string Login)
  63. {
  64. //if (Connection.db.Users.Select(item => item.Login).Contains(Login))
  65. // return true;
  66. //else
  67. return false;
  68. }
  69. #region FOCUS
  70. private void LoginFocus(object sender, RoutedEventArgs e)
  71. {
  72. Login.Text = "";
  73. }
  74. private void LoginLostFocus(object sender, RoutedEventArgs e)
  75. {
  76. if (Login.Text.Trim() == "")
  77. Login.Text = "Логин";
  78. }
  79. private void PasswordFocus(object sender, RoutedEventArgs e)
  80. {
  81. Password.Text = "";
  82. }
  83. private void PasswordLostFocus(object sender, RoutedEventArgs e)
  84. {
  85. if (Password.Text.Trim() == "")
  86. Password.Text = "123456";
  87. }
  88. private void EmailNameFocus(object sender, RoutedEventArgs e)
  89. {
  90. Email.Text = "";
  91. }
  92. private void EmailLostFocus(object sender, RoutedEventArgs e)
  93. {
  94. if (Email.Text.Trim() == "")
  95. Email.Text = "Почта";
  96. }
  97. private void InfoNameFocus(object sender, RoutedEventArgs e)
  98. {
  99. Info.Text = "";
  100. }
  101. private void InfoLostFocus(object sender, RoutedEventArgs e)
  102. {
  103. if (Info.Text.Trim() == "")
  104. Info.Text = "Личная информация";
  105. }
  106. #endregion
  107. }
  108. }