how to delete selected item in C# - c#

I create a listview called listBoxobj. I want to delete the listview based on the selected listview but right now the problem is that when deleting it will delete by index not the selected item.
This is for an apps develops in visual studio,running SQLite.
i've already tried to use this code but keep failed
foreach ( ListViewItem eachItem in listBoxobj.SelectedItems)
{
listBoxobj.Items.Remove(eachItem);
}
I want the selected listview will be deleted instead the listview deleted by index
private void DeleteMV_Click(object sender, RoutedEventArgs e)
{
DatabaseHelperMV_Meriam delete = new DatabaseHelperMV_Meriam();
x = listBoxobj.SelectedItems;//this line keep getting error
delete.DeleteId(x);
Frame.Navigate(typeof(MV_MeriamViewData));
}

For WinForms:
You delete list view selected item using this:
listBoxobj.SelectedItems[0].Remove();
This code you have:
foreach ( ListViewItem eachItem in listBoxobj.SelectedItems)
{
listBoxobj.Items.Remove(eachItem);
}
Is corrected if you have multiselect = true... but if you have multiselect = false, use this: listBoxobj.SelectedItems[0].Remove();
For WPF:
private void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var selected = listBoxobj.SelectedItems.Cast<Object>().ToArray();
foreach (var item in selected)
{
listBoxobj.Items.Remove(item);
}
}

Related

How get checkbox object from listview in wpf

I'd like to make checkboxlistview in wpf application like this image
)
If I check any item on left listview the items of right one should be checked.
I can't use binding, because It's shared project and I need to add UWP project after finish WPF project.
So I can't make binding items to VeiwModel for this project.
I'd like to catch the click event of left items and set the check property to right items. But I can get the checkbox objects from the right listview.
void OnCheckboxClicked1(object sender, RoutedEventArgs e)
{
var checkBox = sender as CheckBox;
var id = checkBox.Tag;
CheckSubList(checkBox.IsChecked, id.ToString());
}
void OnCheckboxClicked2(object sender, RoutedEventArgs e)
{
}
void CheckSubList(bool? isChecked, string Id)
{
foreach (object listView in LogicalTreeHelper.GetChildren(rightListView as FrameworkElement))
{
Console.WriteLine("OKOK");
//foreach (object checkbox in LogicalTreeHelper.GetChildren(obj as FrameworkElement))
//{
// // Some code
//}
}
}
If anyone have a good idea, please help me. Thanks

How to show items from a listview group when selected from combobox

I am trying to display items in a listview from groups and here is what I mean.
I added a listview to a form and in the listview I added a 2 groups then I added items and for the items I chose a group name.
Now in a combox box I add in the selectindexchanged event I put this.
if (comboBox1.Text == "group1")
{
foreach (string itemname1 in listimages.Groups[0].Items)
{
string currentitem = itemname1;
}
}
nothing is working so I am trying to figure out what I am not doing right.
The items in the combobox have the same items as group names.
Any help would be great.
I was able to figure it out. Here is what I did
First on the listview I added Groups, then I added items and on the items in the Tag property I put in the group I wanted it to be linked to.
In the comboBox I put in group names as I typed them when I added them to the listview.
Then I added this code :
private void frmImageSelection_Load(object sender, EventArgs e)
{
items = new ListViewItem[listimages.Items.Count];
listimages.Items.CopyTo(items, 0);
ShowGroup(0);
cmbgroups.SelectedIndex = 0;
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
ShowGroup(cmbgroups.SelectedIndex);
}
void ShowGroup(int index)
{
if (index == 0) // all
{
listimages.Items.Clear();
listimages.Items.AddRange(items);
}
else
{
listimages.Items.Clear();
foreach (ListViewItem item in items)
if (listimages.Groups[index].Name.Equals(item.Tag))
listimages.Items.Add(item);
}
foreach (ListViewItem item in listimages.Items)
item.Group = listimages.Groups[index];
}
ListViewItem[] items;
Anyway if you are not sure I will be glad to help you out just drop me a message in my inbox or something.

C#: How to select the top item in a listview after sorting

I'm filling my System.Windows.Forms.ListView with results from my database as such:
foreach (DataRow row in theTable.Rows)
{
...build item from row..
myListView.Items.Add(item);
}
And then I want to sort my listview in a different order than the rows come back from the DB, so I call
myListView.Sort();
But then when I want to go to select the top item in the listview it won't work, it selected something other than the top item:
myListView.Items[0].Selected = true;
Makes sense since the Items collection is added to in the order of the rows from the table iterated through in the foreach loop.
Using myListView.TopItem.Seleted = true doesn't work either.
So how do I go about selecting the topmost item in the listview AFTER I've sorted it?
Thanks for any answers.
Are you sure the list view is currently selected? If it's not selected you will not see the item being selected.
The following code seem to be working:
private void Populate(object sender, EventArgs e)
{
listView1.Items.Add("D");
listView1.Items.Add("B");
listView1.Items.Add("A");
listView1.Items.Add("C");
}
private void SelectFirst(object sender, EventArgs e)
{
listView1.Items[0].Selected = true;
listView1.Select();
}
private void SortAndSelect(object sender, EventArgs e)
{
listView1.Sorting = SortOrder.Ascending;
listView1.Sort();
listView1.Items[0].Selected = true;
listView1.Select();
}
Notice the listView1.Select()
You probably have HideSelection set to true. Make it false, and try.
myListView.HideSelection = false;
Furthermore listviews can have a selected item but with no focus on some other item. So its better to set both focus and selection together:
if (myListView.Items.Count > 0)
{
myListView.Items[0].Selected = true;
myListView.Items[0].Focused = true;
}
If that doesn't work you can set focus to listview itself to see if selection is falling on right item.

Get selected Row in ListView

I have a Listener on the DoubleClick event in a ListView. I have also activated FullRowSelect.
So when I double-click a row, only the value in the first column appears. I also tried it directly with SelectedItems.
Please help
Code:
private void lvRecipesPos_DoubleClick(object sender, EventArgs e)
{
String s = "";
foreach (ListViewItem item in lvRecipesPos.Items)
{
if (item.Selected == true)
{
s += item.Text.ToString();
}
}
MessageBox.Show(s);
}
1) The ListView has a SelectedItems collection, so you don't have to iterate all items and check if they're selected.
2) Item has a SubItems collection which holds the texts for all sub items

How to delete selected Items from a ListView by pressing the delete button?

I want to delete one or more selected Items from a ListView.
What is the best way to do that?
I´m using C# and the dotnet Framework 4.
You can delete all selected items by iterating the ListView.SelectedItems collection and calling ListView.Remove for each item whenever the user pressed the delete key.
private void listView1_KeyDown(object sender, KeyEventArgs e)
{
if (Keys.Delete == e.KeyCode)
{
foreach (ListViewItem listViewItem in ((ListView)sender).SelectedItems)
{
listViewItem.Remove();
}
}
}
I think there is something called listView.Items.Remove(listView.SelectedItem) and you can call it from your delete button's click event. Or run a foreach loop and see if the item is selected, remove it.
foreach(var v in listView.SelectedItems)
{
listView.Items.Remove(v)
}
I think this is the easiest mode.
private void listView_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Delete)
{
this.listView.Items.Remove(listView.SelectedItem);
}
}
Try this:
// Get an array of all selected items
ListViewItem[] selectedItems = (from i in listView.Items where i.Selected select i).ToArray();
// Delete the items
foreach (ListViewItem item in selectedItems)
listView.Items.Remove(item);
EDIT
I just noticed that the ListView class already has a SelectedItems property. To make sure that you're not changing the collection you're iterating on, I'd copy that collection first:
Seems the above (using AddRange) did not work. I thought that removing the items by iterating over the SelectedItems enumerable would cause an exception, but obviously it does not. So my original code code be modified to match the other answers... sorry for posting non-functional code...
I know this is a bit unrelated but In WPF the mentioned methods did not work for me. I had to create a copy of the selected items and use these to remove items in the listview.:
private void ListBox_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
if (e.Key == System.Windows.Input.Key.Delete)
{
var lst = new List<object>();
foreach (var itemSelected in ListBox.SelectedItems)
{
lst.Add(itemSelected);
}
foreach (var lstitem in lst)
{
ListBox.Items.Remove(lstitem);
}
}
}

Categories

Resources