Search code examples
c#.netwinformslistviewtooltip

Disable WinForms ListViewItem Tooltip for long texts


I have created ListView and added two item with long text. when I select first item second item`s text is clipped, for example "MyIte....".

So when move mouse pointer under this item, I see toolTip with all text.

How to disable this tooltip?

Set property ListView.ShowItemToolTips = false doesn't help.


Solution

  • ListView shows item tooltips when receives a WM_Notify message with TTN_NEEDTEXT lparam. So to disable tooltips, you can process ListView messages and if the control received that message, neglect it.

    You can inherit your ListView and override WndProc, but as another option, you can register a NativeWindow to receive your ListView messages and this way you can filter messages.

    Implementation

    public class ListViewToolTipHelper : NativeWindow
    {
        private ListView parentListView;
        private const int WM_NOTIFY = 78;
        private const int TTN_FIRST = -520;
        private const int TTN_NEEDTEXT = (TTN_FIRST - 10);
        public struct NMHDR
        {
            public IntPtr hwndFrom;
            public IntPtr idFrom;
            public Int32 code;
        }
        public bool TooltipEnabled { get; set; }
        protected override void WndProc(ref Message m)
        {
            if (m.Msg == WM_NOTIFY && !TooltipEnabled)
            {
                var nmHdr = (NMHDR) m.GetLParam(typeof(NMHDR));
                if (nmHdr.code == TTN_NEEDTEXT)
                    return;
            }
    
            base.WndProc(ref m);
        }
        public ListViewToolTipHelper(ListView listView)
        {
            this.parentListView = listView;
            this.AssignHandle(listView.Handle);
        }
    }
    

    Usage

    To disable tooltips for a ListView you can simply create an instance of above class:

    ListViewToolTipHelper helper;
    helper = new ListViewToolTipHelper(this.listView1);
    

    To enable tooltips again:

    helper.TooltipEnabled = true;
    

    Another workaround

    You can disable the tooltip for ListView using this workaround, but the side effect is all other tooltips in form also will be disabled this way.

    ToolTip toolTip = new ToolTip();
    toolTip.SetToolTip(this.listView1, "dummy text");
    toolTip.Active = false;