如何为ListViewItem设置工具提示

36

我正在使用一个带有几个固定宽度列的 ListView

行中的文本可能太长而无法适应该列,因此我想使用户在悬停在 ListViewItem 上时显示一个带有更多文本的工具提示。

我尝试设置 ListViewItemToolTipText 属性:

ListViewItem iListView = new ListViewItem("add");

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

很遗憾,它似乎不起作用。我如何使ListViewItems显示工具提示?

2个回答

61

将 ListView 的 ShowItemToolTips 属性设置为 true。


1
当尝试显示非常长的字符串时,工具提示似乎也有字符限制。有几种解决方案可以规避ListView中显示文本的259列限制(例如https://dev59.com/KFXTa4cB1Zd3GeqP6O6Y),或者您可以让用户复制粘贴所选行。 - valsidalv
1
这仅适用于文本不适合列的项目子项。但是,当您在“ListViewItem”上设置“ToolTipText”属性时,工具提示将始终显示,但这仅适用于第一个子项。 - gangus

7
使用 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:这是我添加的链接中的一部分;只是我应该添加更多行。感谢您指出。 - KMån

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接