none
Is it possible to make a subitem on a listview clickable? RRS feed

  • Question


  • Hello,

    I am wondering if it's it possible to make a subitem on a listview clickable? (on detail view).

    Many thanks.

    Wednesday, March 22, 2006 1:41 PM

Answers

All replies

  • No, the default managed ListView control class doesn't support that, but there are a lot of extends of the ListView control the simplest and best i know is: In-place editing of ListView subitems.
    Wednesday, March 22, 2006 1:50 PM
    Moderator
  • Dear Angry Coder:

    The answer was no at least up to Framework 1.1.  However, a close solution now exists in Framework 2.0 below.  I actually have used something similar to the solution below with tooltips to publish a different tooltip for each item or subitem, whichever the case may be, rather than use only one tooltip for the first (item) column, Microsoft's by-design built-in property for a list view control in C#.

    My problem issue yet unresolved is that the item-mouse-hover message is not activated if you change cells on the same item-row.  But changing cells or columns from one item (or subitem) to another works great.  The final problem then becomes how can we force or trigger an item-mouse-hover callback event.  We are talking a lower level of coding here, but I would suggest that some code could be extended to the list view control that can trigger item-mouse-hover messages not only for changes in item-rows in the list view but also changes for changes in columns while on the same item-row.

    You could likely write the code similar to what Microsoft did to produce the item-mouse-hover event.  Use the mouse move event which captures the mouse point, determines the item and the corresponding bounding rectangle for the item-row, and then locate the particular column-item or column subitem based on the column's compile time defined width or width at runtime if not locked.  This information should then be stored in a private class data attribute for your list view class and then recaptured with any click or double-click mouse message.

    Below, is a repro sample used to capture the item information from an item-mouse-hover event.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Data;
    using System.Text;
    using System.Windows.Forms;
    using System.Diagnostics;

    namespace MyNamespace.Windows.UserControls.Examples
    {
        public partial class MyControl: UserControl
        {
            private ListViewHitTestInfo listViewInfo;
            private Point listViewItemPoint;
           
            public MyControl()
            {
               
                InitializeComponent();

                if (this.listViewMyControlGroup.Items.Count > 0)
                {
                    foreach (ListViewItem item in this.listViewSpeedDialGroup.Items)
                    {
                        item.SubItems[1].Name = "KeyName1";
                        item.SubItems[2].Name = "KeyName2";
                    }
                }
            }


            private void MyControl_Load(object sender, EventArgs e)
            {

            }

            private void listViewMyControl_ItemMouseHover(object sender, ListViewItemMouseHoverEventArgs e)
            {
                System.Windows.Forms.ListView speedDialList = (System.Windows.Forms.ListView)sender;
                ListView.SelectedListViewItemCollection items = myControl.SelectedItems;
                ListViewItem lvi = speedDialList.FocusedItem;

                if (listViewInfo.SubItem != null)
                {
                    string Name = listViewInfo.SubItem.Name;
                    if (e.Item.SubItems.ContainsKey(Name))
                    {
                        int index = e.Item.SubItems.IndexOfKey(Name);
                        if (Name.Equals("KeyName1"))
                        {
       MessageBox.Show("TODO: Handle match on KeyName1 here...", "Information",
                      MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                        else
                        if (Name.Equals("KeyName2"))
                        {
       MessageBox.Show("TODO: Handle match on KeyName2 here...", "Information",
                      MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                    }
                }
            }


            private void listViewMyControl_MouseMove(object sender, MouseEventArgs e)
            {
                System.Windows.Forms.ListView myControl = (System.Windows.Forms.ListView)sender;

                // Get the item at the mouse pointer.
                listViewItemPoint.X = e.X;
                listViewItemPoint.Y = e.Y;
                listViewInfo = myControl.HitTest(listViewItemPoint);
            }
        }
    }

    You should be able to capture the item information from the item mouse hover over message.  This event is new to Framework version 2.0.  I do not know how much time it takes to trigger this event but it may solve your issue.  Microsoft could add item to the mouse and double-click event, but maybe Microsoft's idea was to keep them generic and handle your case in the manner I just described.

    I hope this helps.

    James Sigler

    Dallas, TX

    Consultant, Senior Windows .NET Developer

    jmsigler2@votx.com

    Friday, April 14, 2006 12:07 AM
  • foreach (ListViewItem item in this.listViewSpeedDialGroup.Items)

    Dear Angry Coder:

    I made a typo on the above line.

    It should read "this.listViewMyControlGroup.Items".

    James

     

    Friday, April 14, 2006 12:16 AM
  • I spotted two others...

    System.Windows.Forms.ListView speedDialList = (System.Windows.Forms.ListView)sender;
    ListView.SelectedListViewItemCollection items = myControl.SelectedItems;
    ListViewItem lvi = speedDialList.FocusedItem;

    should read

    System.Windows.Forms.ListView myControlList = (System.Windows.Forms.ListView)sender;
    ListView.SelectedListViewItemCollection items = myControl.SelectedItems;
    ListViewItem lvi = myControlList.FocusedItem;

    I apologize as it is near the end of the day.  I am headed for a Starbucks coffer or a cold beer.  I have not decided yet. :)

    James

     

     

    Friday, April 14, 2006 12:23 AM
  •  
    can anyone help me, I need to develop a app where there is a tree control and a list view control. In list view there are two columns filled and the third one is empty column. what i need to do is to drag the item from tree control and put it in the third blank column of the listView....using C#. pls help me.
    pls provide the C# code if possible...

    thanks in advance !


    Monday, June 30, 2008 1:54 PM