by Jason Tyro - GUI Computing
Technically I shouldn't be writing this article, Matthew Gray started the articles on VB6 WebClasses in the last edition of AVDF, so should probably carry on writing them in his own, very individual style. However, here at GUI Central, I've been working on a project and have gone down the WebClass so it has fallen (some might say plummeted) to me to continue the series.
In this article, I'll be looking at using HTML templates from the WebClasses,
We have 3 html templates - 'tLogon', 'tOK' and 'tCancel'. The tLogon template has 2 very cute textboxes with matching buttons on, the tOK has a very pretty Combo-box while the tCancel template has a stunning text box. This application will show how to process basic user input, simple navigation to other screens within the application and working with replacement tags which are an efficient way of sending text to a web browser.
The HTML is as follows - not rocket science!
<html> <title>WebClass Test</title> <body bgcolor="#00ffff"> <h2>Weblass Test Application</h2> <form method="post" action=WebClass1.ASP?WCI=tLogon&WCE=ProcessForm&WCU> <table border="0"> <TBODY> <tr> <td align="right"><strong>User Name :</strong></TD> <td><input name="txtUserName" ></td></TR> <tr> <td align="right"><strong>Password :</strong></td> <td><input type="password" name="txtPassword" ></td> </tr> <tr> <td> </td> <td><input type="submit" name="cmdOK" value=" OK "> <input type="submit" name="cmdCancel" value="Cancel"></td> </tr></TBODY></TABLE></FORM><strong> <WC@MESSAGE> </WC@MESSAGE> </strong> </body> </html>tOK.htm
<html> <title>WebClass Test</title> <body bgcolor="#00ffff"> <h2>Ok was clicked !</h2> <select name="cboComboBox" size="1"> <WC@OKMessage> </WC@OKMessage> </select> </body> </html>tCancel.htm
<html> <title>WebClass Test</title> <body bgcolor="#00ffff"> <h2>Cancel was clicked !</h2> <input size="30" name="txtMessage" <WC@CancelMessage></WC@CancelMessage></td> </body> </html>
The VB Side of things
When you add an HTML template to your designer, VB actually makes a copy of the file that the IDE uses from then on for the application - tOK.htm becomes tOK1.htm, tLogon.htm becomes tLogon1.htm...I think you get the picture.
One interesting thing that happened was when the application was opened within the VB IDE from a particular machine, you could not edit the htm files, VB would come back with an error saying it couldn't find the file. However if you fired up Notepad separately, edited and saved the file, when you came back to the IDE it would automatically prompt the user that the html template has changed and would they like to refresh the designer to reflect these changes...and it would refresh ok !
The following piece if rocket science is how to handle a click event on the tLogon template.
You first need to right click on the Form1 object and select the 'Connect to Custom Event'….now you weren't expecting that one ! The designer will create an event called Event1…..you weren't expecting that! Rename this 'ProcessForm' or whatever.
Your designer should now look like…..
Now the whole point to this is to allow us to write some extremely complex(!) Visual Basic code, which will execute when the user hits the OK or Cancel buttons on the tLogon template. All you need to do is to double-click on the ProcessForm Event to open the code window.
Option Explicit Option Compare Text Dim sUser As String Dim sPassword As String Private Sub tCancel_ProcessTag(ByVal TagName As String, TagContents As String, SendTags As Boolean) TagContents = ProcessTags(TagName, TagContents) SendTags = False End Sub Private Sub tCancel_Respond() tCancel.WriteTemplate End Sub Private Sub tLogon_ProcessForm() Dim sRequest As String Dim sMessage As String sRequest = Request.Form.Item("cmdOK") sUser = Request.Form.Item("txtUserName") sPassword = Request.Form.Item("txtPassword") If sRequest <> "" Then tOk_Respond Else tCancel_Respond End If End Sub Private Sub tLogon_Respond() tLogon.WriteTemplate End Sub Private Sub tOk_ProcessTag(ByVal TagName As String, TagContents As String, SendTags As Boolean) TagContents = ProcessTags(TagName, TagContents) SendTags = False End Sub Private Sub tOk_Respond() tOk.WriteTemplate End Sub Private Sub WebClass_Start() tLogon_Respond End Sub Private Function ProcessTags(TagName As String, ByVal TagContents As String) As String Dim sResult As String Select Case TagName Case "WC@OKMESSAGE" sResult = CreateCombo() Case "WC@CANCELMESSAGE" sResult = "value=""" & sUser & " - " & sPassword & """" End Select ProcessTags = sResult End Function Private Function CreateCombo() As String Dim sComboString As String Dim iCount As Integer Dim vComboItems As Variant ReDim vComboItems(1) vComboItems(0) = sUser vComboItems(1) = sPassword For iCount = 0 To UBound(vComboItems) sComboString = sComboString & "<option value=""" & iCount & """>" & vComboItems(iCount) Next iCount CreateCombo = sComboString End Function
Just paste that little lot into the code and there you go! Obviously, in a real app, you'd probably want to be doing something a bit more substantial to justify the overhead of VB, but I'm sure you get the idea.
In this example we populate the Web Tags included in the templates with the dynamic html code we need to complete the screen. We use a straightforward text box in the form tCancel and a combo box in tOK which requires some additional processing to populate but nevertheless is still very easy.
Visual Basic checks for the HTML templates before it writes it to the browser for the replacement tags. On finding a tag it fires the templates ProcessTag event.
Request.Form.Item allows us to read in the values entered onto the web page by the user whether it is a some text or a click event. Once we gathered the information we want from the user, because we are writing normal Visual Basic code, we can do all our usual crazy coding stuff ie. API calls, DAO/ADO, ODBC the lot!
The great thing about developing a web application within the Visual Basic IDE, is that you can actually run the application in debug mode, set break points, query the immediate window etc. This makes for very stress free development, well almost !
When you first run your application within the IDE, you will be prompted for a name to give the virtual directory that the Personal Web Server will manage for you. This directory allows PWS to run your application without having to deploy it to the Internet. After this initial input you will never be interrogated again.
Making a ASP of myself
When compiling a WebClass, not only does VB create a DLL, it also creates an initialising ASP file which is the default startup page for the application. This file doesn't require any additional editing from the user, in fact Microsoft specifically state that you should not edit this file...sounds good to me!
In this case, as you will already have seen from the HTML above, the file is WebClassTest.ASP. The ASP that Visual Basic creates looks like this….
<% Response.Buffer=True Response.Expires=0 If (VarType(Application("~WC~WebClassManager")) = 0) Then Application.Lock If (VarType(Application("~WC~WebClassManager")) = 0) Then Set Application("~WC~WebClassManager") = Server.CreateObject("WebClassRuntime.WebClassManager") End If Application.UnLock End If Application("~WC~WebClassManager").ProcessNoStateWebClass "Project1.WebClass1", _ Server, _ Application, _ Session, _ Request, _ Response %>
As you can see from the previous code, the WebClass manager, which is used to handle multiple web classes and is created by the server, creates an instance of the WebClass, it is from this point on that your Visual Basic code kicks in. No output to the client browser is sent from the initialising ASP file.
This is all pretty slick, but (it being version one) there are a few gotchas just waiting for the innocent - not all of which we've got solved just yet.
One thing to remember is when you run your app, which fires up IE4, if you debug your code while the app is running, you have to manually maximise the IE4 window to continue running the app, the IDE seems to be unable to set the focus back automatically. This is a small quibble that you quickly get used to when hitting the infamous <F5> !
You might also find that some of the combo boxes you want to fill don;t stay that way for very long, due to what seems to be a minor bug in the HTML generator.
Another major gotcha is in deployment - you've got your killer web app, how do you distribute it?
This is a pretty straightforward process, or would be if it worked ! The idea is that you package up all your files into a CAB file, or series of, then deploy this onto your internet web server. However we couldn't get the deployment part happening, all sorts of weird and wonderful errors occurred when we tried to use the 'deployment' option on the 'Package and Deployment Wizard' that ships with Visual Studio 6. So instead of creating an 'internet package', which you would thought would be the right one to use, we created a 'standard setup package' and manually told the package wizard which HTM and ASP files the web application uses. We then ran the EXE file it created and installed the files onto the web server and Bob's your uncle or aunty, depending on his preference, a fully functioning web application.
The setup installs the following files that the webclass requires to run.
And of course your compiled web application DLL.
The trouble we had deploying the application from an internet package we have yet to resolve. Any comments or hints are most welcome!
The good thing about WebClasses for me is that my exposure to HTML and ASP is very limited. Although I know internet programming is not really difficult, allowing me to write Visual Basic code to control my application makes web development much more manageable and meant in this case that I was much more productive almost immediately. Familiarity may breed contempt, but there's no doubt that it also makes for a more productive developer.
Internet development gurus may well find using WebClasses slow or even downright frustrating, and there seem to be some 'gremlins' like disappearing combo box tags and deployment issues, however for the 'internet virgin developer', like me, it really gives the application a solid basis to develop from.
I personally like the way your whole web application is contained in its entirety and the fact you can debug your code easily - two "features" of the HTML/ASP way of doing things that seem to be notably absent. The whole VB6 WebClass idea works for me and allows me to concentrate on the application's functionality rather then the physical structure of the various files that make up the web app.