I'm trying to access and change the value of a an object within a bindinglist of objects.
I have a bindinglist of Packages that contain these Package objects:
private BindingList<Package> packages;
Package standardPackage = new Package("Standard",7.5m,standardInterior,standardExterior);
Package deluxePackage = new Package("Deluxe",15,deluxeInterior,deluxeExterior);
Package executivePackage = new Package("Executive", 35, executiveInterior, executiveInterior);
Package luxuryPackage = new Package("Luxury",55,luxuryInterior,luxuryExterior);
this.packages.Add(standardPackage);
this.packages.Add(deluxePackage);
this.packages.Add(executivePackage);
this.packages.Add(luxuryPackage);
Each interior element of the package has a fragrance associated with said package. I have created a method with a switch case to identify the fragrance and return the price of the fragrance.
private decimal Update_Price_With_Fragrance()
{
decimal price = 0;
string fragrance = this.fragrancesSource.Current.ToString();
switch(fragrance)
{
case "Hawaiian Mist":
price = 2.75m;
break;
case "Baby Powder":
price = 1.5m;
break;
case "Country Floral":
price = 2.25m;
break;
case "Pina Colada":
price = .75m;
break;
case "Vanilla":
price = 2.0m;
break;
}
return price;
}
I also have an event handler for when a combobox selected index changes to update the price of the package based on which fragrance is selected, but how do I access the price element of each Package within the bindinglist of packages?
The following shows a way one might use
ComboBoxandListBoxtogether for your CarWash project described here.Create a new
Windows Forms App (.NET Framework)(name: CarWashDL)We'll create a number of classes that contain properties that we'll use to hold the data that we're going to use.
Create a class (name: CarWashFragrance.cs)
Create a class (name: CarWashService.cs)
Create a class (name: CarWashInterior.cs)
Create a class (name: CarWashPackage.cs)
The following classes aren't really necessary - one can create methods in your
Forminstead. I've used them to show an alternative. The data below is stored in a Property, but can be accessed via the Property or Method.Create a class (name: DefaultCarWashFragrance.cs)
Add using directive
using System.Collections.Generic;DefaultCarWashFragrance:
Create a class (name: DefaultCarWashPackages.cs)
Add using directive
using System.Collections.Generic;DefaultCarWashPackages:
Now that all of the classes have been created, we'll work with the
Form(name: Form1.cs).Add the following to the form
It should look similar to the following:
Add the following to
groupBoxInteriorGroupBoxIt should look similar to the following:
Add the following to
groupBoxExteriorGroupBoxIt should look similar to the following:
Add the following to the form (lower-right):
It should look similar to the following:
Open Solution Explorer:
Open Properties Window:
Now we'll subscribe to some events.
Form1 - subscribe to the Form.Load event
ComboBox (name: comboBoxPackage) - subscribe to the ComboBox.SelectedIndexChanged event
comboBoxPackageListBox (name: listBoxInterior) - subscribe to the ListBox.SelectedIndexChanged event
listBoxInteriorComboBox (name: comboBoxFragrance) - subscribe to the ComboBox.SelectedIndexChanged event
comboBoxPackageListBox (name: listBoxExterior) - subscribe to the ListBox.SelectedIndexChanged event
listBoxInteriorIn the code below, you'll notice that I've used a BindingList which is set as the DataSource for each ListBox.
Form1.cs
Here's a demo:
Update:
To fix the flickering of the ListBox that occurs after selecting a value in one of the ListBoxes and then clicking comboBoxFragrance, add the following code to the form (Form1.cs):
Note: The following code is from here.
Additional Resources: