What is c# code equivalent of following XAML, where I have a RichTextBox and I have selected Paragraph, and I want to enable/disable white space on this paragraph. In XAML I know how to enable, but I need to do this in code.
<Paragraph xml:space=\"preserve\"> Tabbed Code</Paragraph>
There is an equivalent I have found and its here,
void EnableWhiteSpace(Paragraph p, bool enable = true){
if(enable){
System.Windows.Markup.XmlAttributeProperties
.SetXmlSpace(this.Document, "preserve");
}
else{
System.Windows.Markup.XmlAttributeProperties
.SetXmlSpace(this.Document, "default");
}
}
This is still not working !!! I am not getting tabs !!...
Here is my problem, I have a RichTextBox which is used to edit code and which does syntax highlighting. Everything is fine except when I call following I see no tabs in my code.
TextRange tr = new TextRange(
myRichTextBox.Document.ContentStart,
myRichTextBox.Document.ContentEnd);
string text = tr.Text;
The text that I receive contains no tabs, so I thought enabling whitespace on every paragraph before doing text range might give me tabs.
UPDATE
I tried navigating every inlines (run) in the paragraph, none contains tab, I am just loosing all the tabs :(
There is no equivalent. The XML option xml:space="preserve" is valid only for interpreting XML files (in your case, XAML, which is a kind of XML), and has no meaning in C# as there are no XML files involved.
The C# equivalent of your XAML code would be following:
Paragraph p = new Paragraph();
p.Inlines.Add(new Run(" Tabbed Code"));
RichTextBox is buggy, it will just ignore all tabs, however its problem of WPF itself and cant be fixed without doing complex workarounds.
Alernative is to write your own FlowDocument to text converter, however this is very complex as nodes do not directly give any line or tag information.
Related
I'm trying to add a rich text content control around the user's selected text in a Word document.
I'm new to VSTO and Content Controls so i'm using the MSDN examples as a baseline. The example shows this, which adds the Content Control at the chosen position:
private void AddRichTextControlAtSelection()
{
word.Document currentDocument = Globals.ThisAddIn.Application.ActiveDocument;
currentDocument.Paragraphs[1].Range.InsertParagraphBefore();
currentDocument.Paragraphs[1].Range.Select();
Document extendedDocument = Globals.Factory.GetVstoObject(currentDocument);
richTextControl1 = extendedDocument.Controls.AddRichTextContentControl("richTextControl1");
richTextControl1.PlaceholderText = "Enter your first name";
}
However i want the Content Control to wrap around the user's selected text. Any help, please?
What you found is one possibility. More efficient and "cleaner" (IMO) would be to use the constructor that accepts a RANGE object and pass the Range. If you want the user's selection then
richTextControl1 = extendedDocument.Controls.AddRichTextContentControl(extendedDocument.Parent.Selection.Range, "richTextControl1");
//the Parent of a Document is the Word.Application
//Selection is a dependent of the Word.Application
Otherwise, building on your code sample:
richTextControl1 = extendedDocument.Controls.AddRichTextContentControl(currentDocument.Paragraphs[1].Range, "richTextControl1");
Note that if you don't need to work with VSTO's extensions of the Content Controls you don't need to go through the GlobalFactory steps, you can simply insert the "interop" versions of the Content Controls.
Simple fix in the end: currentDocument.ActiveWindow.Selection.Range.Select();
I am making a pascal code editor in Mono in MonoDevelop. I am using Mono.TextEditor as a code editor widget. However, I cannot find how to highlight a line in the widget.
After compilation, I collect line numbers where errors occur, and so I want to highlight them in red. I found
Mono.TextEditor.LineBackgroundMarker
which seems to relate to what I want to do, but I cannot find where and how to use it.
Another option I was looking into was ViBuilder, but I don't even know how to use that. I can think of two ways to solve this problem:
Simply make highlight
Mark a line as error, as default style includes:
{ "name": "Underline(Error)", "color":"invalid-red" }
which also seems to be a possible solution.
You can highlight lines in the text editor by adding markers to the underlying document. Use the TextDocument.AddMarker method, as follows:
TextEditor textEditor;
var marker = new Mono.TextEditor.LineBackgroundMarker();
int lineNumber = ...;
textEditor.Document.AddMarker(lineNumber, marker);
textEditor.QueueDraw();
Also have a look at the Mono.TextEditor.StyleTextMarker class. This class has already the properties "BackgroundColor" / "Color" that you are looking for. Underlining may have to be done manually (for example by inheriting from StyleTextMarker and overriding the Draw method).
I have Rich TextBox to Edit XML Text
What i want how to color the XML Tags Names inside the RichTextBox
I want the tags names in RED or Green color.
Any Way to do that?
Work out what your desired regex is using this page. Once you have this you could use something like the following method to update the RichTextBox
public static void HighlightSyntax(RichTextBox richTextBox, Regex yourRegex, Color someColor)
{
richTextBox.BeginUpdate();
int selPos = richTextBox.SelectionStart;
richTextBox.SelectAll();
richTextBox.SelectionColor = normTextColor;
richTextBox.Select(selPos, 0);
// For each match from the regex, highlight the word.
foreach (Match keyWordMatch in yourRegex.Matches(richTextBox.Text))
{
richTextBox.Select(keyWordMatch.Index, keyWordMatch.Length);
richTextBox.SelectionColor = someColor;
richTextBox.Select(selPos, 0);
richTextBox.SelectionColor = normTextColor;
}
richTextBox.EndUpdate();
}
You could also adopt a timer to update this automatically after a set time.
I hope this helps.
Note. For large text files, and approach like this will be slow! In this case I would adopt Sinctilla.NET as a full syntax highlighter as stated in one of the answers below...
there are articles which explain or suggest possible approaches to syntax colouring, for example: How To Implement Syntax Highlighting In A WinForms Application
I think the best and easiest way is to use Scintilla.NET to handle that so you can focus on what really matters to you instead of reinventing the wheel again :)
MSDN created a simple C# function that formats the text content from a RichTextBox:
LINK
Using a simple regex find the location (start and end) of each tag and colorize it like below:
richtextbox1.Select(start, end-start);
richtextbox1.SelectionColor = Color.Green;
richtextbox1.Select(start, 0);
Check out scintilla, a nice source code editing component for Windows which supports Syntax Highlighting too. And there's a .NET wrapper for it called ScintillaNET.
I think for small syntax highlighting projects, roll your own! There are some examples of highlighting in a syntax editor already posted.
https://stackoverflow.com/a/13007641/1033686
https://stackoverflow.com/a/13007710/1033686
For larger projects that demand better highlighting, use Scintilla.NET (but be warned, it it a bit heavy and can be tricky to get working!)
http://scintillanet.codeplex.com/
For enterprise projects use a commercial product like actipro syntax editor.
http://www.actiprosoftware.com/products/controls/windowsforms/syntaxeditor?gclid=CI6rrqmglLMCFSfMtAodbE8AhA
i wrote a small application that will monitor the clipboard and paste text directly in a webbrowser component.
...
DocumentWysiwyg = ClipBoardWebBrowser.Document.DomDocument as IHTMLDocument2;
DocumentWysiwyg.designMode = "On";
...
and when the Clipboard changes, i paste the Clipboard content into the webbrowser using:
ClipBoardWebBrowser.Document.Write(Clipboard.GetText(TextDataFormat.Html));
now, when pasting any copied content from web as html, i get inside the html
span class="Apple-converted-space">Â </span
which does not belong to the html i copied from a website.
what are those? and how can i get rid of them?
any help is really appreciated .
here is the html code for google.de as example
http://pastie.org/pastes/3706386/text
how would i make sure that the pasted clipboard is exactly the same as the copied data in the clipboard?
On Mac multiple occurrences of a regular space " " get converted such that they replace every other regular space with a non-breaking space character aka This allows the spaces to still break on every other character, but preserve such ranges of spaces.
The reason for this is because without this trick HTML would compress multiple spaces to a single one. Having only characters would disable line wrapping, because as their name states they would be non-breaking.
In addition to "Apple-converted-space" there was also "Apple-style-span" but that was eliminated from Webkit in 2011: https://www.webkit.org/blog/1737/apple-style-span-is-gone/
So to answer your question: since Webkit is filling the pasteboard you cannot do anything to prevent such behavior.
Hi I want to set the Xaml property of my silverlight richtext box.
this.Dispatcher.BeginInvoke(() =>
{
richTextBox1.Xaml = "<Paragraph>Blah</Paragraph>";
});
However I get the following exception..
System.ArgumentException: value
at System.Windows.Controls.RichTextBox.set_Xaml(String value)
Can anybody explain this ?
Maybe I am not in the
You actually want to add an XML namespace to the string, so that the Paragraph object can be resolved. Like:
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
As you really only want a single xmlns entry, surround it with a Section block. Your complete working string will be this:
richTextBox1.Xaml = "<Section xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"><Paragraph>Blah</Paragraph></Section>";
To work this out, I entered text into a RichTextBox (e.g. "Blah") and then viewed the textbox1.Xaml property (always investigate using working methods first to see what comes out of it).