dcsimg
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

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

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.


advertisement
Creating the User Control
To display the chat messages in the Panel1 control, you'll use a user control. This user control simply uses a TextBox control and sizes it according to the amount of text to be displayed.

Author's Note: If you are adventurous, you can format this user control as a balloon and spice up its appearance. For simplicity, I am just going to set its background color.

To create a user control, right-click on the project name in Solution Explorer and select Add | User Control…. Add a TextBox control to the default control design surface (see left of Figure 3) and set the properties of the user control and the TextBox control as follows:

Control

Property

Value

UserControl1

BackColor

Desktop

UserControl1

Size

153,107

TextBox1

Multiline

True

TextBox1

BackColor

Info

TextBox1

Location

1,1

TextBox1

Size

151,105

Figure 3. The User Control: Before and after modifications.

The user control will now look like that as shown on the right of Figure 3. Let's now code the user control. Switching to the code-behind of UserControl1, first define an enumeration:


Public Enum Balloons
    Left
    Right
End Enum
This enumeration allows the user of this user control to indicate its background color (more on this later).

In the UserControl1 class, declare the following two functions from the coredll.dll library:


Public Class UserControl1

    <Runtime.InteropServices.DllImport("coredll.dll")> _
Shared Function SendMessage( _
   ByVal hwnd As IntPtr, _
   ByVal msg As Integer, _
   ByVal wParam As Integer, _
   ByVal lParam As Integer) As Integer
    End Function

    <Runtime.InteropServices.DllImport("coredll.dll")> _
    Shared Function GetCapture() As IntPtr
    End Function
In the .NET Compact Framework (applies to the .NET Framework as well), there is no way you can find out how many lines a string takes up in a TextBox control, and the only way to do that is to use P/Invoke and call the native APIs. And this is the reason for declaring the two functions above.

Next, define the following member variables and constant:


    Private _balloonText As String
    Private _balloonType As Balloons

    Private Const EM_GETLINECOUNT = &HBA
    Private hnd As IntPtr
Define the Balloons property, which allows the user to set the background color of the user control:

    Property Balloons() As Balloons
        Get
            Return _balloonType
        End Get

        Set(ByVal value As Balloons)
            _balloonType = value

            If _balloonType = SMSChat.Balloons.Left Then
                TextBox1.BackColor = Color.LightYellow
            Else
                TextBox1.BackColor = Color.LightSalmon
            End If
        End Set
    End Property
All balloons that are displayed on the left have light yellow background color. All those on the right will have light salmon background color.

Finally, define the BalloonText property, which allows user to set the TextBox control with a string:


    Public Property BalloonText()
        Get
            Return _balloonText
        End Get

        Set(ByVal value)
            _balloonText = value

            '---display the text in the TextBox control---
            TextBox1.Text = _balloonText

            '---obtain the number of lines used---
            TextBox1.Capture = True
            hnd = GetCapture()
            TextBox1.Capture = False
            Dim linecount = SendMessage(hnd, EM_GETLINECOUNT, 0, 0)

            '---adjust the height of the TextBox control---
            TextBox1.Height = linecount * 18

            '---adjust the height of the user control---
            Me.Height = TextBox1.Height + 2
        End Set
    End Property
Note that after setting the TextBox control with the specified string, the height of the TextBox and user controls is adjusted appropriately.



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