我的问题是如何通过他的ID找到员工,例如,如果我输入文本框17432,当我点击搜索按钮时,它应该给我员工ID姓名姓氏和工资信息。
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();
for (int i = 0; i < 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 i = 0; i < 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;
for (int i = 0; i < employee.Count(); i++)
{
if (Mixsal < employee[i].Salary)
{
Mixsal = employee[i].Salary;
}
}
for (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 Some Thing Here
}
}
}
这是我的班级员工
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
{
get
{
return employeeID;
}
set
{
employeeID = value;
}
}
public string FullName
{
get
{
return fullName;
}
set
{
fullName = value;
}
}
public string LastName
{
get
{
return lastName;
}
set
{
lastName = value;
}
}
public double Salary
{
get
{
return salary;
}
set
{
salary = value;
}
}
public int Getinfo
{
get
{
return employeeID;
}
set
{
employeeID = value;
}
}
public string employeeInformationToString()
{
// employeeID = Convert.ToInt32(this.textBox1.Text);
return (Convert.ToString(employeeID) + " " + fullName + " " + lastName + " " + Convert.ToString(salary));
}
}
}
答案 0 :(得分:0)
您的表单中包含employee
。
您只需要简单的Linq
来查找给定员工的员工。
var found = employee.FirstOrDefault(e=>e.EmployeeIDNum == employeeid);
if(found != null)
{
// logic here.
// found.employeeInformationToString();
listBox1.Items.Clear(); // check this if you want to clear.
listBox1.Items.Add(found.employeeInformationToString());
}
所以你的完整搜索代码应该是......
private void sreachbtn_Click(object sender, EventArgs e)
{
//// Need Some Thing Here
int employeeid;
if(int.TryParse(textbo1.Text, out employeeid)) // use your textbox name.
{
var found = employee.FirstOrDefault(e=>e.EmployeeIDNum == employeeid);
if(found != null)
{
// logic here.
found.employeeInformationToString()
}
}
}
更新:
看起来您不熟悉Linq
,在这种情况下尝试使用传统循环。
private void sreachbtn_Click(object sender, EventArgs e)
{
//// Need Some 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()
}
}
}
答案 1 :(得分:0)
你去..
$lookup