Multiple Underlined Tooltips on one control - WinForms
I would like a control that shows.
Tooltip1 notooltip Toolti开发者_StackOverflow中文版p2
^ ^
TOOLTIPA TOOLTIPB
To have one tooltip display when hovering over tooltip1 and a different one display when over tooltip2. In reality these would both be underlined and blue.
I can accomplish this with a richtextbox and tracking the locations of tooltips by GetPositionFromCharIndex and mouse events. However, with the richtextbox I can't disable text selection / position caret without disabling the control, which in turn disables all of my events.
I don't want to use multiple labels because it requires manually spacing the labels because of handling character spacing.
Is there a third party control like Dev Express that might handle this? Any alternatives?
Thanks
I'm not 100% sure what you're asking for from your description, but it seems that you want one control that has a ToolTip who's caption depends on what part of the control the mouse is hovering over?
I can't think of a "good" way to do this. The code below will work for a Label, but it's ugly. I'd personally rather create a UserControl with multiple Labels and worry about manually spacing them. But here's what I got:
Label lbl = new Label() { Left = 6, Top = 6, AutoSize = true };
this.Controls.Add(lbl);
// Determine the width of each of the "sections" of the Label Text.
// Use the fact that AutoSize = true will increase the Width of the
// Label as you increase the Text Length.
int width = lbl.Width;
lbl.Width = 0;
lbl.Text = string.Empty;
lbl.Text = "x";
int delta = lbl.Width;
lbl.Text += "x";
delta = lbl.Width - 2 * (lbl.Width - delta);
lbl.Text = "ToolTip1";
int txt1Width = lbl.Width - delta;
delta = lbl.Width;
lbl.Text += " NoToolTip ";
int txt2Width = lbl.Width - delta;
delta = lbl.Width;
lbl.Text += "ToolTip2";
int txt3Width = lbl.Width - delta;
// Use a System.Windows.Forms.ToolTip and set the caption on
// MouseHover, depending on the Position of the Cursor.
var tip = new ToolTip();
lbl.MouseHover += delegate(object sender, EventArgs e)
{
tip.RemoveAll();
// Find the Point for the ToolTip (relative to the Label) based
// on the Position of the Cursor.
this.Cursor = new Cursor(Cursor.Current.Handle);
Point lblScreenPos = lbl.PointToScreen(Point.Empty);
Point tipPoint = new Point(Cursor.Position.X, Cursor.Position.Y + lbl.Height);
tipPoint = new Point(tipPoint.X - lblScreenPos.X, tipPoint.Y - lblScreenPos.Y);
// Determine the location of the "sections" of Label text.
int x = tipPoint.X;
int txt1Left = 3;
int txt1Right = txt1Left + txt1Width;
int txt3Left = txt1Right + txt2Width;
int txt3Right = txt3Left + txt3Width;
// Show the ToolTip with the correct caption.
if (x >= txt1Left && x <= txt1Right)
{
tip.Show("TOOLTIPA", lbl, tipPoint, tip.AutoPopDelay);
}
else if (x >= txt3Left && x <= txt3Right)
{
tip.Show("TOOLTIPB", lbl, tipPoint, tip.AutoPopDelay);
}
};
lbl.MouseLeave += delegate(object sender, EventArgs e) { tip.RemoveAll(); };
The ToolTip is not underlined and the system default font is automatically used. This can only be overridden by owner-drawing the ToolTip. If you need to make it blue and underline it, check out the OwnerDraw property.
Is this what you're after:
ToolTip toolTip = new ToolTip();
toolTip.SetToolTip(control1, "Hello ");
toolTip.SetToolTip(control2, "world!");
...or am I missing the boat entirely here? It's a bit hard to understand what the problem is from your description.
精彩评论