Start New Discussion within our Software Development Community

Hi. I would like to achieve an effect similar to how strings in between the quotation marks in Visual C# are colored. I would like to italicize the text for anything between two asterisks (Including the asterisks themselves) inside a RichTextBox. For example:

This is not counted *This is counted* This is not *This is*

Would I use regular expressions to do this, or would i have no way of changing the text style back in the RichTextBox that way. Any help is greatly appreciated.

This method will allow you to italicize between any two characters. You can even specify a starting point if you want (works for .Net 4.0, otherwise remove the '= 0' default specifier and it will work under anything, but you'll have to provide all three parameters).

private void Italics(RichTextBox r, char m, int s = 0) {
    int end;
    int start;
    bool more = true;

    while (more) {
        start = r.Text.IndexOf(m, s);
        end = r.Text.IndexOf(m, start + 1);
        if (start >= 0 && end > start) {
            r.SelectionStart = start;
            r.SelectionLength = end - start + 1;
            r.SelectionFont = new Font(r.SelectionFont, FontStyle.Italic);
        } else {
            more = false;
        }

        s = end + 1;
    }
}
This question has already been answered. Start a new discussion instead.