windows phone 7.1 - Why TextBox property MaxLength is not working for a customised TextBox? -


i wanted password box numeric input scope, unfortunately passwordbox not allow developers specify numeric inputscope. have achieved objective through customize textbox. code here,mainpage.xaml.cs

using system; using system.collections.generic; using system.linq; using system.net; using system.windows; using system.windows.controls; using system.windows.documents; using system.windows.input; using system.windows.media; using system.windows.media.animation; using system.windows.shapes; using microsoft.phone.controls; using system.text.regularexpressions;  namespace paswwordboxwithnumericinput {     public partial class mainpage : phoneapplicationpage     {         // constructor         public mainpage()         {             initializecomponent();          }      string _enteredpasscode = "";     string _passwordchar = "*";      private void passwordtextbox_keyup(object sender, keyeventargs e)     {         //modify new passcode according entered key         _enteredpasscode = getnewpasscode(_enteredpasscode, e.platformkeycode);          //replace text *         passwordtextbox.text = regex.replace(_enteredpasscode, @".", _passwordchar);          //take cursor end of string         textbox t = new textbox();        // passwordtextbox.selectionstart = t.text.length;     }     private string getnewpasscode(string oldpasscode, int keyid)     {         string newpasscode = string.empty;         switch (keyid)         {             case 233:                 newpasscode = oldpasscode;                 break;             case 8:                 //back key pressed                 if (oldpasscode.length > 0)                     newpasscode = oldpasscode.substring(0, oldpasscode.length - 1);                 break;             case 190:                 // . pressed                 newpasscode = oldpasscode;                 break;             default:                 //number pressed                 newpasscode = oldpasscode + (keyid - 48);                 break;         }         return newpasscode;     }      private void button1_click(object sender, routedeventargs e)     {         string str = _enteredpasscode;     } } 

}

and mainpage.xaml here

<phone:phoneapplicationpage      x:class="paswwordboxwithnumericinput.mainpage"     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     xmlns:phone="clr-namespace:microsoft.phone.controls;assembly=microsoft.phone"     xmlns:shell="clr-namespace:microsoft.phone.shell;assembly=microsoft.phone"     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"     mc:ignorable="d" d:designwidth="480" d:designheight="768"     fontfamily="{staticresource phonefontfamilynormal}"     fontsize="{staticresource phonefontsizenormal}"     foreground="{staticresource phoneforegroundbrush}"     supportedorientations="portrait" orientation="portrait"     shell:systemtray.isvisible="true">      <!--layoutroot root grid page content placed-->     <grid x:name="layoutroot" background="transparent">         <grid.rowdefinitions>             <rowdefinition height="auto"/>             <rowdefinition height="*"/>         </grid.rowdefinitions>          <!--titlepanel contains name of application , page title-->         <stackpanel x:name="titlepanel" grid.row="0" margin="12,17,0,28">             <textblock x:name="applicationtitle" text="my application" style="{staticresource phonetextnormalstyle}"/>             <textblock x:name="pagetitle" text="page name" margin="9,-7,0,0" style="{staticresource phonetexttitle1style}"/>         </stackpanel>          <!--contentpanel - place additional content here-->         <grid x:name="contentpanel" grid.row="1" margin="12,0,12,0">             <textbox height="72" horizontalalignment="left" margin="42,189,0,0" name="passwordtextbox"  verticalalignment="top" width="374"  maxlength="6" inputscope="number"  keyup="passwordtextbox_keyup">                </textbox>             <button content="button" height="72" horizontalalignment="left" margin="221,436,0,0" name="button1" verticalalignment="top" width="160" click="button1_click" />         </grid>      </grid>      <!--sample code showing usage of applicationbar-->     <!--<phone:phoneapplicationpage.applicationbar>         <shell:applicationbar isvisible="true" ismenuenabled="true">             <shell:applicationbariconbutton iconuri="/images/appbar_button1.png" text="button 1"/>             <shell:applicationbariconbutton iconuri="/images/appbar_button2.png" text="button 2"/>             <shell:applicationbar.menuitems>                 <shell:applicationbarmenuitem text="menuitem 1"/>                 <shell:applicationbarmenuitem text="menuitem 2"/>             </shell:applicationbar.menuitems>         </shell:applicationbar>     </phone:phoneapplicationpage.applicationbar>-->  </phone:phoneapplicationpage> 

everything fine, except 2 problems. 1) have set maxlength of textbox fix number, not working(i can put textbox's text more 6). 2) when typing on textbox, cursor not moving(it fixed on first position). it's not users perspective. please me sort out both these problems.

try this, it'll work want. passwordbox maxlength windows phone.

int maxlength = 4; string _enteredpasscode = ""; string _passwordchar = "*";      private void passwordtextbox_keyup(object sender, keyeventargs e)     {         if (passwordtextbox.text.length > 4)         {             // set text previous text of length 4             passwordtextbox.text = system.text.regularexpressions.regex.replace(_enteredpasscode, @".", _passwordchar);             //take cursor end of string             passwordtextbox.selectionstart = passwordtextbox.text.length;              e.handled = false;             return;         }          //modify new passcode according entered key         _enteredpasscode = getnewpasscode(_enteredpasscode, e);         //replace text *         passwordtextbox.text = system.text.regularexpressions.regex.replace(_enteredpasscode, @".", _passwordchar);         //take cursor end of string         passwordtextbox.selectionstart = passwordtextbox.text.length;     }          private string getnewpasscode(string oldpasscode, system.windows.input.keyeventargs keyeventargs)     {         string newpasscode = string.empty;         switch (keyeventargs.key)         {             case key.d0:             case key.d1:             case key.d2:             case key.d3:             case key.d4:             case key.d5:             case key.d6:             case key.d7:             case key.d8:             case key.d9:                 newpasscode = oldpasscode + (keyeventargs.platformkeycode - 48);                 break;             case key.back:                 if (oldpasscode.length > 0)                     newpasscode = oldpasscode.substring(0, oldpasscode.length - 1);                 break;             default:                 //others                 newpasscode = oldpasscode;                 break;         }         return newpasscode;     } 

Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -