How to set tooltip for a ListviewItem
I am using a ListView
with a few fixed-size columns.
The text in the rows may be too large to fit in the column, so I would like to make it so that when the user hovers over the ListViewItem
, it shows a tooltip with more text.
I tried setting the ToolTipText
property of the ListViewItem
:
ListViewItem iListView = new ListViewItem("add");
iListView.ToolTipText = "Add Expanded";
myListView.开发者_JS百科Items.Add(iListView);
Unfortunately, it didn't seem to work. How can I get ListViewItems
to show ToolTips?
Set the ListView's ShowItemToolTips
property to true.
Use ListViewItem.ToolTipText Property
// 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);
}
精彩评论