Click here to Skip to main content
15,886,689 members
Articles / Programming Languages / C#

[WinForms] RichTextBox ToolTip like Visual-Studio's

Rate me:
Please Sign up or sign in to vote.
5.00/5 (7 votes)
25 Sep 2012CPOL2 min read 44.5K   2.8K   23   11
C# RichTextBox tooltip like Visual Studio's.

Introduction    

This is my first post on the CodeProject.

The main idea that pushed me to write this article is that I thought it might be useful to you as it was for me! First of all I searched on the net for something like this component, but unfortunately I didn't find anything like a readymade control or component to use in a project of mine when I was in need for it. So, I decided to stop relying on others work :p and started to make my own. The component works as the following figure shows: 

Image 1

The component  dependencies

The component inherits from the standard ToolTip and relies on the following .NET methods of the RichTextBox control:  

  • GetCharIndexFromPosition(Point)
  • GetLineFromCharIndex(int)

The component is style-able: 

  • You can specify your own color for the text of both; ToolTip title / ToolTip description text. 
  • You can specify your own font for the text for each of both; ToolTip title / ToolTip description text.
  • You can set your own background bitmap.

How to use the component

Here's a step-by-step:

  1. On your project add a reference to the class library file "RTB_ToolTip.dll".
  2. On the VS designer add a new RichTextBox control and let's name it "richTextBox1"
  3. On the VS editor add the following using-directive: 
  4. C#
    using RTB_ToolTip;
  5. After the 'InitializeComponent' or on the 'Load' event (or wherever you want) do the following:
  6. C#
    // Initialize a new RichTextBoxToolTip component
    RichTextBoxToolTip rtb = new RichTextBoxToolTip();
    
    // Set the desired RichTextBox control to use the ToolTip with
    rtb.RichTextBox = richTextBox1;
    
    // Initialize a new dictionary to fill in the desired information data
    eDictionary dict = new eDictionary();
    dict.Add("Title", "Description of the ToolTip here");
    dict.Add("abc", "Alphabets :)");
    dict.Add("123", "Numbers ;)");
    
    // Assigne the dictionary to the RichTextBoxToolTip
    rtb.Dictionary = dict;
  7. Now push F5 button or click on Debug button to see your new RichTextBox behavior.
  8. When the window is shown, type on the richTextBox1 control something like this "Title" / "abc" / "123" (quotes are not necessary). Now put the mouse over a word you typed and the ToolTip will show up automatically like this:

    Image 2

Additional info  

  • If you want to change ToolTip Title/Description color, do the following:
  • C#
    rtb.TitleBrush = Brushes.Blue;  

    Or:

    C#
    rtb.DescriptionBrush = new SolidBrush(Color.Red);
  • You can also set your own pre and after text of the ToolTip title. For example: 
  • C#
    rtb.TitlePrefix = "Definition for '";
    rtb.TitleSuffix = "':";

    The result would look like this:

    Image 3

    You can access the other properties (TitleFont / DescriptionFont / BackgroundImage...) by the same way.

  • For customizing the way the 'SyntaxCheking' class determines words' start and end boundaries you can reset that in the RichTextBoxToolTip.Chars property. Example: 
  • C#
    rtb.Chars = new List<Char>() {' ', '(', ')', '?'};

How does it work?  

Sorry if the source code looks a little bit complicated to understand, I'll try to tell you how it works. See the following figure please:

Image 4

License

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


Written By
Student
Morocco Morocco
He is a 21 year-old student studying at Mohammed I University (in Oujda - Morocco). Born in Aklim - Berkane, Morocco.
Hates discrimination with all its kinds.
He is interested in programming on Visual Studio since 2009.
Current programming languages:
+ VBScript (for both: Windows & web).
+ JScript (for both: Windows & web).
+ Visual Basic 6.0 and higher.
+ C# 2003 and higher.
+ RealBasic.
+ Html.
+ Still learning C++.

Comments and Discussions

 
GeneralMy vote of 5 Pin
JakirBB13-Jul-13 10:25
JakirBB13-Jul-13 10:25 
Questionnice work sir but got a question here Pin
Elegiac2-May-13 20:01
Elegiac2-May-13 20:01 
Question@Bvnvbn Sdasdg Pin
Hicham El Horri25-Mar-13 15:18
Hicham El Horri25-Mar-13 15:18 
Sorry, but it is a bit complecated to make it work with 'multiline'. This tooltip relies on 'DrawString()' method to display both of title (keyword) and description, so the case is that it's not easy to draw the 'string' multilined. But, it is a bug that will be fixed in future releases, incha' Allah Wink | ;) (I'm working on it)
QuestionIt's really odd behaviour!! Pin
Hicham El Horri25-Mar-13 15:12
Hicham El Horri25-Mar-13 15:12 
QuestionSmall Issue Pin
mikeaerni17-Mar-13 17:00
mikeaerni17-Mar-13 17:00 
QuestionIs Very Good But I hava a Question to Ask Pin
Bvnvbn Sdasdg5-Jan-13 6:34
Bvnvbn Sdasdg5-Jan-13 6:34 
QuestionThanks Pin
Hicham El Horri27-Oct-12 20:55
Hicham El Horri27-Oct-12 20:55 
GeneralMy vote of 5 Pin
pcs041425-Oct-12 4:10
pcs041425-Oct-12 4:10 
QuestionCool Pin
Brisingr Aerowing8-Oct-12 3:32
professionalBrisingr Aerowing8-Oct-12 3:32 
QuestionGood Work Pin
Al-Samman Mahmoud24-Sep-12 8:50
Al-Samman Mahmoud24-Sep-12 8:50 
AnswerRe: Good Work Pin
Hicham El Horri24-Sep-12 22:07
Hicham El Horri24-Sep-12 22:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.