Create an editor as XmlSpy with C#

G

Groide

Hello, I'm crazy but I want create an editor with c# and
System.Windows.Forms.RichTextBox.
I have many difficult to create the effect to highlights the text and to
close the tags.
This is my code:

int cursor = richTextBox.SelectionStart;

Regex attrs = new Regex(@"<\/?\w+(\s+\w+=""(\w+)"")*>");

MatchCollection matches = attrs.Matches(richTextBox.Text);

foreach (Match match in matches)

{

GroupCollection groups = match.Groups;

Group group = groups[0];

CaptureCollection captures = group.Captures;

for (int i = 0; i < captures.Count; i++)

{

string word = captures.Value;

int index = captures.Index;

richTextBox.Select(index, word.Length);

richTextBox.SelectionColor = Color.DarkBlue;

richTextBox.DeselectAll();

}

Group groupAttrs = groups[2];

CaptureCollection capturesAttrs = groupAttrs.Captures;

for (int i = 0; i < capturesAttrs.Count; i++)

{

string word = capturesAttrs.Value;

int index = capturesAttrs.Index;

richTextBox.Select(index, word.Length);

richTextBox.SelectionColor = Color.Green;

richTextBox.DeselectAll();

}


}

that color html tag in blue and value of attribute in green
BUT
the effect of flickers is horrible (es. select -> color -> deselect ->
select -> color ..etc...)

Any suggestions?
 
D

Dmytro Lapshyn [MVP]

Hello Groide,

What if you use the SelectionStart and SelectionLength properties and make a
good use of the LockWindowUpdate API to prevent any redrawing until the
re-coloring is done (try without LockWindowUpdate first, though)?
 
G

Groide

Thanks a lot, i must imprt a dll on my c# project
[DllImport("user32.dll")]
but the result is perfect.

Groide


Dmytro Lapshyn said:
Hello Groide,

What if you use the SelectionStart and SelectionLength properties and make
a good use of the LockWindowUpdate API to prevent any redrawing until the
re-coloring is done (try without LockWindowUpdate first, though)?

--
Sincerely,
Dmytro Lapshyn [Visual Developer - Visual C# MVP]


Groide said:
Hello, I'm crazy but I want create an editor with c# and
System.Windows.Forms.RichTextBox.
I have many difficult to create the effect to highlights the text and to
close the tags.
This is my code:

int cursor = richTextBox.SelectionStart;

Regex attrs = new Regex(@"<\/?\w+(\s+\w+=""(\w+)"")*>");

MatchCollection matches = attrs.Matches(richTextBox.Text);

foreach (Match match in matches)

{

GroupCollection groups = match.Groups;

Group group = groups[0];

CaptureCollection captures = group.Captures;

for (int i = 0; i < captures.Count; i++)

{

string word = captures.Value;

int index = captures.Index;

richTextBox.Select(index, word.Length);

richTextBox.SelectionColor = Color.DarkBlue;

richTextBox.DeselectAll();

}

Group groupAttrs = groups[2];

CaptureCollection capturesAttrs = groupAttrs.Captures;

for (int i = 0; i < capturesAttrs.Count; i++)

{

string word = capturesAttrs.Value;

int index = capturesAttrs.Index;

richTextBox.Select(index, word.Length);

richTextBox.SelectionColor = Color.Green;

richTextBox.DeselectAll();

}


}

that color html tag in blue and value of attribute in green
BUT
the effect of flickers is horrible (es. select -> color -> deselect ->
select -> color ..etc...)

Any suggestions?

 
Top