Hi,
- I have a listbox, called "lbox_Products".
- Next to this box, I have a button "btn_AddProduct".
- I have another listbox, called "lbox_SelectedProducts"
When I select an item in my lbox_Products, and I click on the btn_AddProduct, the selected item from the first lbox_Products, is put in the other lbox_SelectedProducts.
This happens like this:
private void btn_AddProduct_Click(object sender, RoutedEventArgs e)
{
lbox_SelectedProducts.Items.Add(lbox_Products.SelectedItem);
}
This works fine, but what I want to do, is have a single line for each product in lbox_SelectedProducts, preceeded by the number of times that it has been added.
For example:
1 phone
3 displays
6 computers
2 speakersets
...
How can I do this...?
Loading
Sam HobbsPosted Jun 18, 2011, 12:18 PM
I suggest creating a class that has item name and quantity and it can have other items too. You can create an object data source from that class. Then you can bind that data source to the other control then when you need to add items to or change items in the other control you would just add to the data source's collection or change the item in the collection. You could even use the same data source in both controls and use a filter to determine what items are shown in the other control. This all sounds complicated but once you know how to do it you can do it fior many other things and it is actually easier.
seenu BPosted Jun 28, 2011, 1:22 AM
2) follw the code
listBox2.Items.Clear();
for (int i = 0; i < listBox1.SelectedItems.Count; i++)
{
listBox2.Items.Add(listBox1.SelectedItems[i].ToString());
}
Sam HobbsPosted Jun 27, 2011, 10:25 AM
Then in the data sources window, with a form open in the designer, you can click on the node for Products and change the control to use. This is confusing; you will understand better when you can actually do it. Change the control to listbox and then drag and drop the Product field to the form. That will create a listbox bound to the Product field. Then change the Product field back to the control that it had originally. Then drag and drop the class to the form, which will create a DataGridView by default.
The next thing to do is to set the filter for the DataGridView's data source and then put data into and get data from the data source.Let me know when you need that.
Kevin BrigittaPosted Jun 27, 2011, 9:52 AM
I will indeed try it do to it as you have indicated.
Thanks for the responses!
Greetz!
Pradeep ChandrakerPosted Jun 19, 2011, 4:51 PM
Dorababu MekaPosted Jun 18, 2011, 8:12 AM
int cnt = 0;
ArrayList lst = new ArrayList();
private void button1_Click(object sender, EventArgs e)
{
string str = listBox1.SelectedItem.ToString();
lst.Add(str);
if (lst.Contains(str))
{
cnt++;
str = cnt.ToString() + str;
}
if (!(listBox2.Items.Contains(str)))
{
listBox2.Items.Clear();
listBox2.Items.Add(str);
}
else
{
listBox2.Items.Add(str);
}
}
VulpesPosted Jun 18, 2011, 8:08 AM
Kevin BrigittaPosted Jun 18, 2011, 8:08 AM
Krishna GaradPosted Jun 18, 2011, 8:01 AM
static int no=1;
now in button click event write
button_click
{
lbox_SelectedProducts.Items.Add(no.ToString()+lbox_Products.SelectedItem);
no++;
}
it will do what you want.