我有兩個列表框。第一個列表框包含交通違規列表。當你點擊添加按鈕並執行代碼時,listbox2得到這個項目「ListBoxTest.Violation」,而不是從listBox1中顯示的項目...將列表框項轉移到另一個列表框(Winforms C#)
我的代碼有什麼問題?
namespace ListBoxTest
{
/// <summary>
/// Description of MainForm.
/// </summary>
public partial class MainForm : Form
{
private List<Violation> violationList = new List<Violation>();
public MainForm()
{
InitializeComponent();
}
void MainFormLoad(object sender, EventArgs e)
{
LoadViolations(); // Initialize and add violations to violationList.
listBox1.DataSource = violationList; // Set the DataSource property.
listBox1.ValueMember = "Code";
listBox1.DisplayMember = "Description";
}
void LoadViolations()
{
Violation violation;
violation = new Violation("001", "Beating the red light");
violationList.Add(violation);
violation = new Violation("002", "Exceeding posted speed limit on the road");
violationList.Add(violation);
violation = new Violation("003", "Driving a vehicle without license to drive");
violationList.Add(violation);
violation = new Violation("004", "Driving a non registered vehicle");
violationList.Add(violation);
violation = new Violation("005", "Vehicle has no plate number");
violationList.Add(violation);
}
void BtnAddClick(object sender, EventArgs e)
{
listBox2.Items.Add(listBox1.SelectedItem); // Add item from listBox1 to listBox2;
}
}
/// <summary>
/// Violation Class
/// Properties: Code, Description
/// </summary>
public class Violation
{
private string _code;
private string _description;
public Violation(string code, string description)
{
_code = code;
_description = description;
}
public String Code
{
get { return _code; }
set { _code = value; }
}
public String Description
{
get { return _description; }
set { _description = value; }
}
}
}
不工作,相同的值被添加到listBox2。 – yonan2236 2011-05-10 05:08:48
我修改了代碼來解決問題。 – 2011-05-10 05:20:31