private void lstTractors_SelectedIndexChanged(object sender, EventArgs e) { // lstTractors listbox is an exact representation of my generic // tractor list. I will use the index of the selected item // to retrieve the tractor object from the generic list. // Put the tractor details onto textboxes // Will also put the tractor registration number into the combobox cboTractors int indexTemp = lstTractors.SelectedIndex; Tractor currentTractor = garage.ElementAt(indexTemp); cboTractors.Items.Add(currentTractor.RegistrationNumber); txtRegNum.Text = currentTractor.RegistrationNumber; txtMake.Text = currentTractor.Make; txtModel.Text = currentTractor.Model; txtPrice.Text = (currentTractor.Price.ToString("c")); // highlighting the contains method... if (garage.Contains(currentTractor)) { int whereItis = garage.IndexOf(currentTractor); Tractor searchTractor = garage.ElementAt(whereItis); // display the tractor } else { // not found } }
private void frmListsEtc_Load(object sender, EventArgs e) { Tractor t1 = new Tractor("88MO1244", "Ford", "4400", 12000); Tractor t2 = new Tractor("76DL5555", "Ford", "3600", 14000); Tractor t3 = new Tractor("94SO5678", "Massey Ferguson", "365", 5000); Tractor t4 = new Tractor("76RN4455", "David Brown", "895", 2500); garage.Add(t1); garage.Add(t2); garage.Add(t3); garage.Add(t4); // Place each tractor in the garage onto lstTractors ListBox foreach (Tractor stockNum in garage) { lstTractors.Items.Add(stockNum); } }