Hello C#!
I want the paragraph tags to be highlighted as a different color as the text (which is black by default).
I was thinking of something like this.
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
string text = richTextBox1.Text;
char[] sep = { ' ', '\n', '.', ',', ':', ';' };
string[] words = text.Split(sep, System.StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < text.Length; i++)
{
if (text[i] == '<')
{
//change color of tag here
}
}
}
Thats just a rough idea. I know I'm gonna have to add code that will loop through the text and when it finds the '<' loop through the characters there and then stop the color change with the '>' character is found. I know it needs a lot of work, but I'm kinda new at this. Any suggestions or help. If I can get this to work I would be so happy. I've been messing with it for a while. Please let me know.
-Dave
David PullmanPosted Apr 7, 2009, 2:04 PM
StefanPosted Apr 7, 2009, 1:36 PM
I'm glad you got it to work. To stop the flashing, add this namespace: "using System.Runtime.Interop;"
Next, Modify your code to look like this:
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
[DllImport("user32.dll")]
public static extern bool LockWindowUpdate(IntrPtr hWndLock);
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
try
{
LockWindowUpdate(richTextBox1.Handle);
Regex tags = new Regex("?[a-z][a-z0-9]*[^<>]*>", RegexOptions.IgnoreCase);
int selPos = richTextBox1.SelectionStart;
int selLength = richTextBox1.SelectionLength;
foreach (Match match in tags.Matches(richTextBox1.Text))
{
richTextBox1.Select(match.Index, match.Length);
richTextBox1.SelectionColor = Color.Blue;
richTextBox1.SelectionStart = selPos;
richTextBox1.SelectionLength = selLength;
richTextBox1.SelectionColor = Color.Black;
}
}
finally
{
LockWindowUpdate(IntPtr.Zero);
}
}
}
}
David PullmanPosted Apr 7, 2009, 1:21 PM
I figured it out. Could anyone please tell me how to stop the flickering? I search for over an hour, but haven't found anything that I understand.
Here is my code so far:
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;
using System.IO;
using System.Text.RegularExpressions;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
Regex tags = new Regex("?[a-z][a-z0-9]*[^<>]*>", RegexOptions.IgnoreCase);
int selPos = richTextBox1.SelectionStart;
int selLength = richTextBox1.SelectionLength;
foreach (Match match in tags.Matches(richTextBox1.Text))
{
richTextBox1.Select(match.Index, match.Length);
richTextBox1.SelectionColor = Color.Blue;
richTextBox1.SelectionStart = selPos;
richTextBox1.SelectionLength = selLength;
richTextBox1.SelectionColor = Color.Black;
}
}
}
}
StefanPosted Apr 7, 2009, 12:21 PM
You said you tried that, but it did not work. Could you be more specific? Was there an error? Did the text just not change color? Please post back and I'll try to help.
David PullmanPosted Apr 7, 2009, 8:43 AM
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
}
I tried that and it didn't work. Please let me know.
StefanPosted Apr 6, 2009, 8:09 PM
Regex tags = new Regex("?[a-z][a-z0-9]*[^<>]*>"), RegexOptions.IgnoreCase;
int selPos = richTexBox1.SelectionStart;
int selLength = richTextBox1.SelectionLength;
foreach (Match match in tags.Matches(richTextBox1.Text))
{
richTextBox1.Select(match.Index, match.Length);
richTextBox1.SelectionColor = Color.Blue;
richTextBox1.SelectionStart = selPos;
richTextox1.SelectionLength = selLength;
richTextBox1.SelectionColor = Color.Black;
}
This uses regex to find a matches (any code inside of a "<" and ">") and colors it. You may notice some flashing. To stop this you'll need to stop the rich text box from updating.
Hope this helps.
David PullmanPosted Apr 6, 2009, 8:03 PM