listView subitem click event.

A

andrewcw

The clcik event on the ListView control seems to trigger
off the item level ( the first column ). I would like to
be able to trap the click event on a particular subitem (
a specific column's row ). If possible, what do I do to
activate that 'cell' - thanks.

I thought that listView.LabelEdit = true; would enable me
to navigate into the cells or something like listView.items
[index].subitems[index].LabelEdit - but I did not see such
a property. Thanks for any hints and a good discussion on
this controls features
 
J

Jeffrey Tan[MSFT]

Hi Andrew,

It seems that there are no event for you to handle the subitem click.
As a workaround, you can do the hit-test yourself.(In the ListView's
MouseDown event)

Sample code like this:
//first add org_i and org_j to the private member of the Form class with
initial value -1.
private int org_i=-1;
private int org_j=-1;

//the code below is in the ListView_MouseDown event.
//col_height is the height of the listview column header
int col_height=listView1.Items[0].Bounds.Height;

//org_i and org_j are the original clicked subitem.
if(org_i!=-1||org_j!=-1)
{
listView1.Items[org_j].UseItemStyleForSubItems =false;
listView1.Items[org_j].SubItems[org_i].BackColor=Color.White ;
}

//items_height and items_width are use for checking if the mouse click out
of all the items.
int items_height=0;
for(int item_count=0;item_count<listView1.Items.Count;item_count++)
{
items_height+=listView1.Items[item_count].Bounds.Height;
}
items_height+=col_height;

int items_width=0;
for(int col_count=0;col_count<listView1.Columns .Count ;col_count++)
{
items_width+=listView1.Columns [col_count].Width ;
}

//if the mouse clicks out of all the items or columns, just return.
if(e.X>=items_width||e.Y>=items_height)
{
return;
}

//i and j are the index of the subitem.
int i=0,j=0;
int width_len=listView1.Columns[0].Width;
int height_len=col_height+listView1.Items[0].Bounds.Height;
while(e.X >width_len)
{
i++;
width_len+=listView1.Columns.Width;
}

while(e.Y>height_len)
{
j++;
height_len+=listView1.Items[j].Bounds.Height ;
}

//change the clicked subitem's backcolor
listView1.Items[j].UseItemStyleForSubItems =false;
listView1.Items[j].SubItems.BackColor=Color.Blue ;

org_i=i;
org_j=j;

<Code End>

In the code, I just use the item[0]'s height as the column header's height
(int col_height=listView1.Items[0].Bounds.Height; ).
This is because I did not find a good way of retrieving the height of the
column header, I think this substitute way does make much difference.
But I think you can use LVM_GETCOLUMN to get the handle of the column, then
you can use Win32 function GetClientRect() to get its height.

It works well on my machine, if you have any questions please feel free to
let me know.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| Content-Class: urn:content-classes:message
| From: "andrewcw" <[email protected]>
| Sender: "andrewcw" <[email protected]>
| Subject: listView subitem click event.
| Date: Tue, 7 Oct 2003 15:26:12 -0700
| Lines: 11
| Message-ID: <[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="iso-8859-1"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| Thread-Index: AcONIgNTlhz7GS68T32Y0fqpTvFtSA==
| X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
| Newsgroups: microsoft.public.dotnet.languages.csharp
| Path: cpmsftngxa06.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:189676
| NNTP-Posting-Host: TK2MSFTNGXA08 10.40.1.160
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| The clcik event on the ListView control seems to trigger
| off the item level ( the first column ). I would like to
| be able to trap the click event on a particular subitem (
| a specific column's row ). If possible, what do I do to
| activate that 'cell' - thanks.
|
| I thought that listView.LabelEdit = true; would enable me
| to navigate into the cells or something like listView.items
| [index].subitems[index].LabelEdit - but I did not see such
| a property. Thanks for any hints and a good discussion on
| this controls features
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top