Mobile Development Introduction to Visual Studio Development Rob Miles Department of Computer Science
Introduction The “Secret Encoder” Program >Encodes text entered, based on a simple keyword Will allow us to explore the issues of mobile development >User Input >Output Display >Use of the Menu Buttons >Program termination
Encoder Program Screen contains three areas: >Encryption key entry >Secret message entry >Output display entry When the code button is pressed the key is applied to the message to produce the result The encryption is performed using XOR, so that it is reversible Key: mykeytext In: password Out: xwbqegg231 Code
Starting Development Initially I am going to write the program for a Smartphone Then we will convert the code over and build a Pocket PC version You will see that the conversion is not difficult and centres around the user interface Finally we can use the encoder in a workspace that targets both devices
Encoder Project First we have to add the data entry components for the program The Visual Studio Toolbox holds only the components which are available for the Smartphone display We are going to use Label and TextBox components
Encoder Form I have added four Label and two TextBox components onto the form They have been given sensible names and aligned correctly
Test Layout When the program runs the layout is somewhat different This is a limitation of Visual Studio 2003 Visual Studio 2005 gives a full preview
Field Traversal The Smartphone application begins running with the most recently added TextBox selected first Moving “forwards” through the fields actually moves towards the “oldest” TextBox This is not what the user will want You should add the lowest TextBox first >Or make use of the tool to change the field order
Smartphone Tab Order The Smartphone Tab order dialogue, selected from the View menu allows you to re-arrange the tab order of the fields on the form
Correct Tabs The order on the right is the one required Note that although the labels are not selected by the tab operation, they appear on the order dialogue
Data Entry The present program will run, and the user can move between the two entry boxes and type text into either We now need a way of triggering the encode action
Adding Menu Keys Not all forms may have menu keys You can edit them by selecting the MainMenu item at the bottom of the form designer
Menu Keys Once you have added the keys to the form you can type menu selections onto them by clicking on the “Type Here” item
Adding the Encode Selection The Encode selection can be added by typing the option name over the button Note that I have the option to type above the selection to create multi-option menus
Smartphone Menu The menu key is displayed as shown We now need to bind an event to the key press to perform our encode action
Binding to Menu Actions It is very easy to bind to a key event In the form designer, double click on the menu item If there is no event hander for that item, one is created and you are taken to it in the source file
Encode Action I have created a method to perform the encode action I could have placed this code inside the event hander itself, but it is more flexible to create a method to do the job: private void doEncode () { outputTextLabel.Text = encode ( inTextBox.Text, keyTextBox.Text ) ; } This calls an encode method which actually performs the translation of the text itself
Testing the program The initial version of the encode method simply returns the original text This allows me to test the program and ensure that it works correctly
The Encode method private string encode ( string input, string key ) { char [] keyChars = key.ToCharArray(); int keypos = 0 ; System.Text.StringBuilder result = new System.Text.StringBuilder(); foreach ( char ch in input ) { int cval = ch - 32; int pval = keyChars[keypos] - 32; result.Append((char)((cval ^ pval) + 32)); keypos++; if ( keypos == key.Length ) keypos = 0 ; } return result.ToString() ; }
Secret Encoder The encryption works, but it is a very weak method Note that the space in the input reveals the key character at that position I leave it to you to create a better one!
Exiting the program At the moment there is no way to exit the program on the Smartphone device We can stop it with Visual Studio, but the user will not be able to do that It is very easy to add an exit menu option
Stopping an Application We can use the standard application termination method to stop the program: private void menuItem2_Click(object sender, System.EventArgs e) { Application.Exit(); } This frees off any resources and exits the program cleanly Note that the Windows CE guidelines aren’t keen on you stopping your programs (but I do it anyway)
TextBox Key Entry You may wish to not show the password when you are using the program As with standard Windows Forms, the TextBox component can be set to allow password entry
Debugging The program can be debugged in exactly the same manner as any other Visual Studio application Any exceptions which are thrown are trapped and you are given the option to debug
Running on the PC The application will run on a standard PC Note how the behaviour of the menu has been adapted for a Windows Form It is often useful to be able to run programs on a PC to test them >Particularly if they make use of file input/output
Labels and Bugs There is a bug in this program; some characters are interpreted by the label display component as controls for access keys (even though these have no meaning for Samrtphone!) The key of “cheese” gives the output as shown
TextBox Replacement Using a TextBox set to read only allows the correct text to be displayed The read only property of the text box is used to prevent the user changing it
Pocket PC Version The Pocket PC version is very similar Just about all of the Smartphone code can be transferred directly over to Pocket PC The only issue is that of user input using buttons
Sample Project 01 Smartphone Encoder If you run the program you will find that you can enter and encode text using a key that you supply If the encoded text is entered it is converted back into the original
Sample Project 02 Pocket PC Encoder The Pocket PC version is virtually identical to the Smartphone one The only change is the use of a button to receive the encode command
Developing the Application You may wish to further develop this application Suggested enhancements include >Improved encoding method >Separate encode/decode methods (perhaps using public and private keys)
Sample Project 03 This project combines a Smartphone and a Pocket PC version of the encoder in a single Visual Studio workspace They all share the same encoder behaviour Note the references to the Encoder resource
More Mobile Fun If you want to make use of remote resources you should take a look at Web Services A sample web service is based at MoreThanOneCanPlay.com Details are on the delegate CD