孟宪会ID:net_lover
881360次访问,排名30好友199人,关注者0
http://dotnet.aspx.cc/
【声明:各位好,由于系统“添加好友”功能只能添加200个,后来的朋友不能加入,见谅!!!】
[加为好友] [即时聊天] [发私信]
net_lover的文章
原创 243 篇
翻译 2 篇
转载 5 篇
评论 846 篇
孟宪会的公告
欢迎光临!您在阅读的过程中有任何建议或者意见,请发邮件或者留言,合作愉快!
最近评论
wangzhongke1983:代码运行到e.Result as Stream就出错了,断点得出:未将对象引用设置到对象的实例。
onlytiancai:这个可以公开吗?
PrideRock:你发这文章是留备份还是啥意思?给讲讲?
lfywy:确实不错,经测试,很不错!感谢孟子分享!
lczddd:牛人就是牛人,不是一般的牛,是巨牛,呵呵
文章分类
收藏
    相册
    文章用图
    【孟子E章】站点
    【孟子E章】站点(RSS)
    【孟子E章】网摘(RSS)
    【孟子E章】网站(RSS)
    Silverlight 1.0 SDK 中文版
    Silverlight 1.0 SDK 中文版
    【网上邻居】
    .Net开发资源中心
    【兔子】专栏
    AppleVB 主页
    Estyle(靳田)之狂想手扎
    huahaoyueyuan
    JavaProgramers的专栏
    意玺的BLOG
    最爱白菜
    枕善居VB.NET源码博客
    美丽眼睛看世界
    阿赖
    存档
    订阅我的博客
    XML聚合  FeedSky

    转载 在Silverlight 2中创建密码输入框收藏

    新一篇: Windows XP Service Pack 3 (Windows XP SP3)简体中文版本正式发布(附下载地址) | 旧一篇: 得到给定扩展名的文件图标

    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="初始值"仍旧是明文

     

    发表于 @ 2008年04月27日 22:32:00|评论(loading...)|编辑

    新一篇: Windows XP Service Pack 3 (Windows XP SP3)简体中文版本正式发布(附下载地址) | 旧一篇: 得到给定扩展名的文件图标

    评论:没有评论。

    发表评论  


    当前用户设置只有注册用户才能发表评论。如果你没有登录,请点击登录
    Csdn Blog version 3.1a
    Copyright © 孟宪会