Hey all, I am currently creating a program that is like an automated food ordering menu.

Imagine a form that has two listboxes (or listviews), the first listbox contains the items that can be ordered, I stored these items into an array and displayed them using a for loop construct on the first listbox. The second listbox contains three columns one for "Food Name" another for "Quantity" and another one for "Price". And between the two boxes there is a button that when pressed transfers a selected item from the first listbox to the next listbox.

So what I have done so far was to display the items on the first listbox, and then transfer a selected item from the first box to the second with the press of the button.

My question now is, how do I access the other columns so that I could fill them up? I know that to add contents in a listbox from one to the other goes something like:
secondlistbox.items.add(firstlistbox.selecteditem)

But my problem with that is it just transfers the item to the first column and that I do not know how I could somehow put in data on the other two columns. I also do not know how to put in the price based on the selected item, what I thought of was creating two arrays, first for the item names and the second for their prizes? how do I go about transferring the price to the third column, with respect to the selected item?

What should I use for my solution a listbox or listview? they seem like the same and looks to me like there are no differences between the two.

And lastly, I want to create my custom made dialog box, in which when the user adds the item to the second listbox, it immediately asks the user the quantity of the current item they would like to purchase, and display that quantity and the computed price (quantity*priceOfProduct) in the second and third columns respectively? I have created and designed another form within my solution that shall become the dialog box, and the only problem is how do I get it to pop up, get the user to type in data to the textbox and then return the data to the second column and use the data returned to compute for the price.

Thank you very much to those who'll be having the patience to help me on this. I can post my very very small code if anyone wants to see what my gui looks like.

Thanks!

ListBox and ListView are both meant to display a list of items. So your first ListBox seems ok to me. For the second I should consider a DataGridView, this would make your well crafted dialog window obsolete though.

Edited 6 Years Ago by ddanbe: n/a

But as I have been reading datagridview's need an sql database to work with? correct me if I am terribly wrong though.

I would just like to use simple listbox and listview controls for my work for now, and maybe work up the ladder as I improve my work.

No, a DataGridView is normally used in connection with a sql DB, but it can be used as is. I used it as an input sheet for matrix data for instance. In your case I also would advise to keep it simple, but a ListView is not your thing.

Oh, okay. But I have done the code using listview now, and the only thing I am lacking is how do I get a value inputted in a textbox from my custom dialog box, to my main form?

If you define a field or better still a property in your dialog form class that contains the text of your textbox in the dialog, you could access it after a DialogResult.OK

Okay here is what I did, considering your suggestion of having a property in my dialog class that can be accessible through my main form I did this:

public partial class getQuantity : Form
    {
        private string returnString;

        public getQuantity()
        {
            InitializeComponent();
        }

        public string itemQuantityReturn{
            get
            {
                return returnString;
            }

            private set
            {
                returnString = value;
            }
        }
    }

and in my main form I did this:

private void buttonAdd_Click(object sender, EventArgs e)
        {
            ListViewItem orderItem = new ListViewItem();
            getQuantity getQuantityDiag = new getQuantity();

            orderItem.Text = (string)menuList.SelectedItem;
            if (getQuantityDiag.ShowDialog() == DialogResult.OK)
            {
                orderItem.SubItems.Add(Convert.ToString((Int32.Parse(getQuantityDiag.itemQuantityReturn))*3));
                orderItem.SubItems.Add(Convert.ToString(getQuantityDiag.itemQuantityReturn));
            }
            orderList.Items.Add(orderItem);

            
 
        }

It does not compile D: also when I comment the line that makes my code to not compile I still could not display the entered item in the dialog box into my main form.

IMO you don't have to make a separate class for returnstring.
Put itemQuantityReturn in your getQuantityDiag. Also put textchanged hanlder in this class which updates the returnstring with the text in the textbox of your dialog.

But I only made a new class, because I had to create a new form for my dialog box.

Your dialogbox IS a form (or should be)
getQuantityDiag should contain what you have in your getQuantity class.
Have to quit this interesting conversation for now. Make the best of it if you can. See you soon.:)

I am now successful with the dialog box and transferring of data between the list box and listview but now I am now getting this error message:

Cannot add or insert the item 'item2' in more than one place. You must first remove it from its current location or clone it.
Parameter name: item

I get this message whenever I try to add more items, what should I add unto my code?

Here is the part where it experiences problems:

private void buttonAdd_Click(object sender, EventArgs e)
        {

            if (getQuantityDiag.ShowDialog() == DialogResult.OK)
            {
                orderItem.Text = Convert.ToString(menuList.SelectedItem);
                orderItem.SubItems.Add(getQuantityDiag.getItemQuantity.Text);
                orderItem.SubItems.Add("P " + ((Double.Parse(getQuantityDiag.getItemQuantity.Text))*itemEntreePriceList[(menuList.SelectedIndex)]).ToString("N2"));
                orderList.Items.Add(orderItem);

                totalCost += ((Double.Parse(getQuantityDiag.getItemQuantity.Text))*itemEntreePriceList[(menuList.SelectedIndex)]);
                itemTotal.Text = "Amount Total: P "+ totalCost.ToString("N2");
            }

            
        }

Also how do I remove an item in a list view if it consists of subitems? for example I would like to remove a row of items that are of the same being but contains different properties?

Edited 6 Years Ago by ticktock: n/a

I'm assuming the error occurs on line 9.
The error message means that you already have added orderItem somwhere else in your code, orderItem is item2 you have added.
And to answer your last question: Use the remove item of the ListView.ListViewItemCollection.

This question has already been answered. Start a new discussion instead.