How can I remove items from the listbox and the List at the same time? The User selects has the listbox click the button to remove, it removes the listbox and list and then updates the listbox.
I'm not using as datasource, I added items in the listbox with foreach
mylist.ForEach(delegate(list add)
{
listBox1.Items.Add(list.ITEM + list.VALOR [...]);
}
);
Solution:
mylist.RemoveAt(listbox.SelectedIndex);
Related
I'm trying to move the selected items from checkedlistbox to listbox.But I can't...
for (int x = 0; x<=checkedListBox1.Items.Count;x++ )
{
if (checkedListBox1.GetItemChecked(x))
{
listBox1.Items.Add(checkedListBox1.SelectedItem + "\r\n");
}
}
Please help me
Below code will add selected items to listBox1 and remove it from checkedListBox1, but SelectedItems will be one item each time because Multi-selection is not supported on CheckedListBox you can use CheckedItems instead of SelectedItems to select more than one item.
// Add Selected Items to ListBox
listBox1.Items.AddRange(checkedListBox1.SelectedItems.OfType<object>().ToArray());
// Remove from CheckedListBox
foreach (var item in checkedListBox1.SelectedItems.OfType<object>().ToList())
{
checkedListBox1.Items.Remove(item);
}
So I have a class "Worker" and a BindingList which is bound to a ComboBox.
workers = new BindingList<Worker>();
//// .. An "Add" button is somewhere there to add "Worker" objects to the list
//ComboBox Workers List
cbWorkersList.DataSource = workers;
cbWorkersList.DisplayMember = "Name";
I want to bind the "Text" field in a TextBox to the "Name" property of the selected "Worker" object, in the ComboBox.
Now I've done it by using the "cbWorkersList_SelectedIndexChanged" event handler, manually modifying txtWorkerName.Text property:
private void cbWorkersList_SelectedIndexChanged(object sender, EventArgs e)
{
if (cbWorkersList.SelectedItem != null)
{
txtWorkerName.Text = workers[workers.IndexOf((Worker)cbWorkersList.SelectedItem)].Name;
}
}
I tried to bound the TextBox in this way:
InitializeComponent();
txtWorkerName.DataBindings.Add("Text", cbWorkersList, "Text");
But it is only updating the TextBox if I click on other item on the list, if I use arrow keys to change the selection, the TextBox text field is not changing.
Is there a way to truly bind them so that if a ComboBox item is selected, the text field will change accordingly?
While on this topic, I also have other 2 ComboBoxes, and I would like to bind 1 to a list which is inside the others' ComboBox object.
What I want is a solution with the following logic:
cbPositions.Items = ((<Shift>)cbShifts.SelcetedItem).PositionsList
when
cbPositions is a ComboBox which should be populated with "Position" objects.
And cbShifts is a ComboBox which is populated with "Shift" objects, where each "Shift" contains a list of Positions.
How can I bind them so every time I choose an other Shift from the list, the Positions ComboBox will be populated with the Position objects from the Shift.PositionList?
And also, I would like to know if it's possible to bind the "Items" of a ComboBox to a List (my workers list in this example), not with code, but in the designer(Application Settings or DataBindings)?
How do I get the row index or record Position of all items in gridData.SelectItems property.
SelectedItems Contains actual data (ViewModel) from the collection bound to datagrid's ItemSource. Therefore you can obtain the index in that collection for every item selected.
if its a datagridview on winforms then this would work
List<int> rowIndexs = new List<int>();
foreach (DataGridViewRow item in gridData.SelectedRows)
{
rowIndexs.Add(item.Index);
}
My collection:
private ObservableCollection<Data> listData;
ListBox source:
ListBoxData.ItemsSource = listData;
Later i add elements 20 times at start of collection:
ListBoxData.Insert(0, new Data());
After adding ListBox scrolls to first element of collection. How to disable scrolling after adding?
does this one works for you? ListBox .SelectedIndex = 0;
i have ListView with items
one ListView with items another one is empty
i need to copy from first ListView selected item to another ListView at the same time
i have to remove the selected item in first ListView in C#
Rather than removing items from the collection that you're enumerating (as per Wael's answer), which is a "bad idea", use a temporary collection, in this instance a List to store them in before removing them:
List<ListViewItem> itemsToMove = new List<ListViewItem>();
foreach (ListViewItem item in listView1.SelectedItems)
{
itemsToMove.Add(item);
}
foreach (ListViewItem item in itemsToMove)
{
listView1.Items.Remove(item);
listView2.Items.Add(item);
}
Where listView1 is the list with the selected items and listView2 is the list to move them to.
this code will copy from the first listview1 to listview2 all the selected items and delete it from listview1
foreach (ListViewItem itm in ListView1.SelectedItems)
{
ListView1.Items.Remove(itm);
listView2.Items.Add(itm);
}