Hello,

I'm in need of color highlighting individual words with in a tooltip.

I'm thinking that the standard tooltip in c# can't do that since it looks like it just takes a unicode string. (Is that correct or is there some gucci way of getting around that?)

I did some googling and found what appears to be a C++ solution(which functionally is what I was looking but is in C++)
http://www.codeguru.com/cpp/controls/richedit/article.php/c2405

I haven't found anything for C# yet? Any suggestions??

Recommended Answers

All 8 Replies

I'm thinking that the standard tooltip in c# can't do that since it looks like it just takes a unicode string. (Is that correct or is there some gucci way of getting around that?)

Yes, just a plain text without formatting.

AFAIK there's only two solutions. Get some third-party tooltip-control or write one of your own. Here's a one example: Balloon ToolTip Control. With some googling you should find more sample code.

HTH

Yes, just a plain text without formatting.

AFAIK there's only two solutions. Get some third-party tooltip-control or write one of your own. Here's a one example: Balloon ToolTip Control. With some googling you should find more sample code.

HTH

Thanks for the quick response but Uggh... I was looking at that Ballon ToolTip Control link you sent.. It look like a ton of coding to get what I want.


I was thinking perhaps I could create a object that would inherit the tooltip object along with a richtextbox. I wonder if could use tooltip to get all the positional information and than just overlay the richtextbox on top? (I'm still sort of new on the C# sharp stuff,(I'm a vb6'er) so I'm not sure if I would regret this approach in the long run. Btw.. Thanks for your search terms. It's providing much better search terms than what I was getting.

I was thinking perhaps I could create a object that would inherit the tooltip object along with a richtextbox

Yes you can do that. In fact you'll end up with coding your own control :)

I'm not sure how to render RTF-formatted text, but if you inherit your control from the ToolTip object, you'll have to write your own Draw method.

Here's a simple example with fixed formatting (no RTF, sorry ;) ). Create a new WinForms project, drop a listview control (listView1) to the form and copy/paste the following code:

using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace BlogTest
{
    // Quick&Dirty user control inherited from ToolTip object
    public class QDToolTip : ToolTip
    {
        // Constructor
        public QDToolTip()
        {
            // Attach own Draw method
            this.Draw += new DrawToolTipEventHandler(QDToolTip_Draw);
        }

        void QDToolTip_Draw(object sender, DrawToolTipEventArgs e)
        {
            // Font properties
            Font ttFont = new Font("Garamond", 10.5F, FontStyle.Regular, GraphicsUnit.Point);
            // Font color
            Brush ttBrush = Brushes.Violet;

            // Set (tooltip) BG color to dark violet
            ((ToolTip)sender).BackColor = Color.DarkViolet;
            // Draw dark violet background
            e.DrawBackground();
            // Draw tooltip text with 10.5pt violet Garamond
            e.Graphics.DrawString(e.ToolTipText, ttFont, ttBrush, e.Bounds);
            // Dispose resources
            ttBrush = null;
            ttFont = null;
        }
    }

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            // 
            QDToolTip MyTip = new QDToolTip();

            // Tell the object that you'll handle the drawing
            MyTip.OwnerDraw = true;
            // Put some text to the tooltip and attach the tooltip to listview control
            MyTip.SetToolTip(listView1, "Formatted Text");
        }
    }
}

Thanks...
I've been playing with the code a little bit... but so far with out success.
within the QDtooltip class I added:
public RichTextBox myRichTextBox = new RichTextBox();

I guess what I'm after to overlay myRichTextBox over the existing Tooltip.
I thinking I need to research docking or something and probably have that overlay within the custom draw method. I'm not sure how to do that yet. If I can get over that hump, I think the Richtext part will be easy.

I'm attempting to overlay a richtextbox over the tool tip.
What I have here seems to compile, seems to make sense in my vb6 brain, but doesn't work.

Is this a dumb approach fundamentally, or am I just missing something really obvious?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


//namespace BlogTest
//{
    // Quick&Dirty user control inherited from ToolTip object
    public class QDToolTip : ToolTip
    {
        public RichTextBox myRichTextBox = new RichTextBox();
        // Constructor
        public QDToolTip()
        {
            // Attach own Draw method
            this.Draw += new DrawToolTipEventHandler(QDToolTip_Draw);
            //myRichTextBox.Dock = DockStyle.Fill;
            
        }

        void QDToolTip_Draw(object sender, DrawToolTipEventArgs e)
        {
            // Font properties
            Font ttFont = new Font("Garamond", 10.5F, FontStyle.Regular, GraphicsUnit.Point);
            // Font color
            Brush ttBrush = Brushes.Violet;

            // Set (tooltip) BG color to dark violet
            ((ToolTip)sender).BackColor = Color.DarkViolet;
            // Draw dark violet background
            e.DrawBackground();
            // Draw tooltip text with 10.5pt violet Garamond
            e.Graphics.DrawString(e.ToolTipText, ttFont, ttBrush, e.Bounds);
                myRichTextBox.Visible = true;
            myRichTextBox.BringToFront();        
            // Dispose resources
            ttBrush = null;
            ttFont = null;
            myRichTextBox.Bounds= e.Bounds;
            myRichTextBox.Top = 0;
            myRichTextBox.Left = 0;
        }
    }
//}


namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        QDToolTip MyTip = new QDToolTip();
        public Form1()
        {
            InitializeComponent();
            // 
            

            // Tell the object that you'll handle the drawing
            MyTip.OwnerDraw = true;
            // Put some text to the tooltip and attach the tooltip to listview control
            MyTip.SetToolTip(listView1, "Formatted Text");
            MyTip.myRichTextBox.Text = "Test";
            
        
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}

What actually happens? (or doesn't happen?)

What actually happens? (or doesn't happen?)

The tooltip text shows up "Formatted text" and nothing is to be seen of "test"

The tooltip seems to behaving a bit weird,(pops up some of the time, but not all the time, when listview1 gets focus.)

Is this a dumb approach fundamentally, or am I just missing something really obvious?

No, it's not. I tried the same thing (if that proofs anything ;) )

But something (maybe obvious) is missing because the RTB does not show up :'(

I changed your code a bit. The RTB is built outside the QDToolTip and passed as a property. The only information needed from the base tooltip is the location where the RTB should be shown.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace BlogTest
{
 // Quick&Dirty user control inherited from ToolTip object
    public class QDToolTip : ToolTip
    {
        // Property
        public RichTextBox myRichTextBox { get; set; }

        // Constructor
        public QDToolTip()
        {
            // Attach own Draw method
            this.Draw += new DrawToolTipEventHandler(QDToolTip_Draw);
        }

        // Popup RichTextBox
        void QDToolTip_Draw(object sender, DrawToolTipEventArgs e)
        {
            myRichTextBox.Location = e.Bounds.Location; // Set Location only
            // Show our RichTextBox, what's missing in here???
            myRichTextBox.Visible = true;
            myRichTextBox.BringToFront();
            myRichTextBox.Show();
        }
    }

    public partial class Form1 : Form
    {
        QDToolTip MyTip = new QDToolTip();

        public Form1()
        {
            InitializeComponent();
            // 
            RichTextBox oRtb = new RichTextBox();
            // Set rtb props, did we miss some property???
            oRtb.Width = 150;
            oRtb.Height = 14;
            oRtb.BackColor = Color.DarkViolet;
            oRtb.ForeColor = Color.Violet;
            oRtb.Font = new Font("Garamond", 10);
            oRtb.Text = "Formatted Text";

            // Tell the object that you'll handle the drawing
            MyTip.OwnerDraw = true;
            // Put some (empty) text to the tooltip and attach the tooltip to listview control
            MyTip.SetToolTip(listView1, " ");
            // Set RichTextBox which should be displayed (instead of ToolTip's text)
            MyTip.myRichTextBox = oRtb;
        }
    }
}

Now the base tooltip is actually a string with a space because we need the functionality and the location from the ToolTip control.

This code doesn't work "as is", but I hope it's a step further. You can put a breakpoint (press F9) at code line 30 to see what is in the myRichTextBox variable.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.