Reader Level:
ARTICLE

Programming Mobile Forms : Palindrome

Posted by Dipal Choksi Articles | Mobile & Embedded August 06, 2001
In this sample, the author explains how to write the palindrome for mobile FORMS.
  • 0
  • 0
  • 16973


Here is a simple Form which checks if the input text is a palindrome or not. This serves as a good introduction to the relatively new world of Programming for Mobile Web Forms using the Mobile Internet ToolKit. Our tools for this small experiment will be our familiar editor - Notepad or any other text editor. Alternatively we can also use the Microsoft Mobile Internet Designer in Microsoft Visual Studio.Net.

Step 1> Directives

Add the directives to the start of the Mobile Page-if you plan to program a lot for Mobile Web Forms, you can expect to see a lot of these lines (as a matter of fact - In every mobile page you create!)


<%@ Page Inherits="System.Web.UI.MobileControls.MobilePage" Language="cs" %>
<%@ Register TagPrefix="mobile"
Namespace
="System.Web.UI.MobileControls"
Assembly
="System.Web.Mobile" %>

Step 2> User Input

Now we add some lines for getting the user input (We have to care about what the user wants-right ?)

<
mobile:Form runat="server" ID="Form1">
<mobile:Label runat="server" ID="Label1">PALINDROME CHECK</mobile:Label>
<mobile:Label runat="server" ID="Label2">Enter the word:</mobile:Label>
<mobile:TextBox runat="server" id="Word" />
<mobile:Command runat="server" OnClick="cmdSubmit_OnClick" Text="Check!"
ID="Command1"/>
</
mobile:Form>
<mobile:Form runat="server" id="Result">
<mobile:Label runat="server" id="lblMessage" />
</
mobile:Form>

This simply adds labels, a text box and a button control to the Mobile page.

When the button is clicked, the function "cmdSubmit_OnClick" is invoked.

Step 3> Function

Let's check out the "juicy" details of cmdSubmit_OnClick-this function will be doing the core of the work for us.

We extract the word entered by the user in the mobile page, reverse it and compare the two; if the two are equal, then we have a palindrome on our hands!

The script evaluates the word and the results are displayed on the Results label on Form2. Note that Form2 is displayed by making it the ActiveForm.

<script runat="server" language="c#">
protected void cmdSubmit_OnClick(Object sender, EventArgs e)
{
try
{
char[] reverse = Word.Text.ToCharArray();
Array.Reverse (reverse);
String strreverse = new String(reverse);
if (strreverse != Word.Text)
lblMessage.Text = Word.Text + " is not a palindrome";
else
lblMessage.Text = Word.Text + " is a palindrome";
}
catch(Exception ex)
{
lblMessage.Text = "Error in operation. Please try again";
}
ActiveForm = Result;
}
</script>

Step 4> How Does It Look ?

You can verify the mobile page on your cell phone or an emulator or your friendly neighborhood Internet Explorer.

Palind1.gif

Figure 1: Input mobile page

Palind2.gif

Figure 2 : Output mobile page

Palind3.gif

Figure 3: Now we know that "palindrome" is not a palindrome.

COMMENT USING

Trending up