在Silverlight 2中创建密码输入框

在Silverlight 2中创建密码输入框,第1张

概述Silverlight Beta2中,没有提供密码输入框控件,估计在正式版里应该提供吧。Chris Pietschmann自己写了一个,原文地址是: http://pietschsoft.com/post/2008/03/PasswordTextBox-for-Silverlight-2-Beta-1.aspx 创建一个PasswordTextBox.cs类,代码如下: ///  Copyrigh

Silverlight Beta2中,没有提供密码输入框控件,估计在正式版里应该提供吧。Chris PIEtschmann自己写了一个,原文地址是:

http://pietschsoft.com/post/2008/03/PasswordTextBox-for-Silverlight-2-Beta-1.aspx

创建一个PasswordTextBox.cs类,代码如下:

///  copyright 2008 Chris PIEtschmann ( http://pIEtschsoft.com )
///  This work is licensed under a Creative Commons Attribution 3.0 United States license
///   http://creativecommons.org/licenses/by/3.0/us/
///
///  This is a Password TextBox built for use with Silverlight 2 Beta 1
///  The reason this was built, is because the standard TextBox in
///  Silverlight 2 Beta 1 does not have Password support.
///  Original link:  http://pIEtschsoft.com/post/2008/03/PasswordTextBox-for-Silverlight-2-Beta-1.aspx
///  

using  System.windows.Controls; 

namespace  SilverlightPasswordTextBox
{
    
public   partial   class  PasswordTextBox : TextBox
    {
        
public  PasswordTextBox()
        {
            
this .TextChanged  +=   new  TextChangedEventHandler(PasswordTextBox_TextChanged);
            
this .KeyDown  +=   new  System.windows.input.KeyEventHandler(PasswordTextBox_KeyDown);
        } 

        
#region  Event Handlers 

        
public   voID  PasswordTextBox_TextChanged( object  sender, TextChangedEventArgs e)
        {
            
if  ( base .Text.Length  >=  _Text.Length)
                _Text 
+=   base .Text.Substring(_Text.Length);
            displayMaskedCharacters();
        } 

        
public   voID  PasswordTextBox_KeyDown( object  sender, System.windows.input.KeyEventArgs e)
        {
            
int  cursorposition  =   this .SelectionStart;
            
int  selectionLength  =   this .SelectionLength; 

            
//  Handle Delete and Backspace Keys Appropriately
             if  (e.Key  ==  System.windows.input.Key.Back
                
||  e.Key  ==  System.windows.input.Key.Delete)
            {
                
if  (cursorposition  <  _Text.Length)
                    _Text 
=  _Text.Remove(cursorposition, (selectionLength  >   0   ?  selectionLength :  1 ));
            }
            
            
base .Text  =  _Text;
            
this .Select((cursorposition  >  _Text.Length  ?  _Text.Length : cursorposition),  0 );
            displayMaskedCharacters();
        } 

        
#endregion  

        
#region  Private Methods 

        
private   voID  displayMaskedCharacters()
        {
            
int  cursorposition  =   this .SelectionStart;
            
            
//  This changes the Text property of the base TextBox class to display all Asterisks in the control
             base .Text  =   new   string (_PasswordChar, _Text.Length); 

            
this .Select((cursorposition  >  _Text.Length  ?  _Text.Length : cursorposition),  0 );
        } 

        
#endregion  

        
#region  PropertIEs 

        
private   string  _Text  =   string .Empty;
        
///   <summary>
        
///  The text associated with the control.
        
///   </summary>
         public   new   string  Text
        {
            
get  {  return  _Text; }
            
set
            {
                _Text 
=  value;
                displayMaskedCharacters();
            }
        } 

        
private   char  _PasswordChar  =   ' * ' ;
        
///   <summary>
        
///  Indicates the character to display for password input.
        
///   </summary>
         public   char  PasswordChar
        {
            
get  {  return  _PasswordChar; }
            
set  { _PasswordChar  =  value; }
        } 

        
#endregion
    }


 

使用方法:

< UserControl  x:Class ="SilverlightApplication1.Page"
    xmlns
="http://schemas.microsoft.com/clIEnt/2007"  
    xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"  
    xmlns:myctl
="clr-namespace:SilverlightApplication1;assembly=SilverlightApplication1"  WIDth ="600"  Height ="480" >
    
< GrID  x:name ="LayoutRoot"  Background ="White" >
        
< Canvas  Canvas.top ="20" >           
            
< myctl:PasswordTextBox  x:name ="UserPassword"  WIDth ="200"  Height ="30"  Canvas.top ="40"  Canvas.left ="20" ></ myctl:PasswordTextBox >
        
</ Canvas >
    
</ GrID >
</ UserControl >

 

注意:要先加入名称空间,具体的值是:

clr-namespace:名称空间全名;assembly=程序集名称

通过使用,发现两个问题:

1,<myctl:PasswordTextBox PasswordChar="" />设置会报错

2,Text="初始值"仍旧是明文

原帖地址:http://blog.csdn.net/net_lover/archive/2008/04/06/2254267.aspx  感谢孟宪会老师

总结

以上是内存溢出为你收集整理的在Silverlight 2中创建密码输入框全部内容,希望文章能够帮你解决在Silverlight 2中创建密码输入框所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1055804.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-25
下一篇2022-05-25

发表评论

登录后才能评论

评论列表(0条)

    保存