· 7 years ago · Sep 12, 2018, 10:06 AM
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Data;
5using System.Drawing;
6using System.Linq;
7using System.Text;
8using System.Threading.Tasks;
9using System.Windows.Forms;
10
11namespace SecondApp1 {
12
13 class User {
14 // Data
15
16 public string email;
17 public string password;
18
19
20 // Methods
21 }
22
23 public partial class Form1 : Form {
24
25 // Data -----------------------------------
26 User[] users;
27
28
29 // Methods ---------------------------------
30
31 //Constructor
32 public Form1() {
33 // Creates UI (Buttons, Labels, Textboxes)
34 InitializeComponent();
35
36 ClearErrorMessages();
37
38 // Create test user
39 // Local variable
40
41 // TestUser 1
42 User testUser = new User();
43 testUser.email = "123.123@asd.hu";
44 testUser.password = "12345";
45
46 // TestUser 2
47 User testUser2 = new User();
48 testUser2.email = "321.321@asd.hu";
49 testUser2.password = "12345";
50
51 users = new User[2];
52 users[0] = testUser;
53 users[1] = testUser2;
54
55 }
56 // EventHandler
57 private void loginBTN_Click(object sender, EventArgs e) {
58
59 // Collecting inputs
60 string email = emailTB.Text;
61 string password = passwordTB.Text;
62
63 // Clear error messages
64 ClearErrorMessages();
65
66 // Popup = IntelliSense
67
68 // Email test
69
70 if (email == string.Empty) {
71
72 emailErrorLB.Text = "Email is required!";
73
74 } else if (!email.Contains('@')) {
75
76 emailErrorLB.Text = "Email must contain an @(at) sign!";
77
78 } else if (email.Length < 5) {
79
80 emailErrorLB.Text = "Email is too short!";
81
82 } else if (!email.EndsWith(".hu")) {
83
84 emailErrorLB.Text = "Email must end with (.hu)!";
85
86 }
87
88
89 // Password Test
90
91
92 if (password == string.Empty) {
93 pwErrorLB.Text = "Password is required!";
94
95 } else if (password.Length < 5) {
96
97 pwErrorLB.Text = "Password is too short!";
98
99 }
100
101
102 if (emailErrorLB.Text == string.Empty && pwErrorLB.Text == string.Empty) {
103
104 // Find out the user's name
105
106 // 1st method
107 //string name = email.Substring(0, email.IndexOf('@'));
108
109
110 // 2nd method
111 // alma@gmail.com@asdjaskld@akljdfasj
112 // 0. alma
113 // 1. gmail.com
114
115 //char[] firstInChar = firstName.ToCharArray();
116
117 string fullName = email.Split('@')[0];
118
119
120 string[] nameArray = fullName.Split('.');
121 string firstName = nameArray[0];
122 string lastName = nameArray[1];
123
124
125
126
127 if (nameArray.Length == 2) {
128
129 firstName = nameArray[0];
130 lastName = nameArray[1];
131
132 } else if (nameArray.Length == 1) {
133
134 firstName = nameArray[0];
135 lastName = "";
136
137 }
138
139
140 // Nake first letter capital
141
142 if (nameArray.Length > 1) {
143
144 firstName = firstName[0].ToString().ToUpper() + firstName.Substring(1);
145 }
146 if (nameArray.Length == 2) {
147 lastName = lastName.First().ToString().ToUpper() + lastName.Substring(1);
148 }
149
150
151 // Check if the user is in the users array
152
153 //Code snippet
154
155 int index = 0;
156 bool isFound = false;
157 do {
158
159 if (users[index].email == email && users[index].password == password) {
160
161 isFound = true;
162 }
163
164 index++;
165 } while (!isFound && index < users.Length);
166
167
168 // For tag Ctrl K + Ctrl + C/U
169 // User is in the DB
170
171 if (isFound) {
172
173 MessageBox.Show("Welcome, " + firstName + " " + lastName + "!");
174
175 } else {
176
177 MessageBox.Show("Wrong email or password");
178 }
179
180
181
182
183 }
184
185 }
186
187 private void ClearErrorMessages() {
188 emailErrorLB.Text = "";
189 pwErrorLB.Text = string.Empty;
190 }
191
192
193
194 }
195}