 | |
| Figure 9. To check spelling, users first highlight a word and then select the Check Spelling menu item. |
Setting Up the Web Services
After creating the basic menu item functionality, you can set up the code to consume the Web services. When the user highlights a word from the TextBox control and clicks the Check Spelling menu item, the application invokes the
CheckSpelling() method (see
Figure 9):
Private Sub mnuSpelling_Click_1(ByVal sender As _
System.Object, ByVal e As System.EventArgs) _
Handles mnuSpelling.Click
CheckSpelling(TextBox1.SelectedText.ToString)
End Sub
The
CheckSpelling() method calls the Spell Check Web service's
CheckTextBody() method and expects a return result of type DocumentSummary. It then examines the result and displays the suggested spellings using a Context menu. Each menu item in the context menu has an event handler named
menuItem_Click.
 | |
| Figure 10. Users can replace selected words with the suggested spelling returned by the Web service. |
Public Sub CheckSpelling(ByVal str As String)
Dim result As SpellCheckerWS.DocumentSummary
Dim ws As New SpellCheckerWS.check
Dim i As Integer
result = ws.CheckTextBody(str, 0)
If result.MisspelledWordCount = 0 Then
Return
End If
cMenu.MenuItems.Clear()
For i = 0 To _
result.MisspelledWord(0).SuggestionCount - 1
Dim mItem As MenuItem = New MenuItem
mItem.Text = result.MisspelledWord(0). _
Suggestions(i).ToString
cMenu.MenuItems.Add(mItem)
AddHandler mItem.Click, AddressOf menuItem_Click
Next
cMenu.Show(TextBox1, New Point(0, 0))
'TextBox1.MousePosition)
End Sub
When users click an item on the context menu, the
menuItem_Click event handler code replaces the highlighted word in the TextBox control with the text of the context menu item (see
Figure 10):
Protected Sub menuItem_Click(ByVal sender As _
System.Object, ByVal e As System.EventArgs)
TextBox1.SelectedText = CType(sender, MenuItem).Text()
End Sub
| Author Note: An easy way to examine the return type of a Web service is to set a Breakpoint after the Web service has returned a result. Figure 11 shows an active breakpoint in the Visual Studio code editor. |
 | |
| Figure 11. A breakpoint during execution in Visual Studio. |
After the Web service returns the result, you can examine the structure of the return data type using the Debug->Quick Watch
item in Visual Studio (see
Figure 12):
Consuming the Dictionary Web service is similar. Selecting the Check Meaning menu calls the
CheckMeaning() method:
Private Sub mnuMeaning_Click(ByVal sender As _
System.Object, ByVal e As System.EventArgs) _
Handles mnuMeaning.Click
CheckMeaning(TextBox1.SelectedText.ToString)
End Sub
The Dictionary Web service returns a MyResult type. The MyResult class encapsulates the returned word meanings, both single and multiple. The code in the
CheckMeaning() method parses and displays the meanings in a message box (see
Figure 13):
 | |
| Figure 12. Use the debugger to examine the result returned by a Web service method call. |
Public Function CheckMeaning(ByVal word As String) _
As String
Dim ws As New DictionaryWS.dic2
Dim result As DictionaryWS.MyResult
result = ws.GetEEMeaning(word)
Dim i As Integer
Dim resultStr As String
resultStr = result.Trans(0).ToString & " :"
For i = 1 To result.Trans.Length - 1
resultStr += result.Trans(i).ToString & "; "
Next
If result.Trans.Length = 1 Then _
resultStr += "<word not found>"
MsgBox(resultStr, MsgBoxStyle.Information, _
"Meaning for " & word)
End Function
 | |
| Figure 13. Selecting the Check Meaning menu item looks up the selected word and then displays the meaning in a message box. |
Finally, the Translation Web service provides four possible translation modes, which users select from the Translation submenu (see
Figure 14).
The single
TranslationMenu_Click() event handles the click events for all four submenu items:
Private Sub TranslationMenu_Click(ByVal sender As _
System.Object, ByVal e As System.EventArgs) _
Handles mnuEnFr.Click, mnuDeEn.Click, _
mnuEnDe.Click, mnuFrEn.Click
Dim mode As String
Select Case CType(sender, MenuItem).Text
Case "English -> French" : mode = "en_fr"
Case "English -> German" : mode = "en_de"
Case "French -> English" : mode = "fr_en"
Case "German -> English" : mode = "de_en"
End Select
Translate(TextBox1.SelectedText.ToString, mode)
End Sub
The
Translate() method sends the message to be translated and the language mode, and expects a return string representing the translated sentence, which it displays in a message box (see
Figure 15):
 | |
| Figure 14. The application supports four different translation modes. |
Public Function Translate(ByVal str As String, _
ByVal lang As String) As String
Dim ws As New TranslationWS.BabelFishService
Dim result As String
Try
result = ws.BabelFish(lang, str)
MsgBox(str & vbCrLf & vbCrLf & result, _
MsgBoxStyle.Information, _
"Translated Sentence")
Catch e As Exception
MsgBox("Sentence cannot be translated", _
MsgBoxStyle.Critical, "Error")
End Try
End Function
 | |
| Figure 15. When a user selects one of the four translation modes, the application calls the Translation Web service and displays the result in a message box. |