c# - How to find employee by id with textbox and button -
my question how can find employee id example if type in textbox 17432 , when click on search button should give me employee id fullname lastname , salary information .
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 project_employee { public partial class form1 : form { employee[] employee = new employee[10]; public form1() { initializecomponent(); employee[0] = new employee(17432, "john", "adverd", 800.00); employee[1] = new employee(17433, "adrian", "smith", 800.00); employee[2] = new employee(17434, "stephen", "rad", 9000.00); employee[3] = new employee(17435, "jesse", "harris", 800.00); employee[4] = new employee(17436, "jonatan", "morris", 9500.00); employee[5] = new employee(17437, "morgen", "freeman", 12000.00); employee[6] = new employee(17438, "leory", "gomez", 10200.00); employee[7] = new employee(17439, "michael", "brown", 9000.00); employee[8] = new employee(17440, "andrew", "white", 3500.00); employee[9] = new employee(17441, "maria", "carson", 17000.00); } private void searchbtn_click(object sender, eventargs e) { listbox1.items.clear(); (int = 0; < 10; i++) { string employeestring = employee[i].employeeinformationtostring() + "\r\n"; listbox1.items.add(employeestring); } } public void lowestemployeesalary() { listbox1.items.clear(); double minimumsal = employee[0].salary; for(int = 0; < employee.count(); i++) { if(minimumsal > employee[i].salary ) { minimumsal = employee[i].salary; } } for(int j = 0; j < employee.count(); j++) { if(minimumsal == employee[j].salary) { string result = string.format("{0} {1} {2} {3}", employee[j].employeeidnum, employee[j].fullname, employee[j].lastname, employee[j].salary); listbox1.items.add(result); } } } public void highemployeesalary() { listbox1.items.clear(); double mixsal = employee[0].salary; (int = 0; < employee.count(); i++) { if (mixsal < employee[i].salary) { mixsal = employee[i].salary; } } (int j = 0; j < employee.count(); j++) { if (mixsal == employee[j].salary) { string result = string.format("{0} {1} {2} {3}", employee[j].employeeidnum, employee[j].fullname, employee[j].lastname, employee[j].salary); listbox1.items.add(result); } } } private void getinfibtn_click(object sender, eventargs e) { lowestemployeesalary(); } private void highestbtn_click(object sender, eventargs e) { highemployeesalary(); } private void sreachbtn_click(object sender, eventargs e) { //// need thing here } } }
and here class employee
using system; using system.collections.generic; using system.linq; using system.text; using system.threading.tasks; using system.windows.forms; namespace project_employee { class employee { private int employeeid; private string fullname; private string lastname; private double salary; public employee() { employeeid = 0; fullname = ""; lastname = ""; salary = 0.0; } public employee(int empidvalue, string fullnameval, string lastnameval) { employeeid = empidvalue; fullname = fullnameval; lastname = lastnameval; salary = 0.0; } public employee(int empidvalue, string fullnameval, string lastnameval, double salaryvalue) { employeeid = empidvalue; fullname = fullnameval; lastname = lastnameval; salary = salaryvalue; } public int employeeidnum { { return employeeid; } set { employeeid = value; } } public string fullname { { return fullname; } set { fullname = value; } } public string lastname { { return lastname; } set { lastname = value; } } public double salary { { return salary; } set { salary = value; } } public int getinfo { { return employeeid; } set { employeeid = value; } } public string employeeinformationtostring() { // employeeid = convert.toint32(this.textbox1.text); return (convert.tostring(employeeid) + " " + fullname + " " + lastname + " " + convert.tostring(salary)); } } }
you have collection of employee
in form.
you need simple linq
find employee given employeeid.
var found = employee.firstordefault(e=>e.employeeidnum == employeeid); if(found != null) { // logic here. // found.employeeinformationtostring(); listbox1.items.clear(); // check if want clear. listbox1.items.add(found.employeeinformationtostring()); }
so complete search code should like...
private void sreachbtn_click(object sender, eventargs e) { //// need thing here int employeeid; if(int.tryparse(textbo1.text, out employeeid)) // use textbox name. { var found = employee.firstordefault(e=>e.employeeidnum == employeeid); if(found != null) { // logic here. found.employeeinformationtostring() } } }
update :
looks not familiar linq
, in case try uing traditional looping.
private void sreachbtn_click(object sender, eventargs e) { //// need thing here int employeeid; if(int.tryparse(textbo1.text, out employeeid)) { employee found= null; foreach(var emp in employee) { if(emp.employeeidnum == employeeid) { found = emp; break; } } //var found = employee.firstordefault(e=>e.employeeidnum == employeeid); if(found != null) { // logic here. found.employeeinformationtostring() } } }
Comments
Post a Comment