简体   繁体   中英

winforms richtextbox how do I know if it is plain text or html

I have a richtextbox, correct me if I am using the wrong control, and they are updating a news portal on their website with it's content.

I am allowing them to use hmtl or they can type in whatever they want.

Here's two examples:

With html:

在此处输入图片说明

Without html:

在此处输入图片说明

My problem is on the carriage returns. If I do not replace the carriage returns in the 'plain' text that they are typing in, it all shows up on one line.

If they don't use html, then I have to replace the carriage returns with <br /> and if they use them, I can't since they are already using breaks.

How do I make it where they can use 'plain' text without html and still make it look right when I insert it into the web page versus using html and not screwing up the looks of it with unnecessary breaks?

This is what I started with, but it only adds more breaks when html is used than necessary:

  Private Sub ShowBody_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    Dim newbody = (String.Format("<div class=""fulldiv""><fieldset><legend>{0}</legend><div><br/>{1}</div></fieldset></div>", Title, Body)).Replace(vbLf, "<br />")
    WebBrowser1.Navigate("about:blank")
    WebBrowser1.Document.OpenNew(False)
    WebBrowser1.Document.Write(newbody)
    WebBrowser1.Refresh()
  End Sub

=== UPDATE ===

I tried the and it was close but it is not showing the text just right if they hit enter multiple times.

Here is an example:

在此处输入图片说明

So, it's not displaying as the users intends.

This is a fairly common problem. Many applications use a checkbox (or something similar) for the user to check if html is being entered. If it is not checked, then <br> is added at the end of each line when it is submitted.

try doing this:

"<pre>" & textbox1.text & "</pre>"

why don't you just replace the carriage returns?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM