The following data are displayed in listbox1. I want to pass items from listbox2 to listbox1. The result should be look as shown below (lb1).How can this be achieved?
Many thanks for your help!
var itm = listBox2.Items[0].ToString();
listBox1.Items.Add(itm);
enter image description here
Use AddRange Method. Try like:
listBox1.Items.AddRange(listBox2.Items);
You can use AddRange
listBox1.Items.AddRange(listBox2.Items);
Or if you want an exact copy of what is in the other listbox without your old items.
listBox1.Items = listBox2.Items;
Related
Is it possible to insert a new item to a listbox at a specific line?
I already tried litsbox1.Items.Add() but this method did not accept any index.
you can try
listBox1.Items.Insert(desiredIndex, yourObject);
Hope this is what you want..
I have a ListBox filled with Countries (which I take from the Active Directory). I want the list to be sorted, but also I want one entry "All" to be at the very top.
How can I do this?
If you are binding the data in code behind you can insert a Listitem at index 0.
ListItem myItem=new ListItem("ALL","value");
myListbox.Items.Insert(0, myItem);
I would sort the list items first before binding to you ListBox. There are several options for doing this depending on what your data source is i.e. DataTable, List, Dictionary etc. To insert an item use code below.
lstCountries.Items.Insert(0, new ListItem("All", "0"));
After you load the data (i.e Countries), add the ListItem as follows:
myListbox.Items.Add(new ListItem() { Text = "All", Value = "" });
myListbox.SelectedIndex = 0; //This line will selected the first item on your ListBox.
Here, you might consider which action take if the ListBox with text "All" is selected.
I'm trying to sort the items in a ListBox. However, upon doing so the Item Value gets set to the Item Text. Any help would be appreciated.
lbxCustomers.DataSource = lbxCustomers.Items.Cast<ListItem>().Reverse().ToList();
lbxCustomers.DataBind();
May be first you should store the list in generic collection and then sort it. Something like this:
List<ListItem> list = new List<ListItem>(lbxCustomers.Items.Cast<ListItem>());
list = list.OrderBy(li => li.Text).ToList<ListItem>();
lbxCustomers.Items.Clear();
lbxCustomers.Items.AddRange(list.ToArray<ListItem>());
Try resetting the DisplayMember and ValueMember properties of the Listbox after setting the DataSource. A lot of times if a control is already bound and you set the DataSource again, then it drops the DisplayMember/ValueMember properties.
Try sorting the list separately.
Point the Text value and Data value in the next step.
Databind the control.
How can I delete an item from listbox at particular location and not the SELECTED one?
Let's say if I have 4 items in listbox and I want to delete the one at index 2, how can I do that without selecting it.
ListBox.Items should be a List. More precise a ListBox.ObjectCollection
Have you tried :
myListBox.Items.RemoveAt(2);
RemoveAt refference in MSDN
listBox1.Items.RemoveAt(position);
ListBox1.Items.RemoveAt(anyindex);
Is it possible to set the selected item of a combobox to be an object that is not in its dropdown list?
If yes, then what must one do?
Use the Text property:
comboBox.Text = "I'm not in the list!";
If you want to add the item to the list, use the Items collection:
comboBox.Items.Add("I was added to the list!");
Is there a reason you cannot add it to the list after the object was generated? Is the object generated asynchronously?
myComboBox.Items.Add(newItem);