Click here to Skip to main content
15,917,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Good morning,

Please exmine the following code,

C#
void combobox1_DrawItem(object sender, DrawItemEventArgs e)
{
    toolTip1.AutoPopDelay = 5000; toolTip1.InitialDelay = 500; 
    toolTip1.ReshowDelay = 100; toolTip1.AutomaticDelay = 500;

    string text = this.combobox1.GetItemText(combobox1.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, combobox1, e.Bounds.Right, e.Bounds.Bottom); 
    }
    else 
    { 
        toolTip1.Hide(P1); 
    }
    e.DrawFocusRectangle();
}



Sir,

Here in combobox1 I have around 200 items, almost each item has long string length and it does not fit in the combo width. Hence I am trying to use tool tip to display the whole content of the mouse hovered item. I am able to do this. But when I move away the mouse from the combboox item or select the item or I switch to next TABPAGE the last tooltip is not disapearing. I donot understand what mistake I am doing while doing so.

Any one can giude and help me please.
Thanks in advance
regards

Gentlemen,

Any help or guide line by any one, I stuck on my project its urgent please... waiting for your kind response. One gentleman proposed some code but it won't work on comboxbox, I have put coments what happens their.

Thanks in advance
Posted
Updated 10-Feb-11 8:41am
v4

Something like this:

C#
ComboBox cb = new ComboBox();
parent.Controls.Add(cb);
ToolTip tooltip = new ToolTip();
tooltip.SetToolTip(cb, "tooltip set for combo box");


This is how ToolTip normally used, it has nothing to do with rendering of graphics (as you're trying to do).

This is just basic usage. For more advanced sample, see my new Answer.

Good luck,
—SA
 
Share this answer
 
v3
Comments
zaki_8279025 10-Feb-11 2:54am    
Sir,

Thanks for your extended help. I tried to implement the code you proposed, it works but does not serve the purpose I am trying. This give the tooltip only after i click the item in the combolist. But I need the tooltip to display the content while I dropdown the combobox list and hover over the item while doing so the tooltip should display where the mouse current position is and once the mouse is taken away the tooltip should get wiped or disapear. So my code is able to display the tootip while hovering over the items but once I move mouse from the combobox item the tooltip remains their and won't disapear, even thou I click to the other TAB page (as I have three TABPAGes) THE TOOLTIP remains at its position. Any help will be highly appericiated.

Thanks and best regards
Sergey Alexandrovich Kryukov 10-Feb-11 13:23pm    
I understand. That was the quick fix. There are many delicate moments about this component. Why not digging into the help on this class just a bit deeper? You know better how you want to have it. There is yet another possibility: to create a little pop-up window of your own. Seriously, not too hard.
--SA
Sergey Alexandrovich Kryukov 10-Feb-11 14:20pm    
I think I found the solution, basically, works as you expected.
Some fine event and text detail are on you.
See my new Answer.
--SA
Thank you for clarification of your requirements.
There are many ComboBox events you may want to use, important one is the one used to hide the ToolTip. This works for me and will give you and idea what else to display in the ToolTip, and what event:

C#
ComboBox cb = new ComboBox();
cb.Items.Add("first");
cb.Items.Add("second");

this.Controls.Add(cb);

System.Windows.Forms.ToolTip tooltip = new ToolTip();
tooltip.ShowAlways = true; //weird, isn't it?

cb.MouseLeave += (sender, eventArgs) => {
    tooltip.Hide(cb);
}; //cb.MouseLeave

cb.MouseMove += (sender, eventArgs) => {
    if (cb.SelectedItem == null)
        tooltip.SetToolTip(cb, string.Format("Mouse: {0}, {1}", eventArgs.X, eventArgs.Y));
    else
        tooltip.SetToolTip(
            cb,
            string.Format(
                "Mouse: {0}, {1}; selected: {2}",
                eventArgs.X, eventArgs.Y, cb.SelectedItem));
}; //cb.MouseMove

cb.SelectedValueChanged += (sender, eventArgs) => {
    if (cb.SelectedItem != null)
        tooltip.SetToolTip(cb, string.Format("Selected: {0}", cb.SelectedItem));
}; //cb.SelectedValueChanged

cb.SelectionChangeCommitted += (sender, eventArgs) => {
    if (cb.SelectedItem != null)
        tooltip.SetToolTip(cb, string.Format("Selection committed: {0}", cb.SelectedItem));
}; //cb.SelectionChangeCommitted


Pay attention: tooltip.ShowAlways (!).

Note for those using C# version prior to v.3, before lambda syntax was introduced: use different syntax for anonymous delegate:

C#
cb.MouseMove += delegate(object sender, MouseEventArgs eventArgs) {
    //...
} //cb.MouseMove 


Have fun!

—SA
 
Share this answer
 
v4
Comments
Espen Harlinn 10-Feb-11 16:45pm    
Good effort, a 5
Sergey Alexandrovich Kryukov 10-Feb-11 16:56pm    
Thank you, Espen.
This question is not trivial (maybe, ToolTip design is oriented to bases usage, not enough flexibility), I had to experiment a bit to make it working. Hope it helps OP to finalize it.
--SA
zaki_8279025 12-Feb-11 7:27am    
Excellent, thankyou very much, I tried these all but as you at one time in relation. I hve never reached what you have lighten to me, excellent piece of code.
May GOD bless you.

best regards

Zaki
Sergey Alexandrovich Kryukov 12-Feb-11 14:41pm    
You're welcome.
Thank you for accepting my answer.
Good luck and call again,
--SA
Looks to me like you might have the show/hide reversed. Try this:


C#
if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)    
{
    toolTip1.Hide(P1);
}
else
{
    toolTip1.Show(text, combobox1, e.Bounds.Right, e.Bounds.Bottom);
}
 
Share this answer
 
Change the value of the AutoPopDelay to something smaller (say 2000) and try.
 
Share this answer
 
Comments
zaki_8279025 10-Feb-11 2:55am    
Thanks for your respose but still it won't work.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900