RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Spice Up Your SMS Chat on the Pocket PC : Page 3

Did you know you can change the way your device represents your SMS messages? Learn how to program your messages to appear visually, complete with photos of the participants.

Coding the Main Application
With the user control out of the way, it's time to code the main application created in the previous section of this article.

As we are going to intercept incoming SMS messages and then retrieve the sender's information from Contacts, you need to add the following references (see also Figure 4):

  • Microsoft.WindowsMobile
  • Microsoft.WindowsMobile.Forms
  • Microsoft.WindowsMobile.PocketOutlook
Figure 4. References: Adding references to the needed libraries.

In the code-behind of Form1, import the following namespaces:

Imports Microsoft.WindowsMobile.PocketOutlook
Imports Microsoft.WindowsMobile.PocketOutlook.MessageInterception
Define the following member variables:

Public Class Form1

    '---used for intercepting SMS messages---
    Private msgInterceptor As MessageInterceptor

    '---the SMS sender's phone number---
    Private SenderPhoneNumber As String

    '---used for saving user's own phone number---
    Private Self As String
In the Form1_Load event, create an instance of the MessageInterceptor class so that all incoming SMS messages are intercepted and deleted after firing an event (SMSInterceptor_MessageReceived):

    Private Sub Form1_Load( _
       ByVal sender As System.Object, _
       ByVal e As System.EventArgs) Handles MyBase.Load

        '---create an instance of the MessageInterceptor class---
        msgInterceptor = New MessageInterceptor( _
           InterceptionAction.NotifyAndDelete, True)

        '---set the event handler for the message interceptor
        AddHandler msgInterceptor.MessageReceived, _
           AddressOf SMSInterceptor_MessageReceived

        '---set the user's phone number---
        mnuMyNumber_Click(Nothing, Nothing)
    End Sub
Notice that when the form is loaded, you will be asked to set your phone number (via the My Number menu item). You need to do this so that you will be able to locate your own information in the Contacts and display your photo (if available; if not, your name). Double-click on the My Number menu item and code as follows:

Private Sub mnuMyNumber_Click( _
   ByVal sender As System.Object, _
   ByVal e As System.EventArgs) Handles mnuMyNumber.Click
        Self = InputBox("Please enter your phone number", "My number")
    End Sub
Next, define the SMSInterceptor_MessageReceived event, which is fired when a SMS message is received:

    '---event handler for the MessageReceived event---
    Private Sub SMSInterceptor_MessageReceived( _
       ByVal sender As Object, _
       ByVal e As MessageInterceptorEventArgs)

        '---extract the SMS message received---
        Dim msg As SmsMessage = e.Message

        '---format as "+123456789:Hello there! Howdy!"---
        '---format as "Johnny Lee:Hello there! Howdy!"---
        Dim s As String = msg.From.Name & ":" & msg.Body

        '---update the textbox with the above data---
        Panel1.BeginInvoke(New _
           myDelegate(AddressOf updatePanelControl), _
           New Object() {s})
    End Sub
Here, you need to cast the message received (e.Message) as a SmsMessage object so that you can extract the sender's name/telephone number and the message body. Note that if the sender's number is not found in Contacts, the From.Name property will return the phone number, or else it returns the name of the sender.

With the name of the sender and the message body extracted, concatenate them with the ":" character and then call the myDelegate delegate to invoke the updatePanelControl() subroutine to display the received SMS message:

    '---delegate for updating the two TextBox controls---
    Public Delegate Sub myDelegate(ByVal str As String)

    '---display received SMS data---
    Public Sub updatePanelControl(ByVal str As String)
        '---format as "+123456789:Hello there! Howdy!"---
        '---format as "Johnny Lee:Hello there! Howdy!"---

        '---show the received data in the TextBox---
        Dim fields As String() = str.Split(":")

        '---add the user's message to the Panel---
        AddConversation(fields(0), fields(1), Balloons.Left)
    End Sub
The updatePanelControl() subroutine splits the string received and pass it to the AddConversation() subroutine, which displays the message using the user control developed earlier.

Author's Note: For all SMS messages received, you will display the user control on the left of the Panel control.

Define the AddConversation() subroutine, as shown in Listing 1.

Observe that the AddConversation() subroutine does the following:

  • Look through all the contacts stored in Contacts, trying to match either the mobile telephone number or the name of the user. Interestingly, you should compare the name of the user (as returned by the From.Name property of the SmsMessage object) against the FileAs property in the Contact class).
  • If the user is found in Contacts, his picture is retrieved (if available). Or else his name will be used instead.
  • For messages sent by others (i.e. not yourself), you need to store its phone number so that you can use it for sending replies. For simplicity, I assume that this conversation only involves two parties – you yourself and one sender.
  • If the sender is not found in Contacts, his name will be his/her phone number (as returned by the From.Name property).
  • Finally, the message is displayed using the user control. If the user has a picture, it is then displayed under the user control, or else his name/phone number will be shown instead.
To reply a message, double-click the Reply menu item and code the following:

Private Sub mnuReply_Click( _
   ByVal sender As System.Object, _
   ByVal e As System.EventArgs) Handles mnuReply.Click

        '---send a SMS message to the recipient---
        SendSMS(SenderPhoneNumber, txtToSend.Text)

        '---add the conversation to the panel---
        AddConversation(Self, txtToSend.Text, Balloons.Right)
    End Sub
The above subroutine calls the SendSMS() subroutine to send a SMS message to the specified user. It then adds the SMS message that you have just sent to the Panel control by calling the AddConversation() subroutine.

The SendSMS() subroutine is defined as follows:

    '---send a SMS message to a recipient---
    Private Sub SendSMS( _
       ByVal receiptnumber As String, _
       ByVal message As String)

        '---from the Microsoft.WindowsMobile.PocketOutlook namespace---
        '---compose a new SMS message---
        Dim SMS As New SmsMessage

        '---set the body of the message---
        SMS.Body = message

        '---add the recipient---
        SMS.To.Add(New Recipient(receiptnumber))

        '---send the message---
    End Sub
Finally, if you are sending a SMS message to another user (that is, you are not replying to an existing message), you use the Send To menu item, which is defined as follows:

Private Sub mnuSendTo_Click( _
   ByVal sender As System.Object, _
   ByVal e As System.EventArgs) Handles mnuSendTo.Click

        '---prompts the user for a phone number---
        Dim number As String = _
           InputBox("Please enter a phone number", "Number")

        '---send a SMS message to the recipient---
        SendSMS(number, txtToSend.Text)

        '---add the conversation to the panel---
        AddConversation(Self, txtToSend.Text, Balloons.Right)
    End Sub

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date