URL 的 ListView 鼠标点击事件 [英] ListView MouseClick Event for URL

查看:22
本文介绍了URL 的 ListView 鼠标点击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在第 3 列中,我有链接,我想从浏览器中的链接创建 ListView MouseClick 事件以显示网站.我是这样写的:

In column 3 I have link and I want to create ListView MouseClick Event for show web-site from link in browser. I wrote this:

private void listView1_MouseClick(object sender, MouseEventArgs e)
{
     try
     {
         string linkOn = "linkto:" +                                       
         listView1.SelectedItems[id.Text].SubItems[linkOnTheme.Index].Text;
         System.Diagnostics.Process.Start(linkOn);
     }    
     catch (Win32Exception ex)
     {
         MessageBox.Show("An error has occured: " + ex.Message);
     }
}

但它仅适用于第一列.我哪里出错了?

but it work only for 1st column. Where I take a mistake?

推荐答案

ListViewMouseClickClick 事件仅在(主要)项目,而不是任何子项目.

The MouseClick and Click event of the ListView fire only on the (main) Item, not on any subitems.

所以你必须使用其他事件并在那里做一个HitTest:

So you must use other events and do a HitTest there:

以下代码可以进入 MouseDownMouseUp 事件:

The following code can either go into the MouseDown or in the MouseUp event:

    Point mousePos = listView1.PointToClient( Control.MousePosition );
    ListViewHitTestInfo htInfo = listView1.HitTest(mousePos);

    if (htInfo.Item == null) return;

    int itemIndex = htInfo.Item.Index;
    int subItemIndex = htInfo.Item.SubItems.IndexOf(htInfo.SubItem);

    if (subItemIndex == yourLinkColumnIndex)
    {
        try
        {
            string linkOn = "linkto:" +  htInfo.Item.SubItems[subItemIndex].Text;
            System.Diagnostics.Process.Start(linkOn);
        } catch (Win32Exception ex)
        {
            MessageBox.Show("An error has occured: " + ex.Message);
        }

    }

这篇关于URL 的 ListView 鼠标点击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆