11 Replies - 793 Views - Last Post: 10 April 2015 - 08:03 PM Rate Topic: -----

#1 kdeff41   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 01-April 15

Using listboxes in Visual Studio

Posted 10 April 2015 - 09:37 AM

Hello all, I am having some trouble creating a listbox in visual studio. I declared an array, set initial values, and now I would like to be able to update those values. It is a pizza inventory app, so I need to update the initial values once the user has added inventory. This is the code I have for the "Update Inventory" button:
            // read in value and convert to double
            double dblInput = Convert.ToDouble(txtInput.Text);

           
            // loop through ingredients and add inventory to selected ingredient 
            for (int i = 0; i < dblInventory.Length; i++)
            {
                // is item at i checked for update?
                if(lstInventory.GetItemCheckState(i) == CheckState.Checked)
                {
                    dblInventory[i] += dblInput;
                }
            
            }
            lstInventory.SelectedItem = dblInventory.ToString();


Unfortunately nothing happens when I do this. Any help would be very appreciated!

Variable Declarations are as such:

            //initialize ingredient list
            lstInventory.Items.Add("Dough 0 lbs");
            lstInventory.Items.Add("Sauce 0 oz");
            lstInventory.Items.Add("Cheese 0 oz");
            lstInventory.Items.Add("Pepporoni 0 oz");
            lstInventory.Items.Add("Sausage 0 oz");
            lstInventory.Items.Add("Mushrooms 0 oz");

            // set initial ingredient values in array
            Array.Resize(ref dblInventory, 6);
            dblInventory[0] = dblDough;
            dblInventory[1] = dblSauce;
            dblInventory[2] = dblCheese;
            dblInventory[3] = dblSausage;
            dblInventory[4] = dblPepporoni;
            dblInventory[5] = dblMushrooms;

This post has been edited by andrewsw: 10 April 2015 - 09:39 AM
Reason for edit:: Added [code][/code] tags


Is This A Good Question/Topic? 0
  • +

Replies To: Using listboxes in Visual Studio

#2 andrewsw   User is offline

  • palpable absurdity
  • member icon

Reputation: 6905
  • View blog
  • Posts: 28,565
  • Joined: 12-December 12

Re: Using listboxes in Visual Studio

Posted 10 April 2015 - 09:38 AM

How to use code tags:

Attached Image
Was This Post Helpful? 0
  • +
  • -

#3 andrewsw   User is offline

  • palpable absurdity
  • member icon

Reputation: 6905
  • View blog
  • Posts: 28,565
  • Joined: 12-December 12

Re: Using listboxes in Visual Studio

Posted 10 April 2015 - 09:45 AM

lstInventory.SelectedItem = dblInventory.ToString();

dblInventory is an array so this line won't result in anything meaningful. But I'm not sure what you are trying to achieve with this?
        dblInventory[i] += dblInput;

Are the array values not increased? How are you confirming this?
Was This Post Helpful? 0
  • +
  • -

#4 kdeff41   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 01-April 15

Re: Using listboxes in Visual Studio

Posted 10 April 2015 - 09:58 AM

with
 dblInventory[i] += dblInput;

I am trying to add what the inout from the user is to the ingredient at position [i] in the array
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw   User is offline

  • palpable absurdity
  • member icon

Reputation: 6905
  • View blog
  • Posts: 28,565
  • Joined: 12-December 12

Re: Using listboxes in Visual Studio

Posted 10 April 2015 - 10:01 AM

Does it not work? How do you know? How are you inspecting the array to see whether the values have been increased or not?
Was This Post Helpful? 0
  • +
  • -

#6 kdeff41   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 01-April 15

Re: Using listboxes in Visual Studio

Posted 10 April 2015 - 10:05 AM

I may be completely off here. I am starting to think I don't even need to use a loop. Basically what I want is to create a listbox of all the ingredients i need so that when the user runs the application they see the list of ingredients and can select them and "update inventory"

Well it may be working within the array itself but maybe I am just not outputting it right to the interface?
Was This Post Helpful? 0
  • +
  • -

#7 andrewsw   User is offline

  • palpable absurdity
  • member icon

Reputation: 6905
  • View blog
  • Posts: 28,565
  • Joined: 12-December 12

Re: Using listboxes in Visual Studio

Posted 10 April 2015 - 10:06 AM

So what should happen when the "Update Inventory" button is pressed?

View Postkdeff41, on 10 April 2015 - 05:05 PM, said:

Well it may be working within the array itself but maybe I am just not outputting it right to the interface?

Possibly.
Was This Post Helpful? 0
  • +
  • -

#8 kdeff41   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 01-April 15

Re: Using listboxes in Visual Studio

Posted 10 April 2015 - 10:07 AM

when the "Update Inventory" is pressed it should loop through the array to determine what position [i] is selected by the user. It should then add the input to that position and output it so that the user can see how much inventory there is
Was This Post Helpful? 0
  • +
  • -

#9 andrewsw   User is offline

  • palpable absurdity
  • member icon

Reputation: 6905
  • View blog
  • Posts: 28,565
  • Joined: 12-December 12

Re: Using listboxes in Visual Studio

Posted 10 April 2015 - 10:18 AM

That description isn't clear to me. Maybe a screenshot would help, but a clear description will still be needed.

Are you looping through an array (of numbers?) or a CheckedListBox?
What are you adding to..? etc..
What is the output, how is it displayed?

I will also add that the more clearly that you describe your question the nearer you will be to a solution.

This post has been edited by andrewsw: 10 April 2015 - 10:16 AM

Was This Post Helpful? 0
  • +
  • -

#10 kdeff41   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 01-April 15

Re: Using listboxes in Visual Studio

Posted 10 April 2015 - 12:20 PM

I actually figured out my problem. I didn't need to loop through my array. Here is the code I came up with that seems to be working, if you're interested:
// read in value and convert to double
            double dblInput = Convert.ToDouble(txtInput.Text);  
                // determine which ingredients were selected, and update accordingly
                if(lstInventory.SelectedIndex == 0)
                {
                    dblDough += dblInput;
                }
                if(lstInventory.SelectedIndex == 1)
                {
                    dblSauce += dblInput;
                }
                if(lstInventory.SelectedIndex == 2)
                {
                    dblCheese += dblInput;
                }
                if(lstInventory.SelectedIndex == 3)
                {
                    dblSausage += dblInput;
                }
                if(lstInventory.SelectedIndex == 4)
                {
                    dblPepporoni += dblInput;
                }
                if(lstInventory.SelectedIndex == 5)
                {
                    dblMushrooms += dblInput;
                }



Was This Post Helpful? 0
  • +
  • -

#11 andrewsw   User is offline

  • palpable absurdity
  • member icon

Reputation: 6905
  • View blog
  • Posts: 28,565
  • Joined: 12-December 12

Re: Using listboxes in Visual Studio

Posted 10 April 2015 - 12:53 PM

Well I'm glad you sorted ;)

Longer term though, this isn't a great solution. The UI is too tightly bound to the data. You are essentially maintaining "parallel arrays" and assuming that the UI order (as the elements appears in the ListBox) will always agree with the order of the data-store (the array of numbers). If you modify, or add to, the items in your inventory, then you will have to manually modify the ListBox and, probably, its element-order.

It would be better to create a class to encapsulate the inventory items - their descriptive text and value - and a list of (presumably) purchases. You could then bind the ListBox to an inventory list.

Anyway, just food for thought (excuse the pun) for later ;)
Was This Post Helpful? 0
  • +
  • -

#12 kdeff41   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 01-April 15

Re: Using listboxes in Visual Studio

Posted 10 April 2015 - 08:03 PM

Oh yes I am sure there are much better ways than what I am doing :) I am new to coding and in a class for it now. We are actually learning object oriented programming and about classes this coming week.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1