[c#] simple login box with logic

Status
Not open for further replies.

jayfella

Active Member
Veteran
1,600
2009
565
680
This little example shows how to create objects without the use of the Design view, how to position the items, how to use the items, using objective programming and making use of returned results.

Hope it helps a few on the way.


PHP:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication9
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.Size = new Size(170, 200);
            DrawObjects();
        }

        private void DrawObjects()
        {
            // draw some controls
            TextBox uname = new TextBox();
            uname.Name = "uname_tb";
            uname.Location = new Point(20, 20);
            uname.Size = new Size(100, 23);
            this.Controls.Add(uname);

            TextBox pword = new TextBox();
            pword.Name = "pwd_tb";
            pword.Location = new Point(20, 53);
            pword.Size = new Size(100, 23);
            this.Controls.Add(pword);

            Button button1 = new Button();
            button1.Location = new Point(20, 86);
            button1.Size = new Size(100, 23);
            button1.Click += new EventHandler(button_click);
            button1.Text = "Login";
            this.Controls.Add(button1);

            Label myLabel = new Label();
            myLabel.Name = "statusLabel";
            myLabel.Location = new Point(20, 119);
            myLabel.Text = "Ready.";
            this.Controls.Add(myLabel);
        }

        private void button_click(object sender, EventArgs e)
        { 
            // create some controls so we can access them.
            TextBox uname = new TextBox();
            TextBox pwd = new TextBox();
            Label status = new Label();

            // for each control in this forms controls
            foreach (Control control in this.Controls)
            {
                if (control is TextBox)
                {
                    if (control.Name == "uname_tb")
                        uname = (TextBox)control;
                if (control.Name == "pwd_tb")
                        pwd = (TextBox)control;
                }

                if (control is Label)
                {
                if (control.Name == "statusLabel")
                        status = (Label)control;
                }
            }

            // get the value of the boolean object "checkLogin"
            // with these details, and store the returned output
            // in a boolean form.
            Boolean output = checkLogin(uname.Text, pwd.Text);

            // if the result of "checkLogin"....
            if (!output)
                status.Text = "false";
            else
                status.Text = "true";
        }

        private bool checkLogin(string username, string password)
        {
            // if the username and password are correct.
            if (username == "jayfella" && password == "letmein")
                return true;

            else return false;

        }
            

    }
}
 
3 comments
Status
Not open for further replies.
Back
Top