programmer01

Hello,

I am trying to make it so that if you select a certain option out of 3 options then the RichTextbox will change...or somthing like a RichTextBox here is what I am unsure about. This will be an Html editor, so there are 3 buttons:

- Code

- Split

- View

Code, of corse, will show the HTML code of the current Html page you are editing. Split will split the Code and View so you can see them both. And View is visual, if you have ever used Frontpage...mabee Microsoft Expression Web...or even kinda like Visual Studio, it is the design view of the website so you cna visually do things without code.

There is my problem with that:

I know how to do code view, RichTextBox, but View/Design....how do I do somthing that will let you visually do that the way frontpage and Expression Web do it, it kinda works like a RichTextBox but it reads the HTML or somthing... I dont know if it is a RichTextBox or what and how would I amke it so that if you, for example, Type in the words "Cool this works" then change the color to green, then it will autmatically add this code to the code view:

<FONT color=darkolivegreen>Cool this works</FONT>

or if you were to make a table in code view:

<TABLE cellSpacing=2 cols=4 cellPadding=2 width="50%" bgColor=forestgreen

border=1>

<TBODY>

<TR>

<TD width="25%">&nbsp;</TD>

<TD width="25%">&nbsp;</TD>

<TD width="25%">&nbsp;</TD>

<TD width="25%">&nbsp;</TD></TR>

<TR>

<TD width="25%">&nbsp;</TD>

<TD width="25%">&nbsp;</TD>

<TD width="25%">&nbsp;</TD>

<TD width="25%">&nbsp;</TD></TR>

<TR>

<TD width="25%">&nbsp;</TD>

<TD width="25%">&nbsp;</TD>

<TD width="25%">&nbsp;</TD>

<TD width="25%">&nbsp;</TD></TR>

<TR>

<TD width="25%">&nbsp;</TD>

<TD width="25%">&nbsp;</TD>

<TD width="25%">&nbsp;</TD>

<TD width="25%">&nbsp;</TD></TR></TBODY></TABLE>

then it would a table that is exactly how you told it to be in the HTML... and so on...

Thanks,

Programmer01




Re: Visual C# Express Edition RichTextBox Html

paoloTheCool

Make a tab control for the different options (code, split, design/preview) like Microsoft FrontPage has. In the preview/design tab, put a webBrowser and on tabControl SelectedIndexChanged event if the selected tab is the design (if you need code, please ask Smile ) then t

do

Code Block
webBrowser1.DocumentText = richTextBox1.Text; //or whatever

Hope this gives you insight...

Smile

Thanks,

paoloTheCool





Re: Visual C# Express Edition RichTextBox Html

programmer01

Ah, I see now, but how would I make the webbrowser allow the text in it be edited So right now there is code view and design mode. If I type Html in code view and then go to Design view then it will show the result of the HTML but how could I make it so that in the design view you can edit it as if it was a textbox itself and not a webbrowser

Or would I have to make a custom control that is a RichTextBox that has some of the functions of a Webbrowser control( like the reading HTML functions and all)

Thanks,

Programmer001






Re: Visual C# Express Edition RichTextBox Html

Derek Smyth

Hi,

I've been wanting an editable web browser for a while now so I took your question as a reason to generate one. So here is how to create your own.

1. Add a reference to your project to the Microsoft HTML Object library (in the COM tab), this will add an assembly called MSHTML.

2. Create a new class and inherit it from the System.Windows.Forms.WebBrowser class

3. Create a new constructor for this class and place in the following code

this.Navigate("about:blank");

this.Document.OpenNew(false);

this.Document.Write("<html><body contentEditable='true'></body></html>");

doc = (mshtml.IHTMLDocument2)this.Document.DomDocument;

doc.designMode = "On";

4. Drop the class, from the toolbox (at the very top), onto a form and run your project. This will give you an editable web browser control.

5. You can change the Bold / Italic etc by calling commands against the document... the code below will turn the selected text in the web browser bold.

public void SetSelectedTextToBold()

{

this.Document.ExecCommand("Bold", false, null);

}

Here is a list of commands you can use

http://msdn2.microsoft.com/en-us/library/ms536977.aspx

The DocumentText property will return the HTML of the document.






Re: Visual C# Express Edition RichTextBox Html

programmer01

EDIT:

Ah ok I think see what you mean should I made the constructor public

EDIT2:

Here is the code I added to a new class:

Code Block

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Drawing;

using System.Data;

using System.Text;

using System.Windows.Forms;

using System.Runtime.InteropServices;

namespace WebBrowserEx

{

[Description("Provides a user control that allows the user to edit HTML page."), ClassInterface(ClassInterfaceType.AutoDispatch)]

public class Class1 : System.Windows.Forms.WebBrowser

{

public WebBrowserD()

{

this.Navigate("about:blank");

this.Document.OpenNew(false);

this.Document.Write("<html><body contentEditable='true'></body></html>");

doc = (mshtml.IHTMLDocument2)this.Document.DomDocument;

doc.designMode = "On";

}

}

}

There is a problem with this code:

1. where I say public WebBrowserD() the compiler underlines WebBrowserD() with the red underline saying it is an error and it thinks this:

Method Must have a return type

2. There is no such thing as doc in this, it does not know what doc is(the last 2 lines before the 3 } at the end)

what should I do to fix this

I am supposed to make a UserControl with a WebBrowser in it and then add this code Or is it supposed to start as an empty class file like I did here and thats all

oh and this is all in a New Class(.DLL) project I made

Thanks,

Programmer01






Re: Visual C# Express Edition RichTextBox Html

programmer01

Ok I fixed some parts but there are still 2 errors and questions I have:

Here is the new code I added into the new class file I made:

Code Block

class WebBrowserDEx : WebBrowser

{

public WebBrowserDEx()

{

this.Navigate("about:blank");

this.Document.OpenNew(false);

this.Document.Write("");

doc = (mshtml.IHTMLDocument2)this.Document.DomDocument;

doc.designMode = "On";

}

}

Now there are no errors with the constructor, but there are 2 errors with doc(I highlighted them both red)

It looks like they are a variable Where and what kind of variable should I make them

And my other question is:

Should I leave this a plain Class File with this code in it or is it supposed to be a UserControl wiht a WebBrowser in it and this code goes in the UserControl code

Thanks,

Programmer01

Edit:

I think I kinda fixed it...:

webBrowser1.Navigate("about:blank");

webBrowser1.Document.OpenNew(false);

mshtml.IHTMLDocument2 doc = (mshtml.IHTMLDocument2)webBrowser1.Document.DomDocument;

doc = (mshtml.IHTMLDocument2)webBrowser1.Document.DomDocument;

doc.designMode = "On";

now I have no errors...But there is one anoying thing it always does...When I test this out in a test project I make a tab control and make 2 tabs, add a RichTextBox to the first and a WebBrowser to the first. Once I add this to tabcontrol1 selected index changed:

Code Block

private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)

{

if (tabControl1.SelectedIndex == 0)

{

richTextBox1.Text = webBrowser1.DocumentText;

}

else if (tabControl1.SelectedIndex == 1)

{

webBrowser1.DocumentText = richTextBox1.Text;

}

}

and then run the program to test it, if I dont add any code to the richtextbox and go to the webbrowser I can edit it! thats good, BUT when I go back to the richtextbox it automatically has this:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<HTML><HEAD>

<META http-equiv=Content-Type content="text/html; charset=utf-8">

<META content="MSHTML 6.00.6000.16544" name=GENERATOR></HEAD>

<BODY></BODY></HTML>

I dont want that to be there automatically like that becuase I am going to add a feature that lets the user select what type of HTML and all that they want to use and I will add code somewhere else that adds that, and the other thing about that is in the RichTextBox if I delete one part of the html that was automatically generated then go to the webbrowser and back what I deleted will be back...I want whatever the user does to stay that way I dont want it to come back like it does now...

I think what is doing this is the about:blank part...but if I take that out then the code has errors...also when I go to Design view(webbrowser tab) it pops up this message box asking me if I want to save I dont want it to do that how do I get rid of this

Thanks,

Programmer01






Re: Visual C# Express Edition RichTextBox Html

Derek Smyth

Hi,

I think the half hour editable web browser has some issues. While trying to find the solution to them I came across this.

http://www.itwriting.com/htmleditor/index.php

http://www.itwriting.com/htmleditor/docwiki/doku.php

Perhaps this would be a better web browser to use.






Re: Visual C# Express Edition RichTextBox Html

programmer01

Cool thanks I will test it out.

two questions about it:

I noticed that in one part of it, it has some Dll Imports of:

user32.dll

ole32.dll

kernel32.dll

ulmon.dll

Are these in Windows Vista becuase if a user uses this program on Vista I dont want it having any errors about not finding those dlls...

Also, when I test it in a program and do the richtextbox and this control in a tab control, I cant figure out how to exchange code back and forth from them like:

When going from code view to design view - cant figure out how to have it read the text/html in the richtextbox

When going from design view to code view - cant figure out how to have the richtextbox read the html made in the htmltextbox control...

I have tried a few things but none work.

Thanks,

Programmer01






Re: Visual C# Express Edition RichTextBox Html

Derek Smyth

Hi,

Yeah it will run ok on Vista however it won't run under 64bit. So make sure that you use the configuration manager to change from AnyCPU to x86. This will force it to run under 32bit (it will still work under 64bit with this setting).

this.richTextBox1.Text = this.htmlEditor1.GetDocumentSource();

this.htmlEditor1.LoadDocument(this.richTextBox1.Text);

I've created a very small example application similar to yours and I don't think the HTML Editor is going to return well formatted HTML. Basically it looks like it will change the formatting, i.e. in your RichTextBox your user will type in some HTML that has been written (formatted) one way, and its unlikely the web browser will return the same format back. You might need to force the web browser to work the way you want it too which gives an indication that its not quite the control for the job but I'm struggling to come up with an alternative approach. The idea you had about making the RTF box handle HTML would require a lot of work as you'd need to create an HTML parser that translated HTML to RTF codes. Quite an interesting challenge all the same but it's almost pointless as the web browser will do it for you.






Re: Visual C# Express Edition RichTextBox Html

programmer01

hmm well thanks, it looks like this will get into very complex things. I guess it is up to me mostly now, thanks for all the help it has helped me out a lot with this.

Thanks,

Programmer01