 |
|
 |
This article saved me a bunch of time! Thanks!
|
|
|
|
 |
|
 |
I change the code to set location of tooltip exactly on Listbox's Item.
But there is a problem, when mouse clicked on tooltip, i want to mouse click event of listbox fired.
|
|
|
|
 |
|
 |
Hello Nishanth
It's indeed a very educative post for newbies like me .. Thanks
But since yest after readin your post about Listbox tooltip sample... I tried working out the same thing for a combobox in vain . i.e Displaying tooltips for the combobox items...
Can u shed some light doing same thing for Combobox items.. Is there any equivalent for IndexFromPoint in ComboBox...???
Awaiting for a response
Srivatsa
|
|
|
|
 |
|
 |
Hey,
I've chaged your code a little bit so that the ToolTip is displayed over the items in the ListBox.
There is no PInvoke here.
Here we go:
protected override void OnMouseMove(System.Windows.Forms.MouseEventArgs e)
{
// Get the index of the mouse-hovered item
int index = base.IndexFromPoint(e.X, e.Y);
// Ensure that there is an item
if (index != ListBox.NoMatches)
{
// Check if the mouse has moved enough distance for a new index
if (LastIndex != index)
{
string s = Items[index].ToString();
Size textSize = TextRenderer.MeasureText(s, base.Font);
Rectangle r = base.GetItemRectangle(index);
if (r.Width < textSize.Width)
// Display the TT over the LB item
tp.Show(s, this, r.X+1, r.Y+2);
else
tp.Hide(this);
LastIndex = index;
}
}
else
{
tp.Hide(this);
}
}
// I use this event handler because I use a different
// font in the LB, so a matching TT font is needed
void tp_Draw(object sender, DrawToolTipEventArgs e)
{
//Draw the standard background
e.DrawBackground();
// Draw the standard border
e.DrawBorder();
// Draw the custom text
TextRenderer.DrawText(e.Graphics, e.ToolTipText, base.Font, e.Bounds, Color.Black, TextFormatFlags.HorizontalCenter);
}
// Changes the TT's size
void tp_Popup(object sender, PopupEventArgs e)
{
Size s = TextRenderer.MeasureText(tp.GetToolTip(this), base.Font);
s.Height += 3;
e.ToolTipSize = s;
}
Thank you Nishant for posting this article.
-- modified at 11:32 Wednesday 2nd May, 2007
|
|
|
|
 |
|
 |
Do not work for .Net 1.1 since TextRenderer is a .Net 2.0 feature only
|
|
|
|
 |
|
 |
I had been trying to figure out for a day or two how to add tooltips to each item in a combobox and luckily I came across your article. After a few modifications to use a combo box instead of a listbox, it works beautifully ... Thank you.
|
|
|
|
 |
|
 |
I am using ASP.NET 2.0 and I want to show tool tips for drop down list control on aspx page. But I cannot make this code work for me. Could you please let me know your solution?
Thanks.
wlin
|
|
|
|
 |
|
 |
I include the ToolTipListBox.cs in my project, but I don't know how to put a ListBox of ToolTipListBox class on my page instead of a ListBox of standard ListBox class.
Thanks.
wlin
|
|
|
|
 |
|
 |
Could please elaborate on how have you handled it for ComboBox ?
|
|
|
|
 |
|
 |
I am having problem of sentence cannot fully shown in listbox in ASP. It will be great if this can be used in ASP as well.
|
|
|
|
 |
|
 |
I justed grab that piece of code and it worked perfectly. Tooltips are far way better than horizontal scrollbars. Thanks a lot !
|
|
|
|
 |
|
 |
The example here would run only on Framework 1.1 and above. .NET Framework 1.0 does not have method IndexFromPoint and there is probably no way to get the index from the coordinates of the mouse pointer in 1.0.
Any ideas?
Tanmay
|
|
|
|
 |
|
 |
Nishant is it possible to do the same in ASP.net
|
|
|
|
 |
|
 |
I would like to be able to do that too. I need a combobox that will display a tooltip for each item in the list. But i think implementing it in ListBox on asp.net will not be as easy because there is no OnMouseMove event to hook to there. Anyway, this article is one of the nearest solution to my problem.
lead, follow, or get out of the way!
|
|
|
|
 |
|
 |
Dear Sir,
I used your code in a program of mine and does not works. Then I tried your own test project and I easily found how to make it fail: just change the Control Font (i.e. place 20point): the line
GetTextExtentPoint32(hdc, str, str.Length, ref size);
errorneously report the heigth 16 whatever le font size !!!
Could you please help me?
|
|
|
|
 |
|
 |
I think to have found the problem.
I send you a utility class that can fit your needs:
/**
* \brief Classe di calcolo della dimensione del testo
* \date 16/02/05
*
* Questa classe contiene codice nativo
* La classe consente di determinare il rettangolo in cui e'contenuto il testo
* usando un device context del dispositivo stesso.
*/
public class textextent
{
private System.Windows.Forms.Form m_form;
private System.Windows.Forms.Control m_control;
private IntPtr m_wnd;
[StructLayout(LayoutKind.Sequential)]
public struct SIZE
{
public int cx;
public int cy;
}
[DllImport("gdi32.dll")]
private static extern int GetTextExtentPoint32(IntPtr hdc,
String str, int len, ref SIZE size);
[DllImport("gdi32.dll")]
private static extern System.IntPtr SelectObject (IntPtr hdc, IntPtr newobj);
[DllImport("user32.dll")]
private static extern IntPtr GetDC(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern int ReleaseDC(IntPtr hWnd,IntPtr hdc);
/**
* costruttore utile per le finetre
* \param graphicObj oggetto grafico di tipo finestra
*/
public textextent(System.Windows.Forms.Form graphicObj)
{
m_wnd = graphicObj.Handle;
m_form = graphicObj;
if (m_form == null)
throw new ArgumentNullException ("graphicObj");
}
/**
* costruttore utile per i controlli
* \param graphicObj oggetto grafico di tipo controllo
*/
public textextent(System.Windows.Forms.Control graphicObj)
{
m_wnd = graphicObj.Handle;
m_control = graphicObj;
if (m_control == null)
throw new ArgumentNullException ("graphicObj");
}
/**
* \brief Calcolo della dimensione del testo
* \param str testo da controllare
* \return struttura con la dimensione del testo
*/
public System.Drawing.Size getExtent (string str)
{
System.IntPtr fntpt;
if (m_control != null)
{
fntpt = m_control.Font.ToHfont ();
}
else
{
fntpt = m_form.Font.ToHfont ();
}
System.Diagnostics.Debug.Assert (0 != (long) fntpt);
if (0 == (long) fntpt)
{
throw new EntryPointNotFoundException ();
}
IntPtr hdc = GetDC(m_wnd);
System.IntPtr oldobj = SelectObject (hdc, fntpt);
SIZE size;
size.cx = 0;
size.cy = 0;
GetTextExtentPoint32(hdc, str, str.Length, ref size);
SelectObject (hdc, oldobj);
ReleaseDC(m_wnd,hdc);
System.Drawing.Size ret = new System.Drawing.Size (size.cx, size.cy);
return ret;
}
}
Sorry,
comments are in Italian...
Please tell me if this is correct.
Regards,
Andrea
|
|
|
|
 |
|
 |
Hi, I need to know if is possible to show tooltip on each node in the treeview
Thanks
Geovanny
h
|
|
|
|
 |
|
 |
Is that possible to show tooltip on each item in the combobox?
|
|
|
|
 |
|
 |
i want combobox tooltip too... Maybe you have it ?
|
|
|
|
 |
|
 |
Hi, thanx for the code I used it in a ListView , the .Net way but the code is mostly yours. userList is a ListView inside my control representing users with Images. Works in both list and details view. tp is setup just like yours.
private void userList_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
ListViewItem index = userList.GetItemAt(e.X,e.Y);
if (index != null)
{
string nick = index.Text;
Graphics g = userList.CreateGraphics();
SizeF size = g.MeasureString(nick, userList.Font);
if(userList.Columns[0].Width - index.ImageList.ImageSize.Width < size.Width)
tp.SetToolTip(userList,nick);
else tp.RemoveAll();
}else tp.RemoveAll(); }
MYrc : A .NET IRC client with C# Plugin Capabilities. See
http://sourceforge.net/projects/myrc for more info.
|
|
|
|
 |
|
 |
...would be if the tooltip overlayed the item it represented, like Microsoft's "Label Tips" do for tree and list controls.
---
Shog9
If I could sleep forever, I could forget about everything...
|
|
|
|
 |
|
 |
Good idea Shog
Basically this wasn't a client requirement. And I could have easily convinced that they should not enter names too large for the width of the list box. But as an afterthough I decided to add tooltips and I assumed initially that there'd be a class. There wasn't one anyway. But then it turned out to be a rather simple task to add tooltips cause of the ToolTip class.
Thanks anyway for the tip, though I'd prolly have to do some R&D to figure out how to do that.
Nish
p.s. I swapped my 15" for a new 17"
Author of the romantic comedy
Summer Love and Some more Cricket [New Win]
Review by Shog9
Click here for review[NW]
|
|
|
|
 |
|
 |
Nishant S wrote:
p.s. I swapped my 15" for a new 17"
Congrats
Now you can design space-wasting UIs with the rest of us...
---
Shog9
If I could sleep forever, I could forget about everything...
|
|
|
|
 |