I have two separate C# ASP.NET programs running on the same server. Each of them uses the Richtextbox control in their respective business layer dll's to strip RTF formatting from text stored in the database as such.
var rtf = new RichTextBox {Rtf = itemWeb.RTF_DESCRIPTION};
item.WebDescription = rtf.Text;
The problem is, when both programs execute it often happens where this line fails(on both programs) with the following error.
[Win32Exception (0x80004005): Error creating window handle.]
These programs do not share any code or dll's whatsoever. The only thing in common is the technique used to strip the formatting and the fact that they are on the same server.
Is there a known issue using the Richtextbox this way? I didn't write the code, but it seems non-standard to use a UI element in a dll, even though this is the common solution when searching for how to strip formatting.
Ideally, I would find a solution without using the Richtextbox. I found one using the regex that comes close, but does not guarantee that 100% of the formatting will be stripped. Any explanations as to why this is happening or any workarounds will be appreciated.
Thanks!
I started getting the same error recently with a method in a static class that converts from RTF to Text.
I tracked it down to the RichTextBox not being properly disposed (or possibily quickly enough) even though the context the RichTextBox was in the method (not global).
If your code doesn't get executed a lot, this might not be the same problem.
It can be reproduced by coding a test care that run through the conversion 30,000+ times. Implementing a using clause solved the problem.
using (System.Windows.Forms.RichTextBox rtBox = new System.Windows.Forms.RichTextBox())
{
rtBox.Rtf = str;
str = rtBox.Text; // convert the RTF to plain text.
}
This worked but it is pretty slow. It would be nice to be able to do this without having to create a control, but that's Microsoft's official advice for RTF conversion.
Related
I’ve been searching on here and I’m developing a chatbot that has various responses, some of the responses are pretty long strings. Is there any way to make sure that line breaks don’t occur mid-word in the output? Would I have to insert it in my code before every response or define it before the method begins? Or is this just not possible? Thanks.
There is no built-in way to format string to fit console.
You need to decide what is the criteria for your line breaking algorithm and implement that.
Notes
you need to re-calculate it every time you render the text (assuming you have some sort of history shown) as window size can change (you can resize console windows similar to all other windows thus changing character-width).
depending on the language finding boundaries of words could range from trivial to implement ("just use spaces") to multiyear research project for once that don't use spaces (range similar to xkcd:Tasks :)).
If you have options I'd recommend switching to HTML rendering instead of console as word breaking already done there for you (and much more like proper emojis which you will have hard time with
in console app)
I am upgrading the objects on my form to DevExpress tools as opposed to the standard WinForm tools.
I am working with a field that contains hyperlinks only, I originally tried to use a MemoEdit but found out after googling that it wasn’t possible to use hyperlinks in this type of field. I seen the RichEditControl get mentioned in a few places but it seems to open a whole control and I’m just trying to make text in a certain field a hyperlink.
So I’ve ended up at the HyperLinkEdit field and it’s working as hoped apart from the fact that the text isn’t wrapped/multi line, meaning that the text just carries on as a single line so on the larger file paths the characters are hidden after a certain length due to it not wrapping and starting a new line.
Could anyone provide a pointer for this? I checked the properties and nothing has jumped out to me and the fact that there’s no multi line property makes me think there isn’t a way to do this using this type of field.
Edit:
I've been working on learning Roslyn and have made great progress with using the CSharpSyntaxRewriter mechanism to work on code. My goal in the end is to have a customizable coding standards enforcer. As such, I started with Microsoft's code formatter project from https://github.com/dotnet/codeformatter/releases. Right now what I'm working on is formatting white space.
The goal here is to learn Roslyn, so I want to write the code to format all of the whitespace instead of using the Formatter built into Visual Studio and Roslyn.
Formatter.FormatAsync(document, cancellationToken: cancellationToken);
I'm able to parse syntax trees and know I can implement the code necessary to do this using the CSharpSyntaxRewriter, but what I'd like to do is somehow simply get the raw source text for the code, do whatever manipulations are necessary character by character in the source file, and then put the new text back as the document.
However, I cannot figure out if it's even possible to do what I'm trying to do with Roslyn/Visual Studio. I would have thought so, since the source is simply a text file that's already loaded into Visual Studio, and the 'document' object can be acquired for it.
Edit
The optimum solution would be a drop down (or context) menu for C# source files that then ran all modifications on the file. I was unable to link MEF/MPF to any sort of hook that would allow whole-scale modifications of a source file. As such, I went to a console application.
From what I understand, MEF/MPF can provide single entry points to the source file, whereas the Roslyn interface allows simultaneous access to the entire source file at one time. I believe that's why Microsoft used Roslyn in order to implement their coding standards enforcer.
As I said, I've only been looking at Roslyn for about a month, so I may be missing something obvious.
MS Word has this capability in its Hebrew and Arabic versions. I would like to achieve this in a windows desktop application, using .Net (may be with win-api calls).
As explained in the link provided by Otaku here, current rich text edit controls can not handle this (unless you go for the hack OP in that Q did, which did not seem like a very good solution).
You could write code to do this manually yourself, ditching the text edit control completely, but that would probably mean a lot of work. It took Microsoft years to get support for combining diacritics working properly in MSWord. I would search for open source software that has this capability, and look at how other developers have done it. It might be hard to find, though, and you would likely have to step outside .NET-land. Maybe OpenOffice can do this?
This discussion might also be of help.
I am afraid that you will find, though, that you'll have to manually parse the Unicode and assign colors to the correct glyphs. If you want to be complete, that is one heck of a job.
In a C# console app I have the need to extract the text from an RTF string, add some more text to it, and then convert it back into RTF. I have been able to do this using the System.Windows.Forms.RichTextBox class, but I find it a bit odd to use a Forms control in a non-Forms app. Any better way to do this?
Doing anything with RTF is pretty difficult unless you're using the windows forms. As stated above, using forms is the easiest way to go.
You could write something yourself, but the RTF spec is pretty complicated.
http://www.biblioscape.com/rtf15_spec.htm
Or you could use a conversion DLL / ActiveX object of which there is a large number available.
http://www.sautinsoft.com/
Or - If you're doing this from Linux, there are also tools available. A cursory glance throws up UnRTF
http://www.gnu.org/software/unrtf/unrtf.html
I haven't included stuff to turn text back to RTF because I think the RTF specification treats and formats text correctly.
I think you should just shake this feeling of "odd". There's nothing odd about it.
It depends on what you mean by 'better'. You are already using the simplest and easiest way of doing it.
There is nothing wrong with using an user-interface control in a console application or even in a web application. The Windows controls are part of the .NET Framework, might as well use them. These controls do not need to be hosted in "forms" in order to work.
Reinventing the wheel, using DLL/ActiveX/OCX, and using Linux are simply not practical answers to your question. The better way is...do what you know. There is actually a performance and maintainence benefit to using existing framework methods then using the suggested alternatives.