我已经用C#编写了这个程序,用于我今晚要完成的任务。我要做的是创建一个名为“Employee”的类,将信息显示在List Box上。我相信我把所有东西放在一起,而且我没有显示任何语法错误,但是当我尝试运行程序时,没有任何反应。你认为你可以帮我找出它不工作的原因吗?出现的只是一个空白列表框。这是我在Form1.cs上的代码:
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 Adam_Zeidan___IS_204___HW10CH9_4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
listBox1.Items.Add("Name\t\tID Number\tDepartment\tPosition");
Employee emp1 = emp1 = new Employee();
emp1.Name = "Susan Meyers";
emp1.IdNumber = 47899;
emp1.Department = "Accounting";
emp1.Position = "Vice President";
listBox1.Items.Add(emp1.Name + "\t" + emp1.IdNumber + "\t\t" + emp1.Department + "\t" + emp1.Position);
Employee emp2 = emp2 = new Employee();
emp2.Name = "Mark Jones";
emp2.IdNumber = 39119;
emp2.Department = "IT";
emp2.Position = "Programmer";
listBox1.Items.Add(emp2.Name + "\t" + emp2.IdNumber + "\t\t" + emp2.Department + "\t" + emp2.Position);
Employee emp3 = emp3 = new Employee();
emp3.Name = "Joy Rogers";
emp3.IdNumber = 81774;
emp3.Department = "Manufacturing";
emp3.Position = "Engineer";
listBox1.Items.Add(emp3.Name + "\t" + emp3.IdNumber + "\t\t" + emp3.Department + "\t" + emp3.Position);
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}
现在这是我用来创建类的Employee.cs代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Adam_Zeidan___IS_204___HW10CH9_4
{
class Employee
{
private string _name;
private int _idNumber;
private string _department;
private string _position;
public Employee()
{
_name = "";
_idNumber = 0;
_department = "";
_position = "";
}
public Employee(string name, int idNumber)
{
_name = name;
_idNumber = idNumber;
_department = "";
_position = "";
}
public Employee(string name, int idNumber, string department, string position)
{
_name = name;
_idNumber = idNumber;
_department = department;
_position = position;
}
public string Name
{
get
{
return _name;
}
set
{
_name = value;
}
}
public int IdNumber
{
get
{
return _idNumber;
}
set
{
_idNumber = value;
}
}
public string Department
{
get
{
return _department;
}
set
{
_department = value;
}
}
public string Position
{
get
{
return _position;
}
set
{
_position = value;
}
}
}
你认为你可以帮我找出它无效的原因吗?
答案 0 :(得分:1)
更改以下行
Employee emp1 = new Employee();
Employee emp2 = new Employee();
Employee emp3 = new Employee();
到
xts:::current.xts_chob()
答案 1 :(得分:0)
我尝试了给定的片段;这对我来说很合适,请检查ListBox
的属性以获取它的可见性,并确保Form1_Load
事件正在触发。我建议按以下方式在类中按overriding .ToString()
简化代码:
public override string ToString()
{
return this.Name + "\t" + this.IdNumber + "\t\t" + this.Department + "\t" + this.Position;
}
以便Form1_Load
中的代码如下所示:
Employee emp1 = new Employee();
emp1.Name = "Susan Meyers";
emp1.IdNumber = 47899;
emp1.Department = "Accounting";
emp1.Position = "Vice President";
listBox1.Items.Add(emp1.ToString());
Employee emp2 = new Employee();
//init emp2 here
listBox1.Items.Add(emp2.ToString());
Employee emp3 = emp3 = new Employee();
//init emp3 here
listBox1.Items.Add(emp3.ToString());