Graphical User Interface (GUI) Applications

GUI applications are known as Windows applications in Visual Studio .NET. The big advantage of a GUI is that you can show much more data to the user at once. In addition to text, you can harness the full power of graphics to compartmentalize data or translate that information into whatever makes the most sense. By now, most developers are familiar with the advantages of a GUI—easy menu navigation, event-driven programming, richer user interaction models, and so on. Because all of these advantages exist, it only makes sense to be able to access Web Service here as well. This time around, we will access the FirstService.GetRandomNumbers Web Method. To begin, we must first create a Windows Application project. Perform the following steps to create the Windows application:

1.
Select File, Project, New Project.

2.
Select the following items in the New Project dialog:

a. Project Type: Visual Basic Projects

b. Templates: Windows Application

c. Name: Chapter2GUI

This time around we have the skeleton for a GUI application. The IDE displays a blank form, Form1. On this form, we want to place controls for the following items:

  • A place for the user to enter the number of random numbers he or she wants.

  • A place for the user to enter the number of minimum value of the random numbers.

  • A place for the user to enter the number of maximum value of the random numbers.

  • A place to display the results to the user.

Figure 2.7 shows my proposed layout of the form.

Figure 2.7. Layout of the Chapter2GUI main form.


After placing the controls on the form, I changed their names to make their data association clear. Our next step is to generate the proxy for the FirstService Web Service. Add a Web reference to http://localhost/Chapter1/Chapter1.vsdisco and rename the namespace from localhost to Chapter1. With this, we are almost done. We just have to write the code that executes when the Get Values button is clicked. The fastest and easiest way to do this is to double-click the Get Values button in the form designer. This will create the skeleton for the click event handler and bring up the code view.

Within the code, we want to make sure that all of the client input is valid. If all of the input is valid, we will call the Web Service and display the output. Listing 2.3 shows the full source for click handler:

Listing 2.3. btnGetValues Click Event Handler
Private Sub btnGetValues_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnGetValues.Click

    Dim minValue As Long
    Dim maxValue As Long
    Dim numElements As Long
    Dim elements() As Long
    Dim rndValue As Long

    'Read in the user data and validate the input

    ' Validate the number of elements value
    If (Me.txtNumElements.Text.Length = 0) Then
        MsgBox("You must enter the number of elements")
        Me.txtNumElements.Select()
        Exit Sub
    ElseIf Not (IsNumeric(Me.txtNumElements.Text)) Then
        MsgBox("The number of elements must be a number")
        Me.txtNumElements.Select()
        Exit Sub
    End If

    ' Validate the minimum value
    If (Me.txtMinValue.Text.Length = 0) Then
        MsgBox("You must enter a minimum value")
        Me.txtMinValue.Select()
        Exit Sub
    ElseIf Not (IsNumeric(Me.txtMinValue.Text)) Then
        MsgBox("The minimum value must be a number")
        Me.txtMinValue.Select()
        Exit Sub
    End If

    ' Validate the maximum value
    If (Me.txtMaxValue.Text.Length = 0) Then
        MsgBox("You must enter a maximum value")
        Me.txtMaxValue.Select()
        Exit Sub
    ElseIf Not (IsNumeric(Me.txtMaxValue.Text)) Then
        MsgBox("The maximum value must be a number")
        Me.txtMaxValue.Select()
        Exit Sub
    End If

    ' Read in the values since they are all numbers
    numElements = System.Convert.ToInt32(Me.txtNumElements.Text)
    minValue = System.Convert.ToInt32(Me.txtMinValue.Text)
    maxValue = System.Convert.ToInt32(Me.txtMaxValue.Text)

    ' Now for a little more validation

    ' Make sure that the number of elements is a positive number
    If (numElements < 1) Then
        MsgBox("The number of elements must be greater than 0")
        Me.txtNumElements.Select()
        Exit Sub
    End If
    ' Make sure that the minimum value is less than the maximum
    ' value.
    If (minValue >= maxValue) Then
        MsgBox("The minimum value must be less than the" & _
            " maximum value")
        Me.txtMinValue.Select()
        Exit Sub
    End If

    ' Everything must be valid. Call the web service
    Dim svc As New Chapter1.FirstService()
    Try
        elements = svc.GetRandomNumbers(numElements, _
            minValue, maxValue)
        If (elements.Length > 0) Then

            ' Clear out the listbox
            Me.lstRetrievedValues.Items.Clear()

            'Add the elements to the list box
            For Each rndValue In elements
                Me.lstRetrievedValues.Items.Add(rndValue.ToString())
            Next

        End If
    Catch ex As Exception
        MsgBox(ex.ToString())
    Finally
        svc.Dispose()
    End Try

End Sub
					

Most of the code in here is fairly straightforward. It checks what the user typed in for the various values and lets him or her know if anything is wrong. The Web Service-specific code does not occur until the last few lines of code.

The first thing you might notice is that the call to GetRandomNumbers is enclosed in a Try...Catch...Finally block. This is a brand new construct in Visual Basic .NET and is meant to supercede the On Error Goto syntax of its predecessors. All calls to Web Services can throw exceptions. Sometimes, it will be because of a connection failure, bad input, a SOAP Fault generated by the server, or some other item. Because an exception might leave the proxy with an open connection, it is very important to wrap the SOAP call in a Try...Catch...Finally block. The Finally part of the block should call the proxy's Dispose function. Doing so will release any resources and close the connection to the server. You will note that we did not do this in the CLI example. Now might be a good time to go back to that example and properly handle the call to the Web Service.

With this code in place, let's experiment with the application itself. Build the application (go to the Build menu and select Build). Then, select Debug, Start. With the application running, experiment and see how it works. Figure 2.8 shows the results of one run on my machine.

Figure 2.8. Chapter2GUI at work.


Before going on to the next example, we will also look at what the SOAP message exchange looked like. You may want to compare this with the request and response done with the HTTP/GET Web request listed in Chapter 1. The differences are minimal at best.

Request:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope
  xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Body>
    <GetRandomNumbers xmlns="http://tempuri.org/">
      <arraySize>10</arraySize>
      <min>1</min>
      <max>10</max>
    </GetRandomNumbers>
  </soap:Body>
</soap:Envelope>

Response:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope
  xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Body>
    <GetRandomNumbersResponse xmlns="http://tempuri.org/">
      <GetRandomNumbersResult>
        <long>6</long>
        <long>10</long>
        <long>9</long>
        <long>7</long>
        <long>8</long>
        <long>1</long>
        <long>10</long>
        <long>10</long>
        <long>6</long>
        <long>2</long>
      </GetRandomNumbersResult>
    </GetRandomNumbersResponse>
  </soap:Body>
</soap:Envelope>

Enough work on the desktop. Let's try out some Web-based access next.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.171.107