【问题标题】:How to set tooltip for a ListviewItem如何为 ListviewItem 设置工具提示
【发布时间】:2011-02-13 10:15:56
【问题描述】:

我正在使用带有几个固定大小列的ListView

行中的文本可能太大而无法放入列中,所以我想这样当用户将鼠标悬停在ListViewItem 上时,它会显示一个带有更多文本的工具提示。

我尝试设置ListViewItemToolTipText 属性:

ListViewItem iListView = new ListViewItem("add");

iListView.ToolTipText = "Add Expanded";
myListView.Items.Add(iListView);

不幸的是,它似乎不起作用。如何让ListViewItems 显示工具提示?

【问题讨论】:

    标签: c# winforms listview


    【解决方案1】:

    使用ListViewItem.ToolTipText属性

    // Declare the ListView.
    private ListView ListViewWithToolTips;
    private void InitializeItemsWithToolTips()
    {
    
        // Construct and set the View property of the ListView.
        ListViewWithToolTips = new ListView();
        ListViewWithToolTips.Width = 200;
        ListViewWithToolTips.View = View.List;
    
        // Show item tooltips.
        ListViewWithToolTips.ShowItemToolTips = true;
    
        // Create items with a tooltip.
        ListViewItem item1WithToolTip = new ListViewItem("Item with a tooltip");
        item1WithToolTip.ToolTipText = "This is the item tooltip.";
        ListViewItem item2WithToolTip = new ListViewItem("Second item with a tooltip");
        item2WithToolTip.ToolTipText = "A different tooltip for this item.";
    
        // Create an item without a tooltip.
        ListViewItem itemWithoutToolTip = new ListViewItem("Item without tooltip.");
    
        // Add the items to the ListView.
        ListViewWithToolTips.Items.AddRange(new ListViewItem[]{item1WithToolTip, 
            item2WithToolTip, itemWithoutToolTip} );
    
        // Add the ListView to the form.
        this.Controls.Add(ListViewWithToolTips);
        this.Controls.Add(button1);
    }
    

    【讨论】:

    • @SLaks:这是我添加的链接的片段;只是我应该添加更多行。感谢您指出。
    【解决方案2】:

    将 ListView 的 ShowItemToolTips 属性设置为 true。

    【讨论】:

    • 在尝试显示非常长的字符串时,工具提示似乎也有字符限制。有几种解决方案可以绕过在 ListView 中显示文本的 259 列限制(例如 stackoverflow.com/questions/5559704/…),或者您可以让用户复制粘贴选定的行。
    • 这仅适用于文本不适合列的项目的子项。但是当您在ListViewItem 上设置ToolTipText 属性时,工具提示将始终显示,但这仅指第一个子项。
    猜你喜欢
    • 2011-09-03
    • 2015-06-21
    • 1970-01-01
    • 1970-01-01
    • 2019-09-14
    • 1970-01-01
    • 1970-01-01
    • 2013-06-17
    • 1970-01-01
    相关资源
    最近更新 更多