Link to home
Start Free TrialLog in
Avatar of chewlf
chewlf

asked on

Dropdown tooltip for C#.NET

Hi All,

I need some helps. How to write in C#.NET on dropdownlist web control to display tooltip? I need this because some of the values inside the dropdownlist are too long, so I am thinking to display them into tooltip. Please help to provide solution code.

Thanks in advance.
Chew
Avatar of VICKRAM
VICKRAM

In th PreRender event of the dropdownlist you can do this

protected void ddlDropDown_PreRender(object sender, EventArgs e)
{
       ddlDropDown.Attributes.Add("ToolTip", "Please select an item from the list");
}
   
Avatar of chewlf

ASKER

Hi Vickram,

Thank you for your response. Would you please let me know how to write the code if I would like to display the tooltip using variable and the values might be different. From your example above, the tooltip is hardcoded. Please advice.

Thanks,
Chew
Avatar of chewlf

ASKER

I found this code somewhere. Anyone know how to convert it to web control?

//Code Start
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;

namespace ListBoxToolTips
{
      /// <summary>
      ///
      /// </summary>
      public class ToolTipListBox : System.Windows.Forms.ListBox
      {
            [StructLayout(LayoutKind.Sequential)]
            public struct SIZE
            {
                  public int cx;
                  public int cy;
            }
            [DllImport("gdi32.dll")]
            public static extern int GetTextExtentPoint32(IntPtr hdc,
                  String str, int len, ref SIZE size);

            [DllImport("user32.dll")]
            public static extern IntPtr GetDC(IntPtr hWnd);

            [DllImport("user32.dll")]
            public static extern int ReleaseDC(IntPtr hWnd,IntPtr hdc);


            public ToolTipListBox()
            {                  
                  tp.InitialDelay = 500;
                  tp.ReshowDelay = 500;
                  tp.AutoPopDelay = 3000;                  
                  tp.Active = true;                  
            }      

            
            protected override void OnMouseMove(System.Windows.Forms.MouseEventArgs e)
            {                        
                  int index = IndexFromPoint(e.X,e.Y);
                  if(index != ListBox.NoMatches )
                  {                        
                        if( LastIndex != index )
                        {
                              string s = Items[index].ToString();      

                              IntPtr hdc = GetDC(this.Handle);
                              SIZE size;
                              size.cx = 0;
                              size.cy = 0;
                              GetTextExtentPoint32(hdc,s,s.Length,ref size);
                              ReleaseDC(this.Handle,hdc);

                              if(this.Width < size.cx)                              
                                    tp.SetToolTip(this,s);

                              LastIndex = index;                        
                        }                                          
                  }
            }
            
            private ToolTip tp = new ToolTip();
            private int LastIndex = -1;
            
      }
}
//Code End

Please help.
Thanks,
Chew
ASKER CERTIFIED SOLUTION
Avatar of VICKRAM
VICKRAM

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial