Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8758925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:40:24+00:00 2026-06-13T14:40:24+00:00

I have a List Box with different Cakes in them. How do I give

  • 0

I have a List Box with different Cakes in them. How do I give each cake a price and have my label display the total cost of the selected cakes? The following is the code I have so far.

for (int index = 0; index < lstCakes.SelectedItems.Count; index++)
        {
            strCakes += Environment.NewLine + lstCakes.SelectedItems[index].ToString();
        }

double tax = 1.13;
        lblOrdered.Text = "You have ordered: " + strCakes + '\n' + "Total Cost: " + (tax * cakeCost).ToString("C");

I tried using switch like the following but that only shows the cost of the last selected item.

switch (lstCakes.SelectedIndex)
        {
            case 0:
                if (lstCakes.SelectedIndex == 0)
                {
                    cakeCost = 18;
                }
                break;
            case 1:
                if (lstCakes.SelectedIndex == 1)
                {
                    cakeCost = 25;
                }
                break;
            case 2:
                if (lstCakes.SelectedIndex == 2)
                {
                    cakeCost = 40;
                }
                break;
            case 3:
                if (lstCakes.SelectedIndex == 3)
                {
                    cakeCost = 30;
                }
                break;
        }

Any suggestion are appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T14:40:25+00:00Added an answer on June 13, 2026 at 2:40 pm

    Assuming that this is a desktop application, you will probably want to put your prices in a config file so that they can be changed later. You add an <appSettings> block to your App.config file with an entry for each cake and then use the ConfigurationManager.AppSettings[] command to retrieve them.

    So if this is a Windows Forms app, then on the form load you can go into your app settings, retrieve the details for all the cakes that you want and then populate your list box with entries for each one (see http://msdn.microsoft.com/en-us/library/z38x31c0.aspx). This way you can dynamically create the text for each entry. If you want each line to contain the price you are going to have to hardcode it into the line’s text. (I think that is what you are asking…)

    One final note. You shouldn’t use + to concatenate strings. Strings in C# are immutable — that means that the string itself cannot be modified (the reason why is a whole other topic that I can explain if you wish). In order to concatenate two strings with “+”, C# needs to create a third string and fill it with the contents of the first two, and this drains performance. To concatenate strings more efficiently, use either a StringBuilder object and the Append() method, or use String.Format() which works the same way.


    Immutable strings:

    Strings at their core are arrays of characters. Just as you cannot resize an array, you cannot resize a string. This is because arrays are stored on the stack… the stack is a piece of memory which is filled with instructions to run your program that are all “stacked” on top of each other. Stack memory is pre-allocated and for all intents and purposes you cannot dynamically change the memory footprint of objects on the stack. You can have an array of 10 ints containing 5 ints and 5 empty spaces, but you cannot take an int[5] and change it to an int[10]. If you want to add 5 more ints to an int[5] you need to instantiate a new int[10] and fill it up. The same thing goes for a string.

    The solution to the array resizing problem is dealt with using Lists and their derivatives. They function using heap memory. This is similar to how a StringBuilder object functions. If you want to know more about stack and heap memory and how it affects how your program runs, this might help you understand a bit better http://www.c-sharpcorner.com/UploadFile/rmcochran/csharp_memory01122006130034PM/csharp_memory.aspx. It is really important to know, because it can explain a lot of mysteries that will stump beginner programmers. Good for you for asking.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a list box with different items, each with unique values, and
I want to have a drop down box that has a list of different
I have three different List collections. How can I show them in one ListBox
I have a list box control that contains enough items to list them with
I have a list box that populates with different sets of data based on
I have a List box that I want to display a list of objects,
i have list box that contain a group of different language : <%@ Page
I want a ListBox full of items. Although, each item should have a different
I have a list box like this, <asp:ListBox ID=ListBox1 runat=server Height=175px Width=213px> <asp:ListItem Value=all>All</asp:ListItem>
I have the list box control (ASP.NET Control On aspx page, language C# ).

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.