Adit Cookbook Pages

Home
Cookbook
A Question of Sorts
Bits & Bobs
Error Event Handling
SQL for Access
Grid Copy
Temporary Files
Delete or Kill
Credit Card
Virtual Arrays
Binary Chop
Numbers
VB to HTML
Viewport
Printing Grids

VB to HTML

Converting VB code to HTML

One of the difficulties we have faced in developing these cookbook pages has been in
displaying the VB code properly formatted and indented. One of our team mentioned they
had noticed a  web site that contained a piece of code for converting the text of a VB routine
to HTML  but had only hazy memory of what it could do. So we knocked up the some code to
do the basics. You can download it from
here as a zip file.
We are including this code on our pages to encourage others to publish useful routines and
clever tricks on the web. If you add anything useful to this code or build a better  version then
please share it with us and, through these pages, with the wider developer community.

The cookbook bit - well the main routine carries out a sequence of substitutions to  replace
the bits of VB code that are likely to be confusing as HTML - &, <, >  and ". You can see if it
works OK by checking out the layout below:

Private Function ConvertCode(ByVal SourceCode As String) As String
    Dim HTMLString As String, ThisLine As String
    Dim EndOfLine As Integer, CharLoop As Integer
    Dim ThisChar As String
    
    HTMLString = "<TT><PRE>"
    Do Until Len(SourceCode) < 1
        EndOfLine = InStr(SourceCode, vbCrLf)
        If EndOfLine > 0 Then
            ThisLine = Left$(SourceCode, (EndOfLine - 1))
            SourceCode = Mid$(SourceCode, (EndOfLine + 2))
        Else
            ThisLine = SourceCode
            SourceCode = ""
        End If
        Now process the current line - making substitutions        
        The order of substitution has some importance so watch changes or additions
        ThisLine = SwapString(ThisLine, "&amp;", "&")
        ThisLine = SwapString(ThisLine, "&lt;", "<")
        ThisLine = SwapString(ThisLine, "&gt;", ">")
        ThisLine = SwapString(ThisLine, "&nbsp;", " ")
        spot a comment section and make it green or return unchanged
        ThisLine = MakeComment(ThisLine)
        Then clear up the quote marks
        ThisLine = SwapString(ThisLine, "&quot;", """")
        and add it to the HTML output
        HTMLString = HTMLString & ThisLine & vbCrLf
    Loop    HTMLString = HTMLString & "</PRE></TT>"
    ConvertCode = HTMLString    
    The tutorial bit!    
    We pass the arguments around By Value as 
       1. We should be, after all this is VB6    
       2  It means we can muck about with the    
          strings without upsetting the source data
End Function
If you download the source code you will find the following two routines included on 
the test form

Context Sensitive Menu's

If you wondered how to make your Edit Menu commands a bit more "context 
sensitive" then there are some ideas for you to try below:

Private Sub mnuEdit_Click()
    Might as well provide some context sensitivity for
    the edit functions even though the system generated    
    menu on right click does it for free on text boxes 
    If TypeOf Screen.ActiveControl Is TextBox Then
        If Len(Screen.ActiveControl.SelText) > 0 Then
            mnuEditOption(0).Enabled = True
            mnuEditOption(1).Enabled = True
            mnuEditOption(3).Enabled = True
        Else
            mnuEditOption(0).Enabled = False
            mnuEditOption(1).Enabled = False
            mnuEditOption(3).Enabled = False
        End If
        If Clipboard.GetFormat(vbCFText) Then
            mnuEditOption(2).Enabled = True
        Else
            mnuEditOption(2).Enabled = False
        End If
    Else
        mnuEditOption(0)Enabled = False
        mnuEditOption(1).Enabled = False
        mnuEditOption(2).Enabled = False
        mnuEditOption(3).Enabled = False
    End If
End Sub
Private Sub mnuEditOption_Click(Index As Integer)
    Select Case Index
        Case 0  ’Cut
            Clipboard.Clear
            Clipboard.SetText (Screen.ActiveControl.SelText)
            Screen.ActiveControl.SelText = ""
        Case 1  ’Copy
            Clipboard.Clear
            Clipboard.SetText (Screen.ActiveControl.SelText)
        Case 2  ’Paste
            Screen.ActiveControl.SelText = Clipboard.GetText
        Case 3  ’Delete
            Screen.ActiveControl.SelText = ""
    End Select
End Sub

Google
 
Web www.adit.co.uk
www.aditsite.co.uk