Register
Hello There, Guest!


Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Snippet Sharing
#3
Here's one for people more familiar with the .NET framework and languages that rely on it, specifically in this case C#.

This is an extension of the 'RichTextBox' class that redefines 'AppendText' to include a colour option. Somewhat simple but very useful.

Code:
   // Define a class 'RichTextBoxExtensions'. Make sure to put this in yuor namespace and not in another class.
   public static class RichTextBoxExtensions
   {
       // define the AppedText void. This accepts 'this' (RichTextBox object) which will be referred to as '
                                                // text (string type) which is the contents to append to the RichTextBox.
                                                // and colour (Color object) used to define the texts colour.
       
       public static void AppendText(this RichTextBox box, string text, Color colour)
       {
            // Most of this is standard from the default RTB, assuming you're reading into this I'll imagine you know what that does...
           box.SelectionStart = box.TextLength;
           box.SelectionLength = 0;

           box.SelectionColor = colour; // Change the colour, simples.
           box.AppendText(text);
           box.SelectionColor = box.ForeColor;
           box.ScrollToCaret(); // Also, scroll to caret (scroll to the bottom of the RichtextBox). Nice for keeping new appended text always on top ;)
       }
   }
[-] The following 1 user Likes LKD70 's post:
  • Apix
Reply


Messages In This Thread
Snippet Sharing - LKD70 - 09-13-2016, 08:58 PM
RE: Snippet Sharing - Apix - 09-13-2016, 09:34 PM
[Snippet] C# - RichTextExtension - LKD70 - 09-13-2016, 11:19 PM
[Snippet] - Ping! - LKD70 - 09-14-2016, 09:59 AM
RE: Snippet Sharing - CantREKTMe - 09-14-2016, 04:21 PM
RE: Snippet Sharing - Owl - 04-26-2018, 06:57 PM
RE: Snippet Sharing - Shadow Fox - 04-27-2018, 12:56 AM
RE: Snippet Sharing - Owl - 04-27-2018, 01:01 AM
RE: Snippet Sharing - Bobchik34 - 08-25-2023, 05:24 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)