示例#1
0
        private void btnLogin_Click(object sender, EventArgs e)
        {
            string       inputuserID, inputPassword;
            bool         flag     = false;
            int          foundSub = 0;
            FileStream   outFile  = new FileStream(FILENAME2, FileMode.Open, FileAccess.Write);
            StreamWriter writer   = new StreamWriter(outFile);

            inputuserID   = txtUserID.Text;
            inputPassword = txtPassword.Text;

            if (inputuserID == ADMIN && inputPassword == ADMINPASSWORD)
            {
                frmAdmin admin = new frmAdmin();
                admin.Show();
                this.Hide();
            }
            else
            {
                for (int y = 0; y < arraySize; ++y)
                {
                    if (inputuserID == userID[y] && inputPassword == password[y])
                    {
                        flag     = true;
                        foundSub = y;
                    }
                }

                if (flag)
                {
                    writer.WriteLine(Convert.ToString(userNumber[foundSub]) + DELIM + userID[foundSub]);
                    writer.Close();
                    outFile.Close();
                    frmBuild frm = new frmBuild();
                    frm.Show();
                    this.Hide();
                }
                else
                {
                    MessageBox.Show("Incorrect User ID or Password.  Please try again.");

                    writer.Close();
                    outFile.Close();

                    txtUserID.Text   = null;
                    txtPassword.Text = null;

                    txtUserID.Focus();
                }
            }
        }
        private void btnCreateAccount_Click(object sender, EventArgs e)
        {
            // Declare bool variables for validating
            bool emailIsValid = false;
            bool passIsValid  = false;
            bool firstIsValid = false;
            bool lastIsValid  = false;

            // If-else statements to check for empty textboxes
            if (txtFirstName.Text.Equals("First"))
            {
                txtFirstName.Focus();
                lblFirstError.Text    = "You can't leave this empty.";
                lblFirstError.Visible = true;
            }
            else if (txtLastName.Text.Equals("Last"))
            {
                txtLastName.Focus();
                lblLastError.Text    = "You can't leave this empty.";
                lblLastError.Visible = true;
            }
            else if (txtEmail.Text.Equals(""))
            {
                txtEmail.Focus();
                lblEmailError.Text    = "You can't leave this empty.";
                lblEmailError.Visible = true;
            }
            else if (txtPass1.Text.Equals(""))
            {
                txtPass1.Focus();
                lblPassError1.Text    = "You can't leave this empty.";
                lblPassError1.Visible = true;
            }
            else if (txtPass2.Text.Equals(""))
            {
                txtPass2.Focus();
                lblPassError2.Text    = "You can't leave this empty.";
                lblPassError2.Visible = true;
            }
            else
            {
                StreamReader reader = new StreamReader(inFile);
                string       recordIn;
                string[]     fields;
                string       fieldEmail;
                bool         foundEmail  = false;
                int          numAccounts = 0;

                while (reader.ReadLine() != null)
                {
                    ++numAccounts;  // counting lines in text file
                }
                reader.Close();
                inFile.Close();

                userEmail = new string[numAccounts];

                StreamReader reader2 = new StreamReader(inFile2);
                recordIn = reader2.ReadLine();

                int x = 0;

                // while statement to read delimited text file and store email field in array
                while (recordIn != null)
                {
                    fields       = recordIn.Split(DELIM);
                    fieldEmail   = fields[3];
                    userEmail[x] = fieldEmail;
                    ++x;
                    recordIn = reader2.ReadLine();
                }

                reader2.Close();
                inFile2.Close();

                firstIsValid = true;
                lastIsValid  = true;

                userNumber = numAccounts + 1;
                // Store first and last name in array
                userAccount[0] = Convert.ToString(userNumber);
                userAccount[1] = first;
                userAccount[2] = last;
                userAccount[5] = Convert.ToString(userTotalOrders);

                // If statement to validate email address and store in array
                atIndex = email.IndexOf(atSign);

                if (atIndex > 0)
                {
                    emailIsValid          = true;
                    lblEmailError.Visible = false;

                    // for loop to check if email already exists
                    for (int y = 0; y < numAccounts; ++y)
                    {
                        if (email == userEmail[y])
                        {
                            foundEmail = true;
                            txtEmail.Focus();
                            lblEmailError.Text    = "Email account already exists. Please use a different email.";
                            lblEmailError.Visible = true;
                        }
                        else
                        {
                            userAccount[3] = email;
                        }
                    }
                }

                else
                {
                    txtEmail.Focus();
                    lblEmailError.Text    = "Invalid email format.";
                    lblEmailError.Visible = true;
                }

                // If statement to validate if passwords match and store in array
                if ((passwordPre.Length < 8) || (passwordCon.Length < 8))
                {
                    txtPass1.Focus();
                    lblPassError2.Text    = "Password length too short.";
                    lblPassError2.Visible = true;
                }
                else if (string.Equals(passwordPre, passwordCon))
                {
                    passIsValid           = true;
                    userPassword          = passwordCon;
                    userAccount[4]        = userPassword;
                    lblPassError2.Visible = false;
                }
                else
                {
                    txtPass1.Focus();
                    lblPassError2.Text    = "Password did not match.";
                    lblPassError2.Visible = true;
                }

                // Declare filestream writer
                FileStream   outFile = new FileStream(FILENAME, FileMode.Append, FileAccess.Write);
                StreamWriter writer  = new StreamWriter(outFile);

                // If statement to write to file when all of users info is valid
                if (firstIsValid && lastIsValid && emailIsValid && passIsValid && !foundEmail)
                {
                    writer.WriteLine(userAccount[0] + DELIM + userAccount[1] + DELIM + userAccount[2] + DELIM + userAccount[3] + DELIM + userAccount[4] + DELIM + userAccount[5]);

                    // close writer
                    writer.Close();
                    outFile.Close();

                    FileStream   outFile2 = new FileStream(FILENAME2, FileMode.Open, FileAccess.Write);
                    StreamWriter writer2  = new StreamWriter(outFile2);
                    writer2.WriteLine(userAccount[0] + DELIM + userAccount[3]);

                    writer2.Close();
                    outFile2.Close();

                    frmBuild secondForm = new frmBuild();
                    secondForm.Show();
                    this.Hide();
                }
            }
        }