Affichage des articles dont le libellé est c# register form. Afficher tous les articles
Affichage des articles dont le libellé est c# register form. Afficher tous les articles

C# Login And Register Form With Text File

How to Create Login and Register Form in C# with Text File



in this c# form design tutorial we will see how to design a login and register form with a text file in one form using csharp programming language .

tools:
- c# programming language.
- microsoft visual studio express 2013.
- text file.
- pixabay.com ( images).

Watch This Full Demo



▶ Download All C# Projects Source Code

- The Project Source Code


using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Csharp_Login_Register_With_Txt_File
{
    public partial class Login_And_Register_Form : Form
    {
        public Login_And_Register_Form()
        {
            InitializeComponent();
        }

        string filepath = @"C:\Users\1BestCsharp\Desktop\files\users.txt";
        // arraylist of usernames
        ArrayList allusernames = new ArrayList();
        // dictionary of usernames and passwords
        Dictionary<string, string> usernameANDpassword = new Dictionary<string, string>();


        // create a function to get all the usernames and passwords
        public void getUsers()
        {
            string[] lines = File.ReadAllLines(filepath);
            string[] values;
            string un = "";
            string ps = "";

            for(int i = 0; i < lines.Length; i++)
            {
                values = lines[i].ToString().Split(':');

                if(values[0].Trim().Equals("Username"))
                {
                    un = values[1];
                    // add username to arraylist
                    allusernames.Add(un);
                }
                else if (values[0].Trim().Equals("Password"))
                {
                    ps = values[1];
                }
                if (un != "" && ps != "")
                {
                    // add usernames and passwords to dictionary
                    usernameANDpassword.Add(un, ps);

                    // clear username and password
                    un = "";
                    ps = "";
                }

            }

        }



        // close the form
        private void pictureBoxClose_Click(object sender, EventArgs e)
        {
            Environment.Exit(Environment.ExitCode);
        }

        private void Login_And_Register_Form_Load(object sender, EventArgs e)
        {
            // display the close icon
            pictureBoxClose.Image = Properties.Resources.cross_mark;

            getUsers();
        }

        // login button
        private void buttonLogin_Click(object sender, EventArgs e)
        {
            string username = textBoxUsername.Text;
            string password = textBoxPassword.Text;
            bool userExist = false;

            if(username.Trim().Equals("") || password.Trim().Equals(""))
            {
                MessageBox.Show("You Need To Enter The Username And Password","Empty Fields",MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
            else
            {
                foreach(var uname in usernameANDpassword)
                {
                    // check if the username exist
                    if(uname.Key.ToString().Trim().Equals(username))
                    {
                        // check if the password exist
                        if(uname.Value.ToString().Trim().Equals(password))
                        {
                            userExist = true;
                            break;
                        }
                    }
                }
                if(userExist)
                {
                    MessageBox.Show("This User Exists", "Login", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("This User Doesn't Exists", "Login", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }

        // register button
        private void buttonRegister_Click(object sender, EventArgs e)
        {
            string username = textBox_RG_Username.Text;
            string fullname = textBox_RG_Fullname.Text;
            string email = textBox_RG_Email.Text;
            string password = textBox_RG_Password.Text;
            string confirmpass = textBox_RG_ConfirmPass.Text;

            // check if the textbox are not empty
            if(!username.Trim().Equals("") && !fullname.Trim().Equals("") && !email.Trim().Equals("") && !password.Trim().Equals(""))
            {
                // check if the password confiramtion are correct
                if(password.Equals(confirmpass))
                {
                    TextWriter writer = new StreamWriter(filepath, true);

                    writer.Write("Username: " + username);
                    writer.WriteLine("");//new line
                    writer.Write("Fullname: " + fullname);
                    writer.WriteLine("");//new line
                    writer.Write("Email: " + email);
                    writer.WriteLine("");//new line
                    writer.Write("Password: " + password);
                    writer.WriteLine("");//new line
                    writer.Write("----------");
                    writer.WriteLine("");//new line

                    writer.Close();
                    MessageBox.Show("Account Created Successfully", "Register", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    usernameANDpassword.Add(username, password);
                }
                else
                {
                    MessageBox.Show("Confirmation Password Error", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            else
            {
                MessageBox.Show("One Ore More Fileds Are Empty", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }

            

        }
    }
}





C# - Login And Register Form Design


How To Design a Login And Register Form In C#

C# Login And Register Form Design


in this c# form design tutorial we will see how to design a login and register form in one  windowForm using csharp programming language .

tools:
- c# programming language.
- microsoft visual studio express 2013.
- iconsdb.com ( to get the close icon).
- canva.com ( to create the app logo ).
- flatuicolorpicker.com ( for color ideas ).

Watch This Full Demo



- The Project Source Code

we will add two buttons and two panels to the form, inside the panels we will add the textboxes and buttons to login and register, and the two buttons will help us to go between the two panels.

c# login form design


c# register form design


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace csharp_LoginRegister_Design
{
    public partial class LoginAndRegisterForm : Form
    {
        public LoginAndRegisterForm()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            button_GoToLogin.PerformClick();
        }

        Color select_color = Color.FromArgb(49, 46, 46);

        private void button_GoToLogin_Click(object sender, EventArgs e)
        {
            panel_login.BringToFront();
            panel_login_bar.BackColor = Color.Yellow;
            panel_register_bar.BackColor = select_color;
            button_GoToLogin.BackColor = select_color;
            button_GoToRegister.BackColor = Color.Black;
        }

        private void button_GoToRegister_Click(object sender, EventArgs e)
        {
            panel_register.BringToFront();
            panel_register_bar.BackColor = Color.Yellow;
            panel_login_bar.BackColor = select_color;
            button_GoToRegister.BackColor = select_color;
            button_GoToLogin.BackColor = Color.Black;
        }

        private void pictureBox_close_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}