How to retain Colors in my Richtextbox even after Replacing - vb.net

I have Richtexbox with some texts and markup tags, i color it based on the Tags to different colors in form load, coloring of texts/tags works fine,
Problem now is when i tried to replace some Text inside My RTFbox, after coloring, the Color seems to vanish everywhere,
I want to retain all the coloring's i did even after any kind of replacements/Editing's in richtextbox, kindly help

When you delete the text during the replace, you also delete and formatting the text contained. The inserted text will default to whatever style is set for the area it's being inserted. If you want to retain the styling, you will have to get the current styling of the text and save it in memory, and then apply it to the new text you're inserting, something like this:
RichTextBox1.Select(0, 5)
Dim txtStyle As Font = RichTextBox1.SelectionFont
Then you can apply txtStyle to whatever text you're inserting/replacing the old stuff with

Related

vb.net I am trying to size a control (label, textbox, richtextbox) so that it is the smallest square that contains a given string

I need to present arbitrary text, which might include line breaks, in a square control with word wrap such that the square is as small as possible but sufficiently large to display the text in its entirety.
This is the closest I've gotten so far:
Load the text into a temporary rich text box, Rich, with BorderStyle = none
I increment the value of size, width=height=size until Rich.ClientRectangle.Width = Rich.Width.
If I could now extract the visual lines one by one I would be done, as I could then save the original string with appropriate line breaks for later display.
Visual lines are not, of course, the same as Rich.Lines array.
Any ideas?
Thank you.

Update [Style] to Match Selection option on Character Style via macro

I'm having an issue with some character style that doesn't reflect the style change when applied (this doesn't happen always):
Example: I have a character style for italics, and when I apply the character style, the word still appears as normal (but the char style is applied, and checking the properties, the style has the check ok for italics).
In order to fix this, I select the word with the issue, right click on the style and use the option "Update [StyleName] to match selection", and it displays the italics correctly.
The problem here is that when I try to replicate this behavior with a VBA Macro (via recording macro), the macro that Word writes has this error:
Run-time error '5900': The property is not allowed for character styles. This is the line with error:
ActiveDocument.Styles("StyleItalic").AutomaticallyUpdate = False
Looking at the code created via Macro, seems that it is not possible for a character style to be automatically updated.
The character style is also created via macro, and I can't see anything wrong in the style:
Private Sub Creo(style As String, fontName As String, fontSize As Integer, hasItalic As Boolean)
On Error Resume Next
Selection.ClearFormatting
ActiveDocument.Find.style = ActiveDocument.Styles(style)
ActiveDocument.Find.Execute
If ActiveDocument.Find.Found = False Then
ActiveDocument.Styles.Add name:=style, Type:=wdStyleTypeCharacter
ActiveDocument.Styles(style).QuickStyle = True
ActiveDocument.Styles(style).font.Size = fontSize
ActiveDocument.Styles(style).font.name = fontName
ActiveDocument.Styles(style).font.Italic = hasItalic
End If
End Sub
Is there a way to fix this? Hope I explain myself. I am working with 1000+ pages Word Document, so this becomes a bit too tedious for manual editing. Also, sometimes the style in other word works ok, but other doesn't. (All the words have both paragraph style and character style)
Thanks!
Your question describes two unrelated issues.
Applying a character style with the same property as the underlying
paragraph style will cause that property to be turned off in the
text. Updating the character style to match the selection will have
the opposite effect to what you want.
To demonstrate: in a new document type a paragraph of text and apply
a style that is defined as italic, e.g. Quote or Intense Quote.
Select the whole paragraph and press Ctrl+I to turn off italics. Now
select just part of the text and apply the character style named
Emphasis. You will see that it has no apparent effect on the text.
This is because both the paragraph style and the character style are
italic, cancelling each other out and having the same effect as
turning italics off manually.
Now right click on Emphasis and select Update to Match Selection.
The selected text will now be italic but, as the text preview in the
Quick Styles gallery will show you, Emphasis is no longer italic.
Only paragraph and linked styles have the Automatically Update
property, which is why you get an error when attempting to set it on a character style.

Check if MigraDoc font of a row is bold or not

I have a MigraDoc Table with multiple rows . I am trying to find if a particular row is bold or not
if(table.Rows[0].Font.bold ==font.bold)
{
Do Something
}
Idea is to change the colour of that row to a specific one.
There are many ways to set font attributes with MigraDoc. What do you want to happen if some columns are bold and some are not? Or if some columns contain both bold and regular text?
The clean approach would be determining the color of the row when you add contents to it. Each MigraDoc document element has a Tag member of type object that you can use for your own purposes.
When filling the row you can set the color directly. Or you can use the Tag member to mark the row as "important" and set the colors for important rows at a later stage.
Untested code that may work - and since there are several ways to make text bold, this will work only if the code that fills the rows also sets the Bold property to true:
if (table.Rows[0].Format.Font.Bold == true)
{
Do Something
}
The above will not work if a row is "bold", but the boldness comes from a Style or is set via paragraph properties.
IMHO using the Tag member is a cleaner way.

RichEditBox loses formatting for empty text

I have something like a diagramming UWP application where each shape has an RichEditBox. The problem is, that I loose the formatting when the text is empty.
For Example:
Change the text to bold: EditBox.Document.Selection.CharacterFormat.Bold = FormatEffect.On.
Delete all text
Get the text via "EditBox.Document.GetText(GetTextFormat.FormatRtf, out text);
=> The result is just an empty string, not valid RTF document at all. This is a problem in my scenario. Is there an approach to solve it? I think RichEditBox should always provide a valid RTF document.

how to set a style for single words in a word document

I would like to insert single words with a specific style in a word document from a macro. Whenever I change the Style property of the Selection object or even of a range (e.g. ActiveDocument.Range(1,3)) the style of the whole paragraph is set.
How can I do this?
You're trying to apply a paragraph style.
These styles can only be applied to complete paragraphs.
If you apply a character style, it will work fine.
You can simply select the word with your mouse (click&drag with the cursor) and then select the text properties such as the font, only the selected characters will change to that font.