Windows Forms: How to make a WhatsApp Messenger in VB.NET

By FoxLearn 6/1/2017 9:37:53 PM   29.11K
How to convert from C# to VB.Net and How to make a WhatsApp Messenger in Visual Basic .Net

Step 1Click New Project, then select Visual Basic on the left, then Windows and then select Windows Forms Application. Name your project "WhatsAppVB.Net" and then click OK

whatsapp vb.netStep 2: Download WhatsApp APIThis is an API written in C# but it can be used in any .NET language. It's a fork from WhatsAPINet, which is based on Chat API

Step 3Design your form as below

whatsapp vb.net

Step 4: Add a click event handler to the button

Imports System.Threading
Imports WhatsAppApi
Imports WhatsAppApi.ApiBase
Imports WhatsAppApi.Helper

Public Class Form1
    Private wa As WhatsApp
    Private Delegate Sub UpdateTextBox(ByVal textbox As TextBox, ByVal value As String)
    Public Sub UpdateDataTextBox(ByVal textbox As TextBox, ByVal value As String)
        Dim box As TextBox = textbox
        box.Text = (box.Text & value)
    End Sub

    Private Sub btnSend_Click(sender As Object, e As EventArgs) Handles btnSend.Click
        If ((Not String.IsNullOrEmpty(Me.txtMessage.Text) AndAlso (Not Me.wa Is Nothing)) AndAlso (Me.wa.ConnectionStatus = CONNECTION_STATUS.LOGGEDIN)) Then
            Me.wa.SendMessage(Me.txtTo.Text, Me.txtMessage.Text)
            Dim txtStatus As TextBox = Me.txtStatus
            txtStatus.Text = (txtStatus.Text & String.Format(Environment.NewLine & "{0}:{1}", txtName.Text, Me.txtMessage.Text))
            Me.txtMessage.Clear()
            Me.txtMessage.Focus()
        End If
    End Sub

    Private Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click
        Dim thread = New Thread(AddressOf ThreadTask)
        thread.IsBackground = True
        thread.Start()
    End Sub

    Private Sub ThreadTask()
        Dim textbox As UpdateTextBox = New UpdateTextBox(AddressOf Me.UpdateDataTextBox)
        Me.wa = New WhatsApp(Me.txtPhoneNumber.Text, Me.txtPassword.Text, Me.txtName.Text, True, False)
        AddHandler Me.wa.OnConnectSuccess, Sub()
                                               If Me.txtStatus.InvokeRequired Then
                                                   Dim objArray1 As Object() = New Object() {Me.txtStatus, "Connected..."}
                                                   Me.Invoke(textbox, objArray1)
                                               End If
                                               AddHandler Me.wa.OnLoginSuccess, Sub(ByVal phone As String, ByVal data As Byte())
                                                                                    If Me.txtStatus.InvokeRequired Then
                                                                                        Dim args As Object() = New Object() {Me.txtStatus, Environment.NewLine & "Login success !"}
                                                                                        Me.Invoke(textbox, args)
                                                                                    End If
                                                                                    While Me.wa IsNot Nothing
                                                                                        Me.wa.PollMessages()
                                                                                        Thread.Sleep(200)
                                                                                        Continue While
                                                                                    End While
                                                                                End Sub

                                               AddHandler Me.wa.OnLoginFailed, Sub(ByVal data As String)
                                                                                   If Me.txtStatus.InvokeRequired Then
                                                                                       Dim args As Object() = New Object() {Me.txtStatus, String.Format(Environment.NewLine & "Login failed: {0}", data)}
                                                                                       Me.Invoke(textbox, args)
                                                                                   End If
                                                                               End Sub

                                               AddHandler Me.wa.OnGetMessage, Sub(ByVal node As ProtocolTreeNode, ByVal from As String, ByVal id As String, ByVal name As String, ByVal message As String, ByVal receipt_sent As Boolean)
                                                                                  If Me.txtStatus.InvokeRequired Then
                                                                                      Dim args As Object() = New Object() {Me.txtStatus, String.Format(Environment.NewLine & "{0}:{1}", name, message)}
                                                                                      Me.Invoke(textbox, args)
                                                                                  End If
                                                                              End Sub
                                               Me.wa.Login(Nothing)
                                           End Sub
        AddHandler Me.wa.OnConnectFailed, Sub(ByVal ex As Exception)
                                              If Me.txtStatus.InvokeRequired Then
                                                  Dim args As Object() = New Object() {Me.txtStatus, String.Format(Environment.NewLine & "Connect failed: {0}", ex.StackTrace)}
                                                  Me.Invoke(textbox, args)
                                              End If
                                          End Sub
        Me.wa.Connect()
    End Sub
End Class

VIDEO TUTORIALS