ListView Item select in winform - c#

I want to select item in a ListView upon clicking. I also want to know what I clicked.
I work on winforms with c#.I also want to know How I can clicking the all row?

Just handle the Click event on the list and use the ListView.SelectedItems property to get what items are selected:
private void listView1_Click(object sender, EventArgs e)
{
var firstSelectedItem = listView1.SelectedItems[0];
}

u can use MouseEventArgs and get the mouse location check if it exists inside the selected item bound , that means the click was made on the selected item .
EDIT :
example :
private void myList_MouseDoubleClick(object sender, MouseEventArgs e)
{
if (myList.SelectedItems.Count >= 1)
{
ListViewItem item = myList.SelectedItems[0];
//here i check for the Mouse pointer location on click if its contained
// in the actual selected item's bounds or not .
// cuz i ran into a problem with the ui once because of that ..
if (item.Bounds.Contains(e.Location))
{
MessageBox.Show("Double Clicked on :"+item.Text);
}
}
}

also if you use xaml for window then you must add MouseUp="listView1_Click" attribute to ListView tag

Related

ObjectListView get object with contextmenu

Hy!
I would like to create an ObjectListView, where you can delete items with a ContextMenu.
So basicly I used to delete it by getting OLV.SelectedIndex, and then deleting from the list OLV is based on, and re-setting the OLV objects.
Then I realized, if I sort the OLV, then delete an item, it deletes another item, since the selected item index does not equals the index in the list.
With OLV CellRightClick event I can get the object behind the clicked item (e.Model),but I dont know how to pass it to the ContextMenu click event handler.
Subjects is a List.
private void subjectListView_CellRightClick(object sender, BrightIdeasSoftware.CellRightClickEventArgs e)
{
if (subjectsListView.SelectedIndex != -1)
{
ContextMenu cm = new ContextMenu();
cm.MenuItems.Add("Delete", new EventHandler(DeleteItem));
subjectsListView.ContextMenu = cm;
}
}
void DeleteItem(object sender, EventArgs e)
{
//get the Subject object, which was clicked on
Subjects.RemoveAt(subjectsListView.SelectedIndex);
subjectsListView.SetObjects(Subjects);
}
So basically I want to get the object (not the index) when the ContextMenus "Delete" item is clicked.
Also, I feel like there is an easier way to do this.
Thanks for the answer.
I would just assign an appropriate ContextMenuStrip from the designer to the ObjectListView.ContextMenuStrip property and then handle the click of the corresponding "Delete" click like this:
private void deleteToolStripMenuItem_Click(object sender, EventArgs e) {
if (objectListView1.SelectedObject != null) {
objectListView1.RemoveObject(objectListView1.SelectedObject);
}
}
Or is there a requirement I am missing from your question?

Using mouse click event to get ListViewItem text

I want to use listview to populate it with data and then use mouseclick event to fill some textboxes with data. I looked up an example in msdn:
ListViewItem theClickedOne = listView1.GetItemAt(e.X, e.Y);
ListViewItem theClickedtwo = listView1.FocusedItem;
if (theClickedOne != null)
{
MessageBox.Show(theClickedtwo.ToString());
//do your thing here.
//there is a reference to the listview item we clicked on
//in our theClickedOne variable.
}
but I couldn't think about a way to use it in order to differentiate the listviewitems I use since the fist Column in my program is the same and it will only give me a string with it's name(first Column).I want to have something similar to next example but for treeview.
void treeView1_NodeMouseClick(Object sender, TreeNodeMouseClickEventArgs e)
{
MessageBox.Show(e.Node.Text);
}
When populating you ListView, set the Tag property of the items, e.g.
newItem.Tag = "Item 1";
The Tag property has type object, so you can use anything you want here to identify the item. When handling the mouse click event simply check the Tag value again:
if((string)(clickedItem.Tag) == "Item 1")
{
// do stuff for this specific item.
}

get the clicked item from a IsItemClickEnabled ListView in C# and WinRT

how can I get the clicket elevent from a ListView which has the IsItemClickEnabled enabled?
I know how to get the selected Item/Index but not the clicked item.
ItemClick is working but I can not say s.th. like:
Object selection = listView1.SelectedItem;
EDIT:
I have a ListView and I need to catch the clicked item from this list in the following method:
private void listView1_ItemClick(object sender, ItemClickEventArgs e)
{
...
}
I may be missing something, but doesn't the following work for you?
private void lv_ItemClick_1(object sender, ItemClickEventArgs e)
{
var item = e.ClickedItem as String;
}
Here I assume the items in the list are simple strings, but in general they'll be whatever type you are using in the collection you've bound to the ItemsSource property of the ListView.
I guess you can also try the SelectionChanged event, and get the clicked item as e.AddedItems or MyListView.SelectedItem or MyListView.SelectedItems.

Prevent user from deselecting an item in a ListBox?

I've got a ListBox with a bunch of items in it. The user can click an item to edit its contents. How do I prevent the user from deselecting all items? i.e., the user shouldn't be able to have nothing selected.
There is a case missing in your situation, which is when the list is cleared you will reselect an item there is no longer on the list. I solve this by adding an extra check.
var listbox = ((ListBox)sender);
if (listbox.SelectedItem == null)
{
if (e.RemovedItems.Count > 0)
{
object itemToReselect = e.RemovedItems[0];
if (listbox.Items.Contains(itemToReselect))
{
listbox.SelectedItem = itemToReselect;
}
}
}
I then put this inside a behaviour.
I'm not sure if there is a direct way to disable deselecting an Item, but one way which would be transparent to the user is to keep track of the last selected Item, and whenever the SelectionChanged event is raised and the selected index is -1, then reselect the last value.
This Works for Sure to Prevent User from Deselect... Add those 2 Events to your checkedListBox1 and set the Property CheckOnClick to "True" in Design Mode. (MSVS2015)
private void checkedListBox1_SelectedValueChanged(object sender, EventArgs e)
{
checkedListBox1.SetItemChecked(checkedListBox1.SelectedIndex, true);
}
private void checkedListBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
checkedListBox1.SetItemChecked(checkedListBox1.SelectedIndex, true);
}
To disable on or more options in your listbox/dropdown, you can add the "disabled" attribute as shown below. This prevent the user from selection this option, and it gets a gray overlay.
ListItem item = new ListItem(yourvalue, yourkey);
item.Attributes.Add("disabled","disabled");
lb1.Items.Add(item);
One solution, as suggested by amccormack:
private void hostsListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if(lstHosts.SelectedItem == null)
{
if(e.RemovedItems.Count > 0)
{
lstHosts.SelectedItem = e.RemovedItems[0];
}

Clear ListBox Selection when empty area is clicked

At least one item always remain selected in ListBox. I want that when user clicks empty area of ListBox, selection should be cleared. How to do this?
I am trying to replicate Opera Notes as a part of my application. First i was using a binded DataGridView now i am using a binded ListBox on left pane
Handle the ListBox.MouseDown event.
Call ListBox.IndexFromPoint, passing the Location property from the MouseDown event's MouseEventArgs parameter.
This should return the index of the item that was clicked, or ListBox.NoMatches if the click was on an empty area.
If the return value is ListBox.NoMatches, set the ListBox.SelectedIndex property to -1 to clear the selection.
Mr. Avalanchis has answered the question already. I am just adding the code necessary to follow the steps what he has suggested. Hope the explicit code will help.
private void listBox1_MouseDown(object sender, MouseEventArgs e)
{
Point pt = new Point(e.X, e.Y);
int index = listBox1.IndexFromPoint(pt);
if (index <= -1)
{
listBox1.SelectedItems.Clear();
}
}

Categories

Resources