09-13-2016, 11:19 PM
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.
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 ;)
}
}