From ff9e5827b7a82257c7b831ca50143930ab3c1a06 Mon Sep 17 00:00:00 2001 From: Maxim Slipenko Date: Sun, 22 May 2022 09:04:08 +0300 Subject: [PATCH] =?UTF-8?q?=D1=81=D0=BE=D0=B5=D0=B4=D0=B8=D0=BD=D1=8F?= =?UTF-8?q?=D0=B5=D1=82=20=D1=80=D0=B5=D0=B3=D0=B8=D1=81=D1=82=D1=80=D0=B0?= =?UTF-8?q?=D1=86=D0=B8=D1=8E=20=D1=81=20=D1=84=D0=BE=D1=80=D0=BC=D0=BE?= =?UTF-8?q?=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AuthorizationForm.cs | 9 ++++--- AwesomeEmailExtractor.csproj | 1 + FormManager.cs | 46 ++++++++++++++++++++++++++++++++++++ Program.cs | 4 +--- RegistrationForm.Designer.cs | 8 ++++--- RegistrationForm.cs | 26 +++++++++++++++++--- 6 files changed, 80 insertions(+), 14 deletions(-) create mode 100644 FormManager.cs diff --git a/AuthorizationForm.cs b/AuthorizationForm.cs index ab3a202..d53f6da 100644 --- a/AuthorizationForm.cs +++ b/AuthorizationForm.cs @@ -22,9 +22,9 @@ namespace AwesomeEmailExtractor try { Globals.currentUser = Authorization.Login(entryLogin.Text, entryPassword.Text); - this.Close(); - new MainForm().Show(); + var mainForm = FormManager.Current.CreateForm(); + FormManager.Current.Navigate(this, mainForm); } catch (Exception ex) { @@ -34,9 +34,8 @@ namespace AwesomeEmailExtractor private void registerButton_Click(object sender, EventArgs e) { - this.Close(); - - new RegistrationForm().Show(); + var form = FormManager.Current.CreateForm(); + FormManager.Current.Navigate(this, form); } } } diff --git a/AwesomeEmailExtractor.csproj b/AwesomeEmailExtractor.csproj index 67135ee..e634b53 100644 --- a/AwesomeEmailExtractor.csproj +++ b/AwesomeEmailExtractor.csproj @@ -84,6 +84,7 @@ AuthorizationForm.cs + diff --git a/FormManager.cs b/FormManager.cs new file mode 100644 index 0000000..39a3473 --- /dev/null +++ b/FormManager.cs @@ -0,0 +1,46 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace AwesomeEmailExtractor +{ + public class FormManager : ApplicationContext + { + private void onFormClosed(object sender, EventArgs e) + { + if (Application.OpenForms.Count == 0) + { + if (((Form)sender).Name == currentForm) + ExitThread(); + } + } + + public T CreateForm() where T : Form, new() + { + var ret = new T(); + ret.FormClosed += onFormClosed; + return ret; + } + + public void Navigate(Form from, Form to) + { + currentForm = to.Name; + from.Close(); + to.Show(); + } + + public string currentForm = "AuthorizationForm"; + + private static Lazy _current = new Lazy(); + public static FormManager Current => _current.Value; + + public FormManager() + { + var authorization = CreateForm(); + authorization.Show(); + } + } +} diff --git a/Program.cs b/Program.cs index 9047a40..e40a3e9 100644 --- a/Program.cs +++ b/Program.cs @@ -20,9 +20,7 @@ namespace AwesomeEmailExtractor Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); - - new AuthorizationForm().Show(); - Application.Run(); + Application.Run(FormManager.Current); postMain(); } diff --git a/RegistrationForm.Designer.cs b/RegistrationForm.Designer.cs index fde80ad..7fe7ef6 100644 --- a/RegistrationForm.Designer.cs +++ b/RegistrationForm.Designer.cs @@ -55,6 +55,7 @@ this.entryPassword.Name = "entryPassword"; this.entryPassword.Size = new System.Drawing.Size(195, 20); this.entryPassword.TabIndex = 10; + this.entryPassword.UseSystemPasswordChar = true; // // label1 // @@ -79,9 +80,10 @@ this.registerButton.Location = new System.Drawing.Point(14, 135); this.registerButton.Name = "registerButton"; this.registerButton.Size = new System.Drawing.Size(364, 23); - this.registerButton.TabIndex = 7; + this.registerButton.TabIndex = 6; this.registerButton.Text = "Зарегистрироваться"; this.registerButton.UseVisualStyleBackColor = true; + this.registerButton.Click += new System.EventHandler(this.registerButton_Click); // // loginButton // @@ -89,7 +91,7 @@ this.loginButton.Location = new System.Drawing.Point(14, 187); this.loginButton.Name = "loginButton"; this.loginButton.Size = new System.Drawing.Size(364, 23); - this.loginButton.TabIndex = 6; + this.loginButton.TabIndex = 7; this.loginButton.Text = "Авторизоваться"; this.loginButton.UseVisualStyleBackColor = true; this.loginButton.Click += new System.EventHandler(this.loginButton_Click); @@ -121,7 +123,6 @@ this.label4.Size = new System.Drawing.Size(178, 18); this.label4.TabIndex = 14; this.label4.Text = "Уже зарегистрированы?"; - this.label4.Click += new System.EventHandler(this.label4_Click); // // RegistrationForm // @@ -140,6 +141,7 @@ this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; this.Name = "RegistrationForm"; this.Text = "Зарегистрироваться"; + this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.RegistrationForm_FormClosed); this.ResumeLayout(false); this.PerformLayout(); diff --git a/RegistrationForm.cs b/RegistrationForm.cs index d996d9b..18d62f5 100644 --- a/RegistrationForm.cs +++ b/RegistrationForm.cs @@ -17,16 +17,36 @@ namespace AwesomeEmailExtractor InitializeComponent(); } - private void label4_Click(object sender, EventArgs e) + private void registerButton_Click(object sender, EventArgs e) { + if (!string.Equals(entryPassword.Text, entryRePassword.Text)) + { + MessageBox.Show("Пароли не совпадают!"); + return; + } + try + { + Globals.currentUser = Authorization.Register(entryLogin.Text, entryPassword.Text); + + var form = FormManager.Current.CreateForm(); + FormManager.Current.Navigate(this, form); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message); + } } private void loginButton_Click(object sender, EventArgs e) { - this.Close(); + AuthorizationForm form = FormManager.Current.CreateForm(); + FormManager.Current.Navigate(this, form); + } - new AuthorizationForm().Show(); + private void RegistrationForm_FormClosed(object sender, FormClosedEventArgs e) + { + } } }