Code Snippet
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
LoadComboBox();
}
private void LoadComboBox()
{
this.cboEmployee.BeginUpdate();
this.cboEmployee.Items.Clear();
List<Employee> empList = GetEmployeeList();
foreach (Employee item in empList)
{
cboEmployee.Items.Add(item);
}
this.cboEmployee.SelectedIndex = 0;
this.cboEmployee.EndUpdate();
}
private List<Employee> GetEmployeeList()
{
List<Employee> employeeList = new List<Employee>();
employeeList.Add(new Employee("Marco", "Blogg"));
employeeList.Add(new Employee("Jo", "Smith"));
employeeList.Add(new Employee("Diego", "Maradona"));
return employeeList;
}
private void cboEmployee_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.cboEmployee.SelectedItem != null)
{
Employee item = (Employee)cboEmployee.SelectedItem;
this.txtName.Text = item.Name ;
this.txtSurname.Text = item.Surname;
}
}
}
public class Employee
{
private string name=string.Empty;
private string surname = string.Empty;
public Employee(string name,string surname)
{
this.name = name;
this.surname = surname;
}
public string Name
{
get { return this.name; }
set { this.name = value; }
}
public string Surname
{
get { return this.surname; }
set { this.surname = value; }
}
public override string ToString()
{
return string.Format("{0} {1}", this.surname, this.name);
}
}