question

nicksohn-3237 avatar image
0 Votes"
nicksohn-3237 asked GrantPurvis-4325 answered

C#: How do I multiply two objects from seperate list boxes?

For my project, I have to create a shopping cart app. I have two list boxes one with quantities and the other with prices. How would I go about multiplying the two values?

windows-hardware-code-general
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

GrantPurvis-4325 avatar image
0 Votes"
GrantPurvis-4325 answered

First, you would run a for statement for the selected list boxes. It would get the information from which boxes were selected and put them into their own integer. Then you would simply multiply the two.

string selectedItem1 = "";
if (Listbox1.Items.Count > 0) {
for (int i = 0; i < Listbox1.Items.Count; i++) {
if (Listbox1.Items[i].Selected) {
if (selectedItem1 == "") {
selectedItem1 = Listbox1.Items[i].Value;
break;
}
}
}

string selectedItem2 = "";
if (Listbox2.Items.Count > 0) {
for (int i = 0; i < Listbox2.Items.Count; i++) {
if (Listbox2.Items[i].Selected) {
if (selectedItem2 == "") {
selectedItem2 = Listbox2.Items[i].Value;
break;
}
}
}

temp = selectedItem1 * selectedItem2

You would have this for each listbox, then multiply selectedItem1 and selectedItem2.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.