All of the examples so far have been command-line programs that wrote to the system console. Now that you have seen the entire development process, let's rewrite our client application to use the new Windows-based WinForms library, which is also available to all NGWS runtime compatible languages. In our example, we'll use Visual Basic and here's the full source code listing:
Listing 10. WinForms Client in VB (Client.cls)
Imports System Imports System.Collections Imports System.WinForms Imports CompCS Imports CompVB Imports CompVC Option Explicit Public Module modmain Public Const vbCrLf = CChar(13) & CChar(10) Public Class Client Inherits Form 'Required by the WinForms Designer Private components As System.ComponentModel.Container Private Button2 As System.WinForms.Button Private Button1 As System.WinForms.Button Private Label1 As System.WinForms.Label Sub New() MyBase.New InitForm ' Required by WinForms Designer. End Sub 'Client form overrides dispose to clean up component list. Overrides Public Sub Dispose() MyBase.Dispose components.Dispose End Sub 'The main entry point for the application Shared Sub Main() Application.Run(New Client) End Sub 'NOTE: This procedure required by the WinForms Designer 'It can be modified using the WinForms Designer. 'Do not modify it using the code editor. Private Sub InitForm() Me.components = New System.ComponentModel.Container Me.Button1 = New Button Me.Button2 = New Button Me.Label1 = New Label Button1.SetLocation(200, 248) Button1.TabIndex = 1 Button1.Text = "&Close" Button1.SetSize(75, 23) Button1.AddOnClick(New EventHandler (AddressOf Me.Button1_Click)) Button2.SetLocation(120, 248) Button2.TabIndex = 2 Button2.Text = "&Execute" Button2.SetSize(75, 23) Button2.AddOnClick(New EventHandler (AddressOf Me.Button2_Click)) Label1.SetLocation(8, 8) Label1.TabIndex = 0 Label1.TabStop = False Label1.Text = "" Label1.SetSize(272, 232) Me.Text = "Client" Me.Controls.Add(Button2) Me.Controls.Add(Button1) Me.Controls.Add(Label1) End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Me.Close ' End Application End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) ' Local Variables Dim myCompCS As New CompCS.StringComponent Dim myCompVB As New CompVB.StringComponent Dim myCompVC As New CompVC.StringComponent Dim StringCount As Integer ' Clear Label Label1.Text = "" ' Display results from C# Component For StringCount = 0 To CInt(myCompCS.Count) - 1 Label1.Text = Label1.Text & MyCompCS.GetString(StringCount) & vbCrLf Next Label1.Text = Label1.Text '& vbCrLf ' Display results from Visual Basic Component For StringCount = 0 to CInt(MyCompVB.Count) - 1 Label1.Text = Label1.Text & myCompVB.GetString(StringCount) & vbCrLf Next Label1.Text = Label1.Text '& vbCrLf ' Display results from Visual C++ Component For StringCount = 0 To CInt(myCompVC.Count) - 1 Label1.Text = Label1.Text & myCompVC.GetString(StringCount) & vbCrLf Next End Sub End Class End Module
In the PDC Tech Preview of the NGWS SDK, the WinForms library is located in the System.WinForms namespace, in particular:
Imports System.WinForms
By importing the namespaces, we can then refer to an included type – like Button
– without having to fully qualify the type name
This next interesting line of code illustrates inheritance, one of the most powerful features of the NGWS runtime:
Inherits Form
With this one statement, we specify that our Client class inherits all of the functionality in the Form
class in the WinForms library. Language independence is an important aspect of the NGWS runtime's inheritance model: not only can we inherit from the runtime, we can inherit from classes written in any NGWS runtime compatible language.
Next we declare the object types that we'll be using on our form, such as:
Private Button1 As System.WinForms.Button
Now we're finally read to execute some code. Here's the constructor for the Client
form, which creates an instance of the base class and then calls the InitForm
method:
Sub New() MyBase.New InitForm ' Required by WinForms Designer. End Sub
And here's the entry point for the program itself, which starts everything off by creating a new instance of the Client
form:
Shared Sub Main() Application.Run(New Client) End Sub
The InitForm
method sets up the form and all of its controls. For Button1
, for example, we create a new button from the Button type:
Me.Button1 = New Button
We then move it, set it's caption (or Text
property), and then resize it:
Button1.SetLocation(200, 248) Button1.TabIndex = 1 Button1.Text = "&Close" Button1.SetSize(75, 23)
Then comes the tricky part: hooking up Click
, just one of the Button
type's many events, to our own subroutine:
Button1.AddOnClick(New EventHandler (AddressOf Me.Button1_Click))
Finally, we add the button to the form's Controls collection:
Me.Controls.Add Button1
The following code highlights the event subroutine that executes when the user clicks on Button1:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Me.Close ' End Application End Sub
Actually, the only thing that happens here is that the form's Close
method is called, thus ending the application. In this particular subroutine we are ignoring the arguments.
The real meat of the program, which uses the same code we saw in the VB client example, is located in the Button2_Click
event. Instead of writing to the Console, however, the WinForms sample adds to the Text
property of the label on the form:
Label1.Text = Label1.Text & myCompVC.GetString(StringCount) & vbCrLf
Running the application creates the following dialog. When the "Execute" button is clicked, the strings are written to the label on the surface of the form: