Using Property, Method,
Class, Array Resize in C# .Net
First we create Class which name is Employee with property and method……..
using System;
using System.Windows.Forms;
namespace ArrayPropertywithClass
{
class Employee
{
private int IntEmpCode;
public int EmpCode
{
get { return
IntEmpCode; }
set { IntEmpCode = value;
}
}
private string
StrFirstName;
public string
FirstName
{
get { return
StrFirstName; }
set { StrFirstName = value;
}
}
private string
StrLastName;
public string LastName
{
get { return
StrLastName; }
set { StrLastName = value;
}
}
private string
StrEmailId;
public string EmailId
{
get { return
StrEmailId; }
set { StrEmailId = value;
}
}
private string
StrPassword;
public string Password
{
get { return
StrPassword; }
set { StrPassword = value;
}
}
private string
StrAddress;
public string Address
{
get { return
StrAddress; }
set { StrAddress = value;
}
}
private string
StrCompanyName;
public string
CompanyName
{
get { return
StrCompanyName; }
set { StrCompanyName = value;
}
}
private string
StrSecurityQues;
public string
SecurityQues
{
get { return
StrSecurityQues; }
set { StrSecurityQues = value;
}
}
private string
StrAnswer;
public string Answer
{
get { return
StrAnswer; }
set { StrAnswer = value;
}
}
static Employee[]
emp = new Employee[1];
static int index = 0;
int empcode;
// code for Add Employee with Array Resize Method…………
public int
AddEmployee(string FirstName, string LastName, string
EmailId, string Password, string Address, string
Company, string SecuQuestion, string SecuAnswer)
{
Random rr=new Random();
empcode=
rr.Next(100, 10000);
emp[index]
= new Employee();
emp[index].EmpCode
= empcode;
emp[index].FirstName
= FirstName;
emp[index].LastName
= LastName;
emp[index].EmailId
= EmailId;
emp[index].Password
= Password;
emp[index].Address
= Address;
emp[index].CompanyName
= Company;
emp[index].SecurityQues
= SecuQuestion;
emp[index].Answer
= SecuAnswer;
index++;
Array.Resize(ref emp,
index + 1);
return empcode;
}
// create method for Search and Update by Employee Code …………
int i ;
public Employee
SearchUpdateByEmpCode(int EmpCode)
{
for ( i = 0; i < index ; i++)
{
if (emp[i].EmpCode == EmpCode)
{
return emp[i];
}
else
{
MessageBox.Show("CustomerID
Doesn't Match Please enter correct id");
}
}
return null;
}
// create method for GetLogin
with EmailId and Password …………
public Employee
GetLogin(string EmailId,string Password)
{
for ( i = 0; i < emp.Length-1; i++)
{
if (emp[i].EmailId == EmailId &&
emp[i].Password == Password)
{
return emp[i];
}
else
{
MessageBox.Show("ID and
Password not match please try again");
break;
}
}
return null;
}
}
}
Code for Employee
Registration Form…………………………………
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 ArrayPropertywithClass
{
public partial class EmpRegistration
: Form
{
public EmpRegitration()
{
InitializeComponent();
}
Employee em = new Employee();
//code for Add new Employee with EmpCode
private void
btnRegitration_Click(object sender, EventArgs e)
{
try
{
int i=em.AddEmployee(txtfirstname.Text,
txtlastname.Text, txtEmailID.Text, txtpassword.Text, txtaddress.Text,txtcompany.Text,
cbSecurityQues.Text, txtans.Text);
lblempc.Visible
= true;
lblempcode.Visible
= true;
lblempcode.ForeColor=Color.Red;
lblempcode.Text=
i.ToString();
MessageBox.Show("Emp Add
Successfully Your EmpCode is"+lblempcode.Text);
ClearAllControl();
this.Hide();
Login ll = new Login();
ll.Show();
}
catch(Exception
ex)
{
MessageBox.Show("Error"+ex.Message);
}
}
private void
btnclose_Click(object sender, EventArgs e)
{
this.Close();
}
private void
btnClear_Click(object sender, EventArgs e)
{
ClearAllControl();
}
//code for Check with
EmpCode
private void
btncheck_Click(object sender, EventArgs e)
{
Employee em1=em.SearchUpdateByEmpCode(Convert.ToInt32(txtempcode.Text));
if (em1 != null)
{
txtfirstname.Text
= em1.FirstName;
txtlastname.Text
= em1.LastName;
txtEmailID.Text
= em1.EmailId;
txtpassword.Text
= em1.Password;
txtaddress.Text
= em1.Address;
txtcompany.Text
= em1.CompanyName;
cbSecurityQues.SelectedText
= em1.SecurityQues;
txtans.Text
= em1.Answer;
}
else
{
MessageBox.Show("This
EmpCode is not exist in Array");
}
}
//code for Clear all TextBox with foreach loop.......
private void
ClearAllControl()
{
cbSecurityQues.Text
= "--Select--";
foreach (Control c in this.Controls)
{
if (c.GetType() == typeof(TextBox))
{
c.Text
= "";
}
}
}
//code for Update all data with EmpCode...............
private void
btnUpdate_Click(object sender, EventArgs e)
{
Employee em1 = em.SearchUpdateByEmpCode(Convert.ToInt32(txtempcode.Text));
if (em1 != null)
{
em1.FirstName
= txtfirstname.Text;
em1.LastName
= txtlastname.Text;
em1.EmailId
= txtEmailID.Text;
em1.Password=txtpassword.Text;
em1.Address
= txtaddress.Text;
em1.CompanyName
= txtcompany.Text;
em1.SecurityQues
= cbSecurityQues.Text;
em1.Answer=txtans.Text;
MessageBox.Show("Update Successfully");
ClearAllControl();
}
else
{
MessageBox.Show("This
EmpCode is not exist in Array");
}
}
//code for LinkLable......................
private void
linklbllogin_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
this.Hide();
Login lg = new Login();
lg.Show();
}
}
}
Code for Login Page…………………….
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 ArrayPropertywithClass
{
public partial class Login : Form
{
public Login()
{
InitializeComponent();
}
Employee em = new Employee();
Employee em1;
private void
btnlogin_Click(object sender, EventArgs e)
{
em1 =
em.GetLogin(txtEmailID.Text, txtpassword.Text);
if (em1 != null)
{
this.Hide();
WelCome ww = new WelCome(txtEmailID.Text);
ww.Show();
}
else
{
MessageBox.Show("EmailId
and Password not match Please try again");
}
}
//code for LinkLable......................
private void lbllinknewregistration_LinkClicked(object sender, LinkLabelLinkClickedEventArgs
e)
{
this.Hide();
EmpRegitration er = new EmpRegitration();
er.Show();
}
private void
Login_Load(object sender, EventArgs e)
{
}
}
}
0 comments:
Post a Comment