Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 238
  • Last Modified:

Display html in rich text box

What I want is the following:
I have the path of a html file (with text only, no tables pictures etc.). I want to display the text in the rich text box.
The text must be displayed with all the font settings i.e. font color, size, bold etc.
The user can then edit the text (I already have code to save the rich text box as html).
Some ideas how to solve:
1. Alogrithm to analyse the html tags.
2. Load the page into a webbrowser object and the use the document.body.innertext to get the text (doesn't give the text format).

I'll appreciate a clear answer with a working example.
0
Chaya
Asked:
Chaya
  • 5
  • 3
1 Solution
 
RhaedesCommented:
Do you really need to use a RichTextBox? If you set the HTML document's designMode property to "on" you can edit text directly in the webbrowser. This is much easier than passing text to and fro between the two formats.

For example, put a webbrowser on a form and add this code:

Private Sub Form_Load()
WebBrowser1.Navigate ("about:blank")
End Sub

Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
WebBrowser1.Document.designMode = "On"
End Sub

Obviously, the navigate method loads the file into the webbrowser. To save the file once you've edited it, write the string 'WebBrowser1.Document.All(0).outerHTML' to a file.

Kindest regards,
Rhaedes
0
 
ChayaAuthor Commented:
Your answer might help me...
Is it possible to allow the user to edit the webbrowser text format i.e. change selected font to bold etc.
I need that the editing of the text will be as flexible as by the rich text box object.
Thanks for the speedy reply
0
 
RhaedesCommented:
It's very simple. Here's a simple example: Put a webbrowser (WebBrowser1) on a form, with two checkboxes (Check1 and Check2) to turn bold and italics on an off. Add two comboboxes (Combo1 and combo2) for font size and colour. Reference the HTML Object Library and add the code below.
You'll see how it works with the execCommand methods in the code. For a complete list check out http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/methods/execcommand.asp

Kindest regards,
Rhaedes


Private WithEvents Hdoc As HTMLDocument

Private Sub Check1_Click()
r = Hdoc.selection.createRange
Hdoc.execCommand "Bold", Check1.Value
WebBrowser1.SetFocus
End Sub

Private Sub Check2_Click()
r = Hdoc.selection.createRange
Hdoc.execCommand "Italic", Check1.Value
WebBrowser1.SetFocus
End Sub

Private Sub Combo1_Click()
r = Hdoc.selection.createRange
Hdoc.execCommand "ForeColor", True, Combo1.Text
WebBrowser1.SetFocus
End Sub

Private Sub Combo2_Click()
r = Hdoc.selection.createRange
Hdoc.execCommand "FontSize", True, Combo2.Text
WebBrowser1.SetFocus
End Sub

Private Sub Form_Load()
WebBrowser1.Navigate ("about:blank")

With Combo1
.AddItem "black"
.AddItem "red"
.AddItem "green"
End With

With Combo2
.AddItem "2"
.AddItem "5"
.AddItem "16"
End With

End Sub

Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
WebBrowser1.Document.designMode = "On"
Set Hdoc = WebBrowser1.Document
End Sub
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ChayaAuthor Commented:
Thanks, I tried your code and it works well.
But...
I still have one problem with it:
I want the user to be able to use it like a text editor without realizing it is a webbrowser. I want to know how to cancel the internet popup that comes up with a right click and to display instead my own popup (that has options for changing fonts, bold etc.)
Could you provide me with code for this? I will provide 20 bonus points
0
 
ChayaAuthor Commented:
I have found code to do the above (change menu with right click). I'm checking the code now.
0
 
ChayaAuthor Commented:
I have found code to do the above (change menu with right click). I'm checking the code now.
0
 
RhaedesCommented:
Since Hdoc is declared as withevents, you can trap mouse clicks on the document, and even have different popups depending on the element under the mouse. Basically, use the sub 'Private Sub HDoc_onmousedown()' and chack for a right mouse click using 'If HDoc.parentWindow.event.button = 2 Then...'
You could check out Richie Simon's code at http://www.angelfire.com/realm/vb-shared/IE_WB_DOM_tips.htm#overrwb

Kindest regards,
Rhaedes

0
 
ChayaAuthor Commented:
You seem to be the expert in webbrowsers. Any chance of help with the following:

1.How can I prevent the user from draging and dropping images from other webbrowser controls
2. How can I cause that some webbrowser controls on the form will have no popup menus and others will have my pop up menu
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now