Here is a simple example that loads the combobox and reacts to a selectedIndexChanged event.
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;
using WinFormExamples.Models;
namespace WinFormExamples.Forms
{
public partial class ComboExample : Form
{
private List<car> carList;
private void ComboExample_Load(object sender, EventArgs e)
{
carList = new List<car>();
carList.Add(new Car(1000, 4, 6, "Mercedes"));
carList.Add(new Car(1001, 2, 6, "Ferrari"));
carList.Add(new Car(1002, 2, 12, "Jaguar"));
comboBox1.DisplayMember = "CarName";
comboBox1.ValueMember = "CarId";
comboBox1.DataSource = carList;
}
public ComboExample()
{
InitializeComponent();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex > -1)
{
Car selectedCar = carList.Where(c => c.CarId == Convert.ToInt64(comboBox1.SelectedValue)).First();
txtName.Text = selectedCar.CarName;
txtDoors.Text = selectedCar.Doors.ToString();
txtCylinders.Text = selectedCar.Cylinders.ToString();
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WinFormExamples.Models
{
public class Car
{
public long CarId {get; set;}
public int Doors { get; set; }
public int Cylinders { get; set; }
public string CarName { get; set; }
public Car(long id, int doors, int cylinders, string name)
{
CarId = id;
Doors = doors;
Cylinders = cylinders;
CarName = name;
}
}
}</car></car>