Lets Have a fun with Technology.

BTemplates.com

Follow by Email

Sunday, November 18, 2012

How to set tooltip on Combobox Items in Windows Application.


How to set tooltip on Combobox Items in Windows Application.

Hello,
Below code block is set tool tip on combobox items. First you need to drop Tooltip UI Control on windows Form. Default name of tooltip control is toolTip1. Now we need to create different events to show tooltip on Combobox each items. Add Below code in form load events.


 
private void Form1_Load(object sender, EventArgs e)
        {
            cmbProperty.DrawMode = DrawMode.OwnerDrawFixed;
            cmbProperty.DrawItem += new DrawItemEventHandler(cmbProperty_DrawItem);
            cmbProperty.DropDownClosed += new EventHandler(cmbProperty_DropDownClosed);
            cmbProperty.Leave += new EventHandler(cmbProperty_Leave);
             

        }


In Above cmbProperty is Combobox name. Now add below event handler in form1.cs
 

void cmbProperty_Leave(object sender, EventArgs e)
        {
            toolTip1.Hide(cmbProperty);
        }

        void cmbProperty_DropDownClosed(object sender, EventArgs e)
        {
            toolTip1.Hide(cmbProperty);
        }

        void cmbProperty_DrawItem(object sender, DrawItemEventArgs e)
        {
            if (e.Index < 0) { return; } 
            string text = cmbProperty.GetItemText(cmbProperty.Items[e.Index]);
            e.DrawBackground();
            using (SolidBrush br = new SolidBrush(e.ForeColor))
            { e.Graphics.DrawString(text, e.Font, br, e.Bounds); }
            if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
            { toolTip1.Show(text, cmbProperty, e.Bounds.Right, e.Bounds.Bottom); }
            e.DrawFocusRectangle();
        }



Now run the application and Enjoy!!! Let me know if you are facing any problem.

3 comments: