C#列表框通过单击项目填充文本框
我在listbox上遇到了一些问题,我一直在尝试这样做,当我点击一个项目时,它会用该项目的信息填充文本框(下图)。 (不让我贴照片) 这是我的代码(我目前拥有的代码将用我需要的内容填充文本框,但我希望它能够通过单击项目来完成相同的操作)C#列表框通过单击项目填充文本框,c#,winforms,listbox,C#,Winforms,Listbox,我在listbox上遇到了一些问题,我一直在尝试这样做,当我点击一个项目时,它会用该项目的信息填充文本框(下图)。 (不让我贴照片) 这是我的代码(我目前拥有的代码将用我需要的内容填充文本框,但我希望它能够通过单击项目来完成相同的操作) 提前感谢您的帮助。请查看ListBox上的事件,它可能会满足您的需要。事实上,docuemntation中的示例代码显示了当在第一个列表框中选择项目时,如何在第二个列表框中选择项目,以便让您了解一些情况。使用列表框的单击事件;将SelectedItems[0]强
提前感谢您的帮助。请查看ListBox上的事件,它可能会满足您的需要。事实上,docuemntation中的示例代码显示了当在第一个列表框中选择项目时,如何在第二个列表框中选择项目,以便让您了解一些情况。使用列表框的单击事件;将SelectedItems[0]强制转换为employee并填充文本框。为简单起见,将列表框的多个选择设置为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.
}
}
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);
}
}
}
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.
}
}