Visual Basic 2005 Guide
Developing The Application
It goes without saying that this page follows on from the previous. If you haven't followed all of the advice on that page, go back now and do it. I don't want any arguing about it - you know I'm right.
Step 1 - A New Menu
Return to the MDI parent form, frmMain. Right mouse click on the View menu item on your form. Choose to insert a new menu item. Check the properties window for this item and change the Name property to InsertMenu. Change the Text property to &Insert. The ampersand is how you get the ALT keyboard shortcut that you expect with application menus.
On your new menu, add the menu item, HTML Tags. Double click on the new menu item and add the following code in the event handler,
If ActiveMdiChild Is Nothing Then Exit Sub
Dim theBox As New RichTextBox
theBox = ActiveMdiChild.Controls(0)
theBox.SelectedText = "<html>" & vbNewLine & "<head>"& vbNewLine & "<title></title>" & vbNewLine & "</head>"
theBox.SelectedText += vbNewLine & "<body>"& vbNewLine & "</body>" & vbNewLine & "</html>"
Test out this code. You should find a load of HTML tags on the active document. Look carefully at the first two lines which give us a reference to text box on the active child window.
Step 2 - An Insert Tag Function
For a great many HTML tags we want to be able to highlight some text in the active window and have the tags placed around the text that we have highlighted. The procedure changes little from one HTML tag to the next. The following procedure can be used to enter a simple HTML tag.
Private Sub addTag(ByVal myTag As String)
Dim theBox As New RichTextBox
theBox = ActiveMdiChild.Controls(0)
Dim strTemp As String
strTemp = theBox.SelectedText
strTemp = "<" + myTag + ">" + strTemp + "</" + myTag + ">"
theBox.SelectedText = strTemp
theBox.Focus()
End Sub
All we need to do now is call this procedure with the appropriate parameter to create different tags. Start the process by creating a new menu item on the Insert Menu with the text Heading. Add 6 sub items to this menu, called Heading 1, Heading 2 up to Heading 6.
Double click on the Heading 1 menu item. In the event handler sub routine, add the following code,
addtag("h1")
Do the same for the other heading menu items, remembering to change the number to match the level of the heading.
Step 3 - Adding Loads Of Menu Commands
Using the model shown above, you should be able to create menu or toolbar items that create a whole range of other tags. Take the application on another stage by adding a range of features for adding tags. The table below shows some of the HTML tags that you could create using this procedure.
Procedure call | Inserts | HTML Effect |
---|---|---|
addtag("b") | <b>selected text</b> | bold |
addtag("body") | <body>selected text</body> | body |
addtag("center") | <center>selected text</center> | centre text |
addtag("head") | <head>selected text</head> | head |
addtag("i") | <i>selected text</i> | italics |
addtag("li") | <li>selected text</li> | list item |
addtag("ol") | <ol>selected text</ol> | ordered list (numbers or letters) |
addtag("p") | <p>selected text</p> | paragraph |
addtag("s") | <s>selected text</s> | strikethrough |
addtag("sub") | <sub>selected text</sup> | subscript |
addtag("sup") | <sup>selected text</sup> | superscript |
addtag("table") | <table>selected text</table> | table |
addtag("td") | <td>selected text</td> | table cell |
addtag("title") | <title>selected text</title> | window title |
addtag("tr") | <tr>selected text</tr> | table row |
addtag("u") | <u>selected text</u> | underline |
addtag("ul") | <ul>selected text</ul> | unordered list (bullets) |