The concept: What we will have is two boxes in our forum. A richTextBox and a web browser. We will mainly be using the richTextBox TectChanged handle which will allow us to run code every time the text in the text box is changed. When the text is changed we will write all the text in the text box to a index.html file, then render that index.html in the webBrowser. This will give us a Real Time HTML Editor. So lets get started.
The first thing we need to do is add the richTextBox and webBrowser to the form. You can do this any way you want but i'll post a screen shot of how mine is laid out below.
Now that that is laid out lets start the code. To keep things simple we only need to add one void, to get the one we need select the richTextBox and in the properties pane go to Events. Then look for the event called TextChanged and double click on it, you should be presented with the Form1.cs Source view like the image below.
Now inside of the richTextBox1_TextChanged void were gonna add the code for our Editor.
string current_source = richTextBox1.Text; StreamWriter stream = new StreamWriter("index.html"); stream.Write(current_source); stream.Close();
This is getting the current HTML that has been typed into the richTextBox1.Text, then it writes it to a index.html file.
string place = Directory.GetCurrentDirectory(); string url = place + "/index.html";
Now we find the location of the index.html file, so that we can render it in the webBrowser.
And last but not least we load the HTML from the richTextBox into the webBrowser so it may be displayed on screen. Good luck (=
string current_source = richTextBox1.Text; StreamWriter stream = new StreamWriter("index.html"); stream.Write(current_source); stream.Close(); string place = Directory.GetCurrentDirectory(); string url = place + "/index.html"; webBrowser1.Navigate(url);
Things to expand on:
- Try doing string recognition to help with tags, such as ( When user types "<html>" insert "\n</html>" into HTML )
- Add a FTP option so you can upload your HTML onto a server right from the program when you are done
- Toy with the font in the richTextBox to get a more eye pleasing editor.
I'll go more in depth and post follow up tutorials for adding more options to the editor, and string recognition like i mentioned before.
If you want to see an option added to the editor please comment below and tell me so i can do it in the next tutorial. Thanks for reading XD