.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> ASP.NET >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Composite Palindrome Checker Control

Posted By:Dhananjay Kumar       Posted Date: December 16, 2009    Points: 25    Category: ASP.NET    URL: http://www.dotnetspark.com  
 

Composite Palindrome Checker Control

Step 1: Creating the Server Control.

The first step is to start a new project with the custom control template and build the control.

To create the custom control

  1. On the File menu, point to New, and then click Project. The New Project dialog box appears. 

  2. In the Project Types pane, choose either Visual Basic Projects or Visual C# Projects. Select Web Control Library in the Templates pane. 



  3. Change the Name to CompositePalindromeControl and click OK. The new project is created, and CompositePalindromeControl opens in the Code Editor. 
  4. Edit the code to change the derivation from WebControl to CompositeControl. 

    public class ServerControl1 : WebControl


    Change this to 

    public class ServerControl1 : CompositeControl
  5. Also implement INamingContainer. This interface is useful for managing unique ids for controlâ?Ts children.

    Code will look like

    public class ServerControl1 : CompositeControl ,INamingContainer
  6. Add the PalindromeFound event like below.

    public event EventHandler PalindromeFound;
  7. Add four member variables â?" a TextBox, a Button, a Label and  a LiteralControl. Code would look like below. 

    public
    event EventHandler PalindromeFound;
    protected TextBox textboxPalindrome;
    protected Button buttonCheckForPalindrome;
    protected LiteralControl literalcontrolPalindromeStatus;
    protected Label labelForTextBox;
  8. Write a method alphanumeric .

        protected string alphanumeric(string str)

        {

            string strTemp = (String)str.Clone();

            if (str != null)

            {

                char[] var = strTemp.ToCharArray();

                int i = 0;

                foreach (char c in var)

                {

                    if (char.IsLetterOrDigit(c))

                    {

                        i++;

                    }

                    else

                    {

                        strTemp = strTemp.Remove(i, 1);

                    }

                }

            }

            return strTemp;

        }

  9. Write a Method checkForPalindrome

    protected bool checkForPalindrome()
    {
        if (this.Text != null)
       
    {
           
    String strControlText = this.Text;
           
    String strTextToUpper = null;
           
    strTextToUpper = Text.ToUpper();
            
    strControlText = this.alphanumeric(strTextToUpper);
           
    char[] rgcReverse = strControlText.ToCharArray();
           
    Array.Reverse(rgcReverse);
            
    String strReverse = new String(rgcReverse);
           
    if (strControlText == strReverse)
            
    {
               
    return true;
            
    }
            else
           {
              
    return false;
          
    }
       
    }
       
    else
       
    {
           
    return false;
       
    }
    }


  10. Add and EventHandler OnCheckPalindrome  to be applied on Button. Button will get added soon. 

    public void OnCheckPalindrome(Object o, System.EventArgs e)

    {

        this.Text = this.textboxPalindrome.Text;

        this.checkForPalindrome();
    }


  11. Now override the CreateChildControl method

        protected override void CreateChildControls()

        {

            //base.CreateChildControls();

            labelForTextBox = new Label();

            labelForTextBox.Text = "Enter a Palindrome";

            this.Controls.Add(labelForTextBox);

            textboxPalindrome = new TextBox();

            this.Controls.Add(textboxPalindrome);

            Controls.Add(new LiteralControl("
    "
    ));

            buttonCheckForPalindrome = new Button();

            buttonCheckForPalindrome.Text = " Click For Palindrome";

            buttonCheckForPalindrome.Click += new EventHandler(OnCheckPalindrome);

            this.Controls.Add(buttonCheckForPalindrome);

            Controls.Add(new LiteralControl("
    "
    ));

            this.tablePalindromes = new Table();

            this.Controls.Add(tablePalindromes);

            this.ChildControlsCreated = true;

        }

    In above code , if a comilation error is coming at the time of building of Project. Do not wory . tablePalindromes is yet to be add in the class. For bravity donâ?Tt try to build project by this step. Wait for few more steps. 

  12. Now to Show the palindrome status whenever Text property is se. so need to modify the Text Property.

    First comment out default property line as follows

    namespace CompositePalindromeControl

    {

        //[DefaultProperty("Text")]

    Then, declare a private string varibale text. Then write property of text as follows . private String text;

        public string Text

        {

            get

            {

                return text;

            }

            set

            {

                text = value;

                if (this.checkForPalindrome())

                {

                    if (PalindromeFound != null)

                    {

                        PalindromeFound(this, EventArgs.Empty);

                    }

                    literalcontrolPalindromeStatus.Text = String.Format("This is a Palindrome
    {0}"
    , text);

                }

                else

                {

                    literalcontrolPalindromeStatus.Text = String.Format("This is not a Palindrome
    {0}"
    , text);

                }

            }

        }

  13. Add namespace System.Collection and declare two varibale as 

        protected Table tablePalindromes; 

        protected ArrayList alPalindromes;

  14. Add a Method  BuildPalindromesTable to build a palindrome table based on the content of the ArrayList.

        protected void BuildPalindromesTable()

        {

            this.alPalindromes = (ArrayList)this.ViewState["palindromes"];

            if (this.alPalindromes != null)

            {

                foreach (string s in this.alPalindromes)

                {

                    TableCell tablecell = new TableCell();

                    tablecell.BorderStyle = BorderStyle.Double;

                    tablecell.BorderWidth = 3;

                    tablecell.Text = s;

                    TableRow tablerow = new TableRow();

                    tablerow.Cells.Add(tablecell);

                    this.tablePalindromes.Rows.Add(tablerow);

                }

            }

        }

  15. Update the text property setter to manage the Table. 

        public string Text

        {

            get

            {

                //String s = (String)ViewState["Text"];

                //return ((s == null) ? "[" + this.ID + "]" : s);

                return text;

            }

            set

            {

                //ViewState["Text"] = value;

                text = value;

                this.alPalindromes = (ArrayList)this.ViewState["palindromes"];

                if (this.alPalindromes == null)

                {

                    this.alPalindromes = new ArrayList();

                }

                if (this.checkForPalindrome())

                {

                    if (PalindromeFound != null)

                    {

                        PalindromeFound(this, EventArgs.Empty);

                    }

                    alPalindromes.Add(text);

                    literalcontrolPalindromeStatus.Text = String.Format("This is a Palindrome
    {0}"
    , text);

                }

 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend