Highlighting/Coloring charactars in a wpf richtextbox
public static void HighlightText(RichTextBox richTextBox,int startPoint,int endPoint, Color color)
{
//Trying to highlight charactars here
}
Startpoint is the first character that should be highlighted and the endPoint the last one.
Have been searching the web for quiet a while now but i still haven't figured out how to solve my problem.
I hope any of you knows how to solve this problem.
c# wpf richtextbox
add a comment |
public static void HighlightText(RichTextBox richTextBox,int startPoint,int endPoint, Color color)
{
//Trying to highlight charactars here
}
Startpoint is the first character that should be highlighted and the endPoint the last one.
Have been searching the web for quiet a while now but i still haven't figured out how to solve my problem.
I hope any of you knows how to solve this problem.
c# wpf richtextbox
4
Possible duplicate of changing the colour of certain characters and count in RichTextBox
– ikerbera
Nov 15 '18 at 12:55
The posted duplicate is linked to a winforms application
– Sander Boonstra
Nov 15 '18 at 13:32
My bad. Here you have the duplicate for WPF.
– ikerbera
Nov 15 '18 at 13:35
That one doesnt work either for wpf
– Sander Boonstra
Nov 19 '18 at 13:31
add a comment |
public static void HighlightText(RichTextBox richTextBox,int startPoint,int endPoint, Color color)
{
//Trying to highlight charactars here
}
Startpoint is the first character that should be highlighted and the endPoint the last one.
Have been searching the web for quiet a while now but i still haven't figured out how to solve my problem.
I hope any of you knows how to solve this problem.
c# wpf richtextbox
public static void HighlightText(RichTextBox richTextBox,int startPoint,int endPoint, Color color)
{
//Trying to highlight charactars here
}
Startpoint is the first character that should be highlighted and the endPoint the last one.
Have been searching the web for quiet a while now but i still haven't figured out how to solve my problem.
I hope any of you knows how to solve this problem.
c# wpf richtextbox
c# wpf richtextbox
edited Nov 15 '18 at 13:28
Sander Boonstra
asked Nov 15 '18 at 12:52
Sander BoonstraSander Boonstra
32
32
4
Possible duplicate of changing the colour of certain characters and count in RichTextBox
– ikerbera
Nov 15 '18 at 12:55
The posted duplicate is linked to a winforms application
– Sander Boonstra
Nov 15 '18 at 13:32
My bad. Here you have the duplicate for WPF.
– ikerbera
Nov 15 '18 at 13:35
That one doesnt work either for wpf
– Sander Boonstra
Nov 19 '18 at 13:31
add a comment |
4
Possible duplicate of changing the colour of certain characters and count in RichTextBox
– ikerbera
Nov 15 '18 at 12:55
The posted duplicate is linked to a winforms application
– Sander Boonstra
Nov 15 '18 at 13:32
My bad. Here you have the duplicate for WPF.
– ikerbera
Nov 15 '18 at 13:35
That one doesnt work either for wpf
– Sander Boonstra
Nov 19 '18 at 13:31
4
4
Possible duplicate of changing the colour of certain characters and count in RichTextBox
– ikerbera
Nov 15 '18 at 12:55
Possible duplicate of changing the colour of certain characters and count in RichTextBox
– ikerbera
Nov 15 '18 at 12:55
The posted duplicate is linked to a winforms application
– Sander Boonstra
Nov 15 '18 at 13:32
The posted duplicate is linked to a winforms application
– Sander Boonstra
Nov 15 '18 at 13:32
My bad. Here you have the duplicate for WPF.
– ikerbera
Nov 15 '18 at 13:35
My bad. Here you have the duplicate for WPF.
– ikerbera
Nov 15 '18 at 13:35
That one doesnt work either for wpf
– Sander Boonstra
Nov 19 '18 at 13:31
That one doesnt work either for wpf
– Sander Boonstra
Nov 19 '18 at 13:31
add a comment |
1 Answer
1
active
oldest
votes
Here is the general idea:
public static void HighlightText(RichTextBox richTextBox, int startPoint, int endPoint, Color color)
{
//Trying to highlight charactars here
TextPointer pointer = richTextBox.Document.ContentStart;
TextRange range = new TextRange(pointer.GetPositionAtOffset(startPoint), pointer.GetPositionAtOffset(endPoint));
range.ApplyPropertyValue(TextElement.BackgroundProperty, new SolidColorBrush(color));
}
However you will need to iterate through the document to get the correct text positions, since offset index number doesn't match number of characters. Some characters may represent multiple offset positions.
Here is the method that does that. I'm not aware of a way to do this without looping through the entire document.
public static void HighlightText(RichTextBox richTextBox, int startPoint, int endPoint, Color color)
{
//Trying to highlight charactars here
TextPointer pointer = richTextBox.Document.ContentStart;
TextPointer start = null, end = null;
int count = 0;
while (pointer != null)
{
if (pointer.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text)
{
if (count == startPoint) start = pointer.GetInsertionPosition(LogicalDirection.Forward);
if (count == endPoint) end = pointer.GetInsertionPosition(LogicalDirection.Forward);
count++;
}
pointer = pointer.GetNextInsertionPosition(LogicalDirection.Forward);
}
if (start == null) start = richTextBox.Document.ContentEnd;
if (end == null) end = richTextBox.Document.ContentEnd;
TextRange range = new TextRange(start, end);
range.ApplyPropertyValue(TextElement.BackgroundProperty, new SolidColorBrush(color));
}
It seems to work for the first time the method is called. However when the method is called for the second time it doesnt seem to work properly. Hopefully you might have an idea to fix this problem?
– Sander Boonstra
Nov 19 '18 at 13:16
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53319918%2fhighlighting-coloring-charactars-in-a-wpf-richtextbox%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Here is the general idea:
public static void HighlightText(RichTextBox richTextBox, int startPoint, int endPoint, Color color)
{
//Trying to highlight charactars here
TextPointer pointer = richTextBox.Document.ContentStart;
TextRange range = new TextRange(pointer.GetPositionAtOffset(startPoint), pointer.GetPositionAtOffset(endPoint));
range.ApplyPropertyValue(TextElement.BackgroundProperty, new SolidColorBrush(color));
}
However you will need to iterate through the document to get the correct text positions, since offset index number doesn't match number of characters. Some characters may represent multiple offset positions.
Here is the method that does that. I'm not aware of a way to do this without looping through the entire document.
public static void HighlightText(RichTextBox richTextBox, int startPoint, int endPoint, Color color)
{
//Trying to highlight charactars here
TextPointer pointer = richTextBox.Document.ContentStart;
TextPointer start = null, end = null;
int count = 0;
while (pointer != null)
{
if (pointer.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text)
{
if (count == startPoint) start = pointer.GetInsertionPosition(LogicalDirection.Forward);
if (count == endPoint) end = pointer.GetInsertionPosition(LogicalDirection.Forward);
count++;
}
pointer = pointer.GetNextInsertionPosition(LogicalDirection.Forward);
}
if (start == null) start = richTextBox.Document.ContentEnd;
if (end == null) end = richTextBox.Document.ContentEnd;
TextRange range = new TextRange(start, end);
range.ApplyPropertyValue(TextElement.BackgroundProperty, new SolidColorBrush(color));
}
It seems to work for the first time the method is called. However when the method is called for the second time it doesnt seem to work properly. Hopefully you might have an idea to fix this problem?
– Sander Boonstra
Nov 19 '18 at 13:16
add a comment |
Here is the general idea:
public static void HighlightText(RichTextBox richTextBox, int startPoint, int endPoint, Color color)
{
//Trying to highlight charactars here
TextPointer pointer = richTextBox.Document.ContentStart;
TextRange range = new TextRange(pointer.GetPositionAtOffset(startPoint), pointer.GetPositionAtOffset(endPoint));
range.ApplyPropertyValue(TextElement.BackgroundProperty, new SolidColorBrush(color));
}
However you will need to iterate through the document to get the correct text positions, since offset index number doesn't match number of characters. Some characters may represent multiple offset positions.
Here is the method that does that. I'm not aware of a way to do this without looping through the entire document.
public static void HighlightText(RichTextBox richTextBox, int startPoint, int endPoint, Color color)
{
//Trying to highlight charactars here
TextPointer pointer = richTextBox.Document.ContentStart;
TextPointer start = null, end = null;
int count = 0;
while (pointer != null)
{
if (pointer.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text)
{
if (count == startPoint) start = pointer.GetInsertionPosition(LogicalDirection.Forward);
if (count == endPoint) end = pointer.GetInsertionPosition(LogicalDirection.Forward);
count++;
}
pointer = pointer.GetNextInsertionPosition(LogicalDirection.Forward);
}
if (start == null) start = richTextBox.Document.ContentEnd;
if (end == null) end = richTextBox.Document.ContentEnd;
TextRange range = new TextRange(start, end);
range.ApplyPropertyValue(TextElement.BackgroundProperty, new SolidColorBrush(color));
}
It seems to work for the first time the method is called. However when the method is called for the second time it doesnt seem to work properly. Hopefully you might have an idea to fix this problem?
– Sander Boonstra
Nov 19 '18 at 13:16
add a comment |
Here is the general idea:
public static void HighlightText(RichTextBox richTextBox, int startPoint, int endPoint, Color color)
{
//Trying to highlight charactars here
TextPointer pointer = richTextBox.Document.ContentStart;
TextRange range = new TextRange(pointer.GetPositionAtOffset(startPoint), pointer.GetPositionAtOffset(endPoint));
range.ApplyPropertyValue(TextElement.BackgroundProperty, new SolidColorBrush(color));
}
However you will need to iterate through the document to get the correct text positions, since offset index number doesn't match number of characters. Some characters may represent multiple offset positions.
Here is the method that does that. I'm not aware of a way to do this without looping through the entire document.
public static void HighlightText(RichTextBox richTextBox, int startPoint, int endPoint, Color color)
{
//Trying to highlight charactars here
TextPointer pointer = richTextBox.Document.ContentStart;
TextPointer start = null, end = null;
int count = 0;
while (pointer != null)
{
if (pointer.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text)
{
if (count == startPoint) start = pointer.GetInsertionPosition(LogicalDirection.Forward);
if (count == endPoint) end = pointer.GetInsertionPosition(LogicalDirection.Forward);
count++;
}
pointer = pointer.GetNextInsertionPosition(LogicalDirection.Forward);
}
if (start == null) start = richTextBox.Document.ContentEnd;
if (end == null) end = richTextBox.Document.ContentEnd;
TextRange range = new TextRange(start, end);
range.ApplyPropertyValue(TextElement.BackgroundProperty, new SolidColorBrush(color));
}
Here is the general idea:
public static void HighlightText(RichTextBox richTextBox, int startPoint, int endPoint, Color color)
{
//Trying to highlight charactars here
TextPointer pointer = richTextBox.Document.ContentStart;
TextRange range = new TextRange(pointer.GetPositionAtOffset(startPoint), pointer.GetPositionAtOffset(endPoint));
range.ApplyPropertyValue(TextElement.BackgroundProperty, new SolidColorBrush(color));
}
However you will need to iterate through the document to get the correct text positions, since offset index number doesn't match number of characters. Some characters may represent multiple offset positions.
Here is the method that does that. I'm not aware of a way to do this without looping through the entire document.
public static void HighlightText(RichTextBox richTextBox, int startPoint, int endPoint, Color color)
{
//Trying to highlight charactars here
TextPointer pointer = richTextBox.Document.ContentStart;
TextPointer start = null, end = null;
int count = 0;
while (pointer != null)
{
if (pointer.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text)
{
if (count == startPoint) start = pointer.GetInsertionPosition(LogicalDirection.Forward);
if (count == endPoint) end = pointer.GetInsertionPosition(LogicalDirection.Forward);
count++;
}
pointer = pointer.GetNextInsertionPosition(LogicalDirection.Forward);
}
if (start == null) start = richTextBox.Document.ContentEnd;
if (end == null) end = richTextBox.Document.ContentEnd;
TextRange range = new TextRange(start, end);
range.ApplyPropertyValue(TextElement.BackgroundProperty, new SolidColorBrush(color));
}
answered Nov 15 '18 at 15:43
Neil BNeil B
8971412
8971412
It seems to work for the first time the method is called. However when the method is called for the second time it doesnt seem to work properly. Hopefully you might have an idea to fix this problem?
– Sander Boonstra
Nov 19 '18 at 13:16
add a comment |
It seems to work for the first time the method is called. However when the method is called for the second time it doesnt seem to work properly. Hopefully you might have an idea to fix this problem?
– Sander Boonstra
Nov 19 '18 at 13:16
It seems to work for the first time the method is called. However when the method is called for the second time it doesnt seem to work properly. Hopefully you might have an idea to fix this problem?
– Sander Boonstra
Nov 19 '18 at 13:16
It seems to work for the first time the method is called. However when the method is called for the second time it doesnt seem to work properly. Hopefully you might have an idea to fix this problem?
– Sander Boonstra
Nov 19 '18 at 13:16
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53319918%2fhighlighting-coloring-charactars-in-a-wpf-richtextbox%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
4
Possible duplicate of changing the colour of certain characters and count in RichTextBox
– ikerbera
Nov 15 '18 at 12:55
The posted duplicate is linked to a winforms application
– Sander Boonstra
Nov 15 '18 at 13:32
My bad. Here you have the duplicate for WPF.
– ikerbera
Nov 15 '18 at 13:35
That one doesnt work either for wpf
– Sander Boonstra
Nov 19 '18 at 13:31