我遇到了一些与listbox有关的问题,一直试图让它成为当我点击一个项目时它将填充文本框(下图)与该项目的信息。 http://i.stack.imgur.com/G32uq.jpg(不要让我发布图片)。
继承我的代码(这段代码我目前将填充文本框中包含我需要的内容,但我希望它能够通过单击项目来完成相同的操作)。
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;
using System.IO;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private int index;
private const int SIZE = 4;
private int count = 0;
private Employee[] employees = new Employee[SIZE];
List<Employee> listEmp = new List<Employee>(SIZE);
public Form1()
{
InitializeComponent();
listEmp.Add(new Hourly(1, "Karl", "lane drive", "201-9090", 40, 12.00)); //item1
listEmp.Add(new Salaried(2, "Steve", "circle road", "803-1230", 1200)); // item2
listEmp.Add(new Hourly(3, "Westley", "square alley", "892-2000", 40, 10.00)); //item3
listEmp.Add(new Salaried(4, "Anders", "triangle boulevard", "910-8765", 1000)); //item4
index = 0;
computPayBtn.Enabled = true;
listBox1.DataSource = listEmp;
}
// opens a file and reads data into the employee objects
private void openToolStripMenuItem1_Click(object sender, EventArgs e)
{
Stream myStream = null;
Employee tempEmploy = null;
string type = null;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "c:\\";
openFileDialog1.Filter = "text files (*.txt)|*txt";
count = 0;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
if ((myStream = openFileDialog1.OpenFile()) != null)
{
StreamReader data = new StreamReader(myStream);
do
{
type = data.ReadLine();
if (type != null)
{
if (type == "hourly")
tempEmploy = new Hourly();
else if (type == "salaried")
tempEmploy = new Salaried();
tempEmploy.ReadData(data);
employees[count++] = tempEmploy;
}
} while (type != null);
computPayBtn.Enabled = true;
count = 0;
}
}
}
private void exitToolStripMenuItem1_Click(object sender, EventArgs e)
{
this.Close();
}
// shows next employee when clicked.
private void computPayBtn_Click_1(object sender, EventArgs e)
{
checkbox.Clear( );
int index = count;
if (index < SIZE)
{
string emp = "Fluffshuffle Electronics check no. ";
emp += string.Format("{0}", index);
emp += Environment.NewLine;
emp += Environment.NewLine;
emp += " Pay to the order of ";
emp += employees[index].Name;
emp += Environment.NewLine;
emp += " ";
emp += string.Format("{0:C}", employees[index].CalcPay());
emp += Environment.NewLine;
emp += Environment.NewLine;
emp += " First National Bank";
checkbox.Text = emp;
namebox.Text = employees[index].Name;
addressbox.Text = employees[index].Address;
phonebox.Text = employees[index].PhoneNum;
empNumbox.Text = string.Format("{0}", employees[index].EmpNum);
Hourly houremploy = employees[index] as Hourly;
if (houremploy != null)
{
hoursbox.Text = string.Format("{0:F2}", houremploy.HoursWorked);
wagebox.Text = string.Format("{0:F2}", houremploy.HourlyWage);
salarybox.Clear();
}
Salaried salemploy = employees[index] as Salaried;
if (salemploy != null)
{
hoursbox.Clear();
wagebox.Clear();
salarybox.Text = string.Format("{0:F2}", salemploy.Salary);
}
count++;
}
else
{
computPayBtn.Enabled = false;
namebox.Clear( );
addressbox.Clear(); ;
phonebox.Clear(); ;
empNumbox.Clear( );
hoursbox.Clear();
wagebox.Clear();
salarybox.Clear();
count = 0;
}
}
// saves employee objects into a txt file.
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
Stream myStream = null;
count = SIZE;
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.InitialDirectory = "c:\\";
saveFileDialog.Filter = "text files (*.txt)|*txt";
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
if ((myStream = saveFileDialog.OpenFile()) != null)
{
StreamWriter data = new StreamWriter(myStream);
for (int i = 0; i < count; i++)
{
employees[i].WriteData(data);
employees[i] = null;
}
data.Close();
computPayBtn.Enabled = false;
count = 0;
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}
和班级
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace WindowsFormsApplication1
{
// provides methods to read and write objects to a file
public interface IStorable
{
// writes object's data to a StreamWriter object
// The StreamReader object to write to
void WriteData(StreamWriter swo);
// reads object's data from a StreamReader object
// The StreamReader object to read from
void ReadData(StreamReader sro);
}
public abstract class Employee : IStorable
{
private int empNum;
private string name;
private string address;
private string phoneNum;
protected const double STATE_TAX = 0.075;
protected const double FED_TAX = 0.20;
// set data members to defaults
public Employee()
{
empNum = 0;
name = "unknown";
address = "unknown";
phoneNum = "unknown";
}
// set data members to values passed to method
// employee number, name, address, and phone number
public Employee(int _empNum, string _name, string _address, string _phoneNum)
{
empNum = _empNum;
name = _name;
address = _address;
phoneNum = _phoneNum;
}
public int EmpNum
{
get { return empNum; }
set { empNum = value; }
}
public string Name
{
get { return name; }
set { name = value; }
}
public string Address
{
get { return address; }
set { address = value; }
}
public string PhoneNum
{
get { return phoneNum; }
set { phoneNum = value; }
}
// reads object's data from a StreamReader object
// The method is virtual so we can use polymorphism
public virtual void ReadData(StreamReader sro)
{
EmpNum = int.Parse(sro.ReadLine());
Name = sro.ReadLine();
Address = sro.ReadLine();
PhoneNum = sro.ReadLine();
}
// writes object's data to a StreamReader object
// The method is virtual so we can use polymorphism
public virtual void WriteData(StreamWriter sro)
{
sro.WriteLine(this.EmpNum);
sro.WriteLine(this.Name);
sro.WriteLine(this.Address);
sro.WriteLine(this.PhoneNum);
}
// calculates the employee's net pay
public abstract double CalcPay();
}
// The Hourly Class - represents an hourly employee
// Inherits from Employee
class Hourly : Employee
{
private const int WEEK = 40;
private const double BONUS = 1.5;
private double hoursWorked;
private double hourlyWage;
//set data members to defaults
public Hourly()
{
hoursWorked = 0.0;
hourlyWage = 0.0;
}
// set data members to values passed as arguments
// employee number, name, address, phone number, hours, and wage
public Hourly(int _empNum, string _name, string _address, string _phoneNum, double _hours, double _wage)
: base(_empNum, _name, _address, _phoneNum)
{
hoursWorked = _hours;
hourlyWage = _wage;
}
public double HoursWorked
{
get { return hoursWorked; }
set { hoursWorked = value; }
}
public double HourlyWage
{
get { return hourlyWage; }
set { hourlyWage = value; }
}
// calculates gross pay
// hours * wage + time and 1/2 for overtime
public override double CalcPay()
{
double overTime = 0.0;
if (hoursWorked > WEEK)
{
overTime = hoursWorked - WEEK;
hoursWorked -= WEEK;
}
double grossPay = hoursWorked * hourlyWage + overTime * hourlyWage * BONUS;
double stateTax = grossPay * STATE_TAX;
double fedTax = grossPay * FED_TAX;
return (grossPay - stateTax - fedTax);
}
// reads object's data from a StreamReader object
// Over-rides the ReadData method in Employee
public override void ReadData(StreamReader sro)
{
HoursWorked = double.Parse(sro.ReadLine());
HourlyWage = double.Parse(sro.ReadLine());
base.ReadData(sro);
}
// writes object's data to a StreamWriter object
// Over-rides the WriteData method in Employee
public override void WriteData(StreamWriter swo)
{
swo.WriteLine("hourly");
swo.WriteLine(this.HoursWorked);
swo.WriteLine(this.HourlyWage);
base.WriteData(swo);
}
}
class Salaried : Employee
{
private const double BENEFITS = 0.0524;
private double salary;
// set data members to defaults
public Salaried()
{
salary = 0.0;
}
// set data members to values passed as arguments
// employee number, name, address, phone number, salary
public Salaried(int _empNum, string _name, string _address, string _phoneNum, double _salary)
: base(_empNum, _name, _address, _phoneNum)
{
salary = _salary;
}
public double Salary
{
get { return salary; }
set { salary = value; }
}
// calculates pay for a salaried employee
public override double CalcPay()
{
double stateTax = salary * STATE_TAX;
double fedTax = salary * FED_TAX;
double bennies = salary * BENEFITS;
return (salary - stateTax - fedTax - bennies);
}
//reads object's data from a StreamReader object
public override void ReadData(StreamReader sro)
{
Salary = double.Parse(sro.ReadLine());
base.ReadData(sro); // call Employee's ReadData to get name, address, etc
}
// writes data to StreamWriter
public override void WriteData(StreamWriter swo)
{
swo.WriteLine("salaried");
swo.WriteLine(this.Salary);
base.WriteData(swo);
}
}
}
提前感谢您的帮助。
答案 0 :(得分:1)
使用listbox的点击事件;将SelectedItems [0]作为雇员投射并填充文本框。为简单起见,将列表框的多个选项设置为false。例如:
private void listBox1_Clik(object sender, EventArgs e)
{
Employee employee = listBox1.SelectedItems[0] as Employee;
if (employee != null)
{
// use the employee object to populate the textbox.
}
}
答案 1 :(得分:0)
看看ListBox上的SelectedIndexChanged事件,它可能会做你想要的。实际上,docuemntation中的示例代码显示了如何在第一个ListBox中选择一个项目时在第二个ListBox中选择一个项目,这样可以给你一些想法。