Click here to Skip to main content
15,891,136 members
Articles / Programming Languages / Visual Basic

WebResourceProvider VB.NET style

Rate me:
Please Sign up or sign in to vote.
4.50/5 (2 votes)
30 Aug 20023 min read 82.6K   263   24  
Ravi Bhavnani's WebResourceProvider ported to the .NET Framework
Public Class GetTranslation
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
    Friend WithEvents SourceText As System.Windows.Forms.TextBox
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents TranslatedText As System.Windows.Forms.TextBox
    Friend WithEvents Label3 As System.Windows.Forms.Label
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents Button2 As System.Windows.Forms.Button
    Friend WithEvents ReverseTranslatedText As System.Windows.Forms.TextBox
    Friend WithEvents RadioItalian As System.Windows.Forms.RadioButton
    Friend WithEvents RadioSpanish As System.Windows.Forms.RadioButton
    Friend WithEvents RadioGerman As System.Windows.Forms.RadioButton
    Friend WithEvents RadioFrench As System.Windows.Forms.RadioButton
    Friend WithEvents StatusBar1 As System.Windows.Forms.StatusBar
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.GroupBox1 = New System.Windows.Forms.GroupBox()
        Me.RadioItalian = New System.Windows.Forms.RadioButton()
        Me.RadioSpanish = New System.Windows.Forms.RadioButton()
        Me.RadioGerman = New System.Windows.Forms.RadioButton()
        Me.RadioFrench = New System.Windows.Forms.RadioButton()
        Me.SourceText = New System.Windows.Forms.TextBox()
        Me.Label1 = New System.Windows.Forms.Label()
        Me.Label2 = New System.Windows.Forms.Label()
        Me.TranslatedText = New System.Windows.Forms.TextBox()
        Me.Label3 = New System.Windows.Forms.Label()
        Me.ReverseTranslatedText = New System.Windows.Forms.TextBox()
        Me.Button1 = New System.Windows.Forms.Button()
        Me.Button2 = New System.Windows.Forms.Button()
        Me.StatusBar1 = New System.Windows.Forms.StatusBar()
        Me.GroupBox1.SuspendLayout()
        Me.SuspendLayout()
        '
        'GroupBox1
        '
        Me.GroupBox1.Controls.AddRange(New System.Windows.Forms.Control() {Me.RadioItalian, Me.RadioSpanish, Me.RadioGerman, Me.RadioFrench})
        Me.GroupBox1.Location = New System.Drawing.Point(8, 8)
        Me.GroupBox1.Name = "GroupBox1"
        Me.GroupBox1.Size = New System.Drawing.Size(376, 48)
        Me.GroupBox1.TabIndex = 0
        Me.GroupBox1.TabStop = False
        Me.GroupBox1.Text = "Translate from english to:"
        '
        'RadioItalian
        '
        Me.RadioItalian.Location = New System.Drawing.Point(284, 16)
        Me.RadioItalian.Name = "RadioItalian"
        Me.RadioItalian.Size = New System.Drawing.Size(68, 24)
        Me.RadioItalian.TabIndex = 3
        Me.RadioItalian.Text = "Italian"
        '
        'RadioSpanish
        '
        Me.RadioSpanish.Location = New System.Drawing.Point(196, 16)
        Me.RadioSpanish.Name = "RadioSpanish"
        Me.RadioSpanish.Size = New System.Drawing.Size(68, 24)
        Me.RadioSpanish.TabIndex = 2
        Me.RadioSpanish.Text = "Spanish"
        '
        'RadioGerman
        '
        Me.RadioGerman.Location = New System.Drawing.Point(108, 16)
        Me.RadioGerman.Name = "RadioGerman"
        Me.RadioGerman.Size = New System.Drawing.Size(68, 24)
        Me.RadioGerman.TabIndex = 1
        Me.RadioGerman.Text = "German"
        '
        'RadioFrench
        '
        Me.RadioFrench.Checked = True
        Me.RadioFrench.Location = New System.Drawing.Point(20, 16)
        Me.RadioFrench.Name = "RadioFrench"
        Me.RadioFrench.Size = New System.Drawing.Size(68, 24)
        Me.RadioFrench.TabIndex = 0
        Me.RadioFrench.TabStop = True
        Me.RadioFrench.Text = "French"
        '
        'SourceText
        '
        Me.SourceText.Location = New System.Drawing.Point(8, 80)
        Me.SourceText.Multiline = True
        Me.SourceText.Name = "SourceText"
        Me.SourceText.Size = New System.Drawing.Size(376, 72)
        Me.SourceText.TabIndex = 1
        Me.SourceText.Text = ""
        '
        'Label1
        '
        Me.Label1.Location = New System.Drawing.Point(8, 64)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(136, 16)
        Me.Label1.TabIndex = 2
        Me.Label1.Text = "English text:"
        '
        'Label2
        '
        Me.Label2.Location = New System.Drawing.Point(8, 160)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(136, 16)
        Me.Label2.TabIndex = 2
        Me.Label2.Text = "Translation:"
        '
        'TranslatedText
        '
        Me.TranslatedText.Location = New System.Drawing.Point(8, 176)
        Me.TranslatedText.Multiline = True
        Me.TranslatedText.Name = "TranslatedText"
        Me.TranslatedText.ReadOnly = True
        Me.TranslatedText.Size = New System.Drawing.Size(376, 72)
        Me.TranslatedText.TabIndex = 1
        Me.TranslatedText.Text = ""
        '
        'Label3
        '
        Me.Label3.Location = New System.Drawing.Point(8, 256)
        Me.Label3.Name = "Label3"
        Me.Label3.Size = New System.Drawing.Size(136, 16)
        Me.Label3.TabIndex = 2
        Me.Label3.Text = "Reverse translation:"
        '
        'ReverseTranslatedText
        '
        Me.ReverseTranslatedText.Location = New System.Drawing.Point(8, 272)
        Me.ReverseTranslatedText.Multiline = True
        Me.ReverseTranslatedText.Name = "ReverseTranslatedText"
        Me.ReverseTranslatedText.ReadOnly = True
        Me.ReverseTranslatedText.Size = New System.Drawing.Size(376, 72)
        Me.ReverseTranslatedText.TabIndex = 1
        Me.ReverseTranslatedText.Text = ""
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(304, 352)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(80, 24)
        Me.Button1.TabIndex = 3
        Me.Button1.Text = "Exit"
        '
        'Button2
        '
        Me.Button2.Location = New System.Drawing.Point(216, 352)
        Me.Button2.Name = "Button2"
        Me.Button2.Size = New System.Drawing.Size(80, 24)
        Me.Button2.TabIndex = 3
        Me.Button2.Text = "Translate"
        '
        'StatusBar1
        '
        Me.StatusBar1.Location = New System.Drawing.Point(0, 385)
        Me.StatusBar1.Name = "StatusBar1"
        Me.StatusBar1.Size = New System.Drawing.Size(394, 22)
        Me.StatusBar1.SizingGrip = False
        Me.StatusBar1.TabIndex = 4
        Me.StatusBar1.Text = "Ready"
        '
        'GetTranslation
        '
        Me.AutoScale = False
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(394, 407)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.StatusBar1, Me.Button1, Me.Label1, Me.SourceText, Me.GroupBox1, Me.Label2, Me.TranslatedText, Me.Label3, Me.ReverseTranslatedText, Me.Button2})
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog
        Me.MaximizeBox = False
        Me.MinimizeBox = False
        Me.Name = "GetTranslation"
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        Me.Text = "GetTranslation"
        Me.GroupBox1.ResumeLayout(False)
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.Close()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim myTranslatorProvider As TranslationProvider
        Dim myTranslationMode As TranslationProvider.TranslateModes

        'clean up textboxes
        TranslatedText.Text = ""
        ReverseTranslatedText.Text = ""

        'show what we are about to do
        StatusBar1.Text = "Getting translation from Google..."
        Me.Refresh()

        'get the translation
        If RadioFrench.Checked = True Then
            myTranslationMode = TranslationProvider.TranslateModes.EnglishToFrench
        ElseIf RadioGerman.Checked = True Then
            myTranslationMode = TranslationProvider.TranslateModes.EnglishToGerman
        ElseIf RadioItalian.Checked = True Then
            myTranslationMode = TranslationProvider.TranslateModes.EnglishToItalian
        ElseIf RadioSpanish.Checked = True Then
            myTranslationMode = TranslationProvider.TranslateModes.EnglishToSpanish
        End If
        Try
            myTranslatorProvider = New TranslationProvider(SourceText.Text, myTranslationMode)
            TranslatedText.Text = myTranslatorProvider.TranslationText
        Catch ex As Exception
            MsgBox(GetErrorString(ex), MsgBoxStyle.Critical, "Error while translating")
            TranslatedText.Text = ""
            ReverseTranslatedText.Text = ""
            StatusBar1.Text = "Error"
            Exit Sub
        End Try


        'show what we are about to do
        StatusBar1.Text = "Getting reverse translation from Google..."
        Me.Refresh()

        'get the reversed translation
        If myTranslationMode = TranslationProvider.TranslateModes.EnglishToFrench Then
            myTranslationMode = TranslationProvider.TranslateModes.FrenchToEnglish
        ElseIf myTranslationMode = TranslationProvider.TranslateModes.EnglishToGerman Then
            myTranslationMode = TranslationProvider.TranslateModes.GermanToEnglish
        ElseIf myTranslationMode = TranslationProvider.TranslateModes.EnglishToItalian Then
            myTranslationMode = TranslationProvider.TranslateModes.ItalianToEnglish
        ElseIf myTranslationMode = TranslationProvider.TranslateModes.EnglishToSpanish Then
            myTranslationMode = TranslationProvider.TranslateModes.SpanishToEnglish
        End If

        Try
            With myTranslatorProvider
                .TranslateMode = myTranslationMode
                .SourceText = TranslatedText.Text
                ReverseTranslatedText.Text = .TranslationText
            End With
        Catch ex As Exception
            MsgBox(GetErrorString(ex), MsgBoxStyle.Critical, "Error while translating")
            TranslatedText.Text = ""
            ReverseTranslatedText.Text = ""
            StatusBar1.Text = "Error"
            Exit Sub
        End Try

        StatusBar1.Text = "Ready"

    End Sub

    Private Function GetErrorString(ByVal ex As Exception) As String
        Dim mySB As New System.Text.StringBuilder()
        AddErrorString(mySB, ex)
        Return mySB.ToString

    End Function

    Private Sub AddErrorString(ByRef mySB As System.Text.StringBuilder, ByRef ex As Exception)
        mySB.Append(ex.Message + vbCrLf)
        If Not ex.InnerException Is Nothing Then
            AddErrorString(mySB, ex.InnerException)
        End If
    End Sub

    Private Sub GetTranslation_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        StatusBar1.Text = "Ready"
    End Sub

    Private Sub GetTranslation_Show(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Activated
        SourceText.Focus()
    End Sub
End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Switzerland Switzerland
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions