WPF TextBox模仿PasswordBox的密码显示功能

WPF TextBox模仿PasswordBox的密码显示功能WPFTextBox显示密码,模仿PasswordBox的功能

WPF TextBox模仿PasswordBox的密码显示功能

这并不是多此一举,因为WPF的PasswordBox不支持继承,所以想扩展PasswordBox的属性就没法实现,所以有了本文内容,当然这个思路也可以扩展到其他语言。

已经更新了升级版,链接如下升级版链接
https://blog.csdn.net/qq_41908152/article/details/122067744

一、添加属性 Password,用于存储密码

public string Password { 
    get; set; } = string.Empty;

二、添加属性 PasswordChar,用于设置显示为密码的字符,默认为 ‘●’

public char PasswordChar { 
    get; set; } = '●';

三、重写TextBox的TextChanged事件事件,代码以及内部逻辑如下(用户有可能往密码框里粘贴密码,以下代码已全部考虑到了此操作)

//重写文本框内容改变事件
        protected override void OnTextChanged(TextChangedEventArgs e)
        { 
   
            base.OnTextChanged(e);
            //已键入的文本长度 Text 为 TextBox 的属性(获取或设置文本框的文本内容)
            int textLength = Text.Length;
            //已保存的密码长度
            int psdLength = Password.Length;
            //起始修改位置
            int startIndex = -1;
            for (int i = 0; i < textLength; i++)
            { 
   
                if (Text[i] != PasswordChar)
                { 
   
                    startIndex = i;
                    break;
                }
            }
            //未作任何修改
            if (startIndex == -1 && textLength == psdLength) return;
            //结束修改位置
            int stopIndex = -1;
            for (int i = textLength - 1; i >= 0; i--)
            { 
   
                if (Text[i] != PasswordChar)
                { 
   
                    stopIndex = i;
                    break;
                }
            }
            //添加或修改了一个或连续的多个值
            if (startIndex != -1)
            { 
   
                //累计修改长度
                int alterLength = stopIndex - startIndex + 1;
                //长度没变化,单纯的修改了一个或连续的多个值
                if (textLength == psdLength)
                { 
   
                    Password = Password.Substring(0, startIndex) + Text.Substring(startIndex, alterLength) + Password.Substring(stopIndex + 1);
                }
                //新增了内容
                else
                { 
   
                    //新增且修改了原来内容
                    if (alterLength > textLength - psdLength)
                    { 
   
                        //计算未修改密码个数 textLength - alterLength
                        //计算已修改密码个数 = 原密码长度 - 未修改密码个数 psdLength - (textLength - alterLength)
                        //原密码该保留的后半部分的索引 = 已修改密码个数 + 起始修改位置
                        Password = Password.Substring(0, startIndex) + Text.Substring(startIndex, alterLength) + Password.Substring(psdLength - (textLength - alterLength) + startIndex);
                    }
                    //单纯的新增了一个或多个连续的值
                    else
                    { 
   
                        Password = Password.Substring(0, startIndex) + Text.Substring(startIndex, alterLength) + Password.Substring(startIndex);
                    }

                }
            }
            //删除了一个或连续的多个值
            else
            { 
   
                //已删除的数据长度 SelectionStart 为 TextBox 的属性(获取或设置当前所选内容的起始位置的字符索引)
                int length = psdLength - textLength;
                if (SelectionStart < textLength)
                { 
   
                    //改变了中间的数据
                    Password = Password.Substring(0, SelectionStart) + Password.Substring(SelectionStart + length);
                }
                else
                { 
   
                    //删除了结尾的数据
                    Password = Password.Substring(0, SelectionStart);
                }
            }
            //记住光标位置(设置完Text后会丢失,所以现在要记住)
            int selectionStart = SelectionStart;
            //设置显示密码
            Text = new string(PasswordChar, textLength);
            //设置光标位置
            SelectionStart = selectionStart;
        }

怕有些初学者迷茫,把完整的类也贴出来吧(这是创建了一个“自定义控件”),虽然没啥东西(包含上述代码)

using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SuperControl
{ 
   
    public class SuperPasswordBox : TextBox
    { 
   
        static SuperPasswordBox()
        { 
   
            DefaultStyleKeyProperty.OverrideMetadata(typeof(SuperPasswordBox), new FrameworkPropertyMetadata(typeof(TextBox)));
        }

        /// <summary>
        /// 密码
        /// </summary>
        public string Password { 
    get; set; } = string.Empty;

        /// <summary>
        /// 显示为密码的字符
        /// </summary>
        public char PasswordChar { 
    get; set; } = '●';

        //重写文本框内容改变事件
        protected override void OnTextChanged(TextChangedEventArgs e)
        { 
   
            base.OnTextChanged(e);
            //已键入的文本长度
            int textLength = Text.Length;
            //已保存的密码长度
            int psdLength = Password.Length;
            //起始修改位置
            int startIndex = -1;
            for (int i = 0; i < textLength; i++)
            { 
   
                if (Text[i] != PasswordChar)
                { 
   
                    startIndex = i;
                    break;
                }
            }
            //未作任何修改
            if (startIndex == -1 && textLength == psdLength) return;
            //结束修改位置
            int stopIndex = -1;
            for (int i = textLength - 1; i >= 0; i--)
            { 
   
                if (Text[i] != PasswordChar)
                { 
   
                    stopIndex = i;
                    break;
                }
            }
            //添加或修改了一个或连续的多个值
            if (startIndex != -1)
            { 
   
                //累计修改长度
                int alterLength = stopIndex - startIndex + 1;
                //长度没变化,单纯的修改了一个或连续的多个值
                if (textLength == psdLength)
                { 
   
                    Password = Password.Substring(0, startIndex) + Text.Substring(startIndex, alterLength) + Password.Substring(stopIndex + 1);
                }
                //新增了内容
                else
                { 
   
                    //新增且修改了原来内容
                    if (alterLength > textLength - psdLength)
                    { 
   
                        //计算未修改密码个数 textLength - alterLength
                        //计算已修改密码个数 = 原密码长度 - 未修改密码个数 psdLength - (textLength - alterLength)
                        //原密码该保留的后半部分的索引 = 已修改密码个数 + 起始修改位置
                        Password = Password.Substring(0, startIndex) + Text.Substring(startIndex, alterLength) + Password.Substring(psdLength - (textLength - alterLength) + startIndex);
                    }
                    //单纯的新增了一个或多个连续的值
                    else
                    { 
   
                        Password = Password.Substring(0, startIndex) + Text.Substring(startIndex, alterLength) + Password.Substring(startIndex);
                    }

                }
            }
            //删除了一个或连续的多个值
            else
            { 
   
                //已删除的数据长度
                int length = psdLength - textLength;
                if (SelectionStart < textLength)
                { 
   
                    //改变了中间的数据
                    Password = Password.Substring(0, SelectionStart) + Password.Substring(SelectionStart + length);
                }
                else
                { 
   
                    //删除了结尾的数据
                    Password = Password.Substring(0, SelectionStart);
                }
            }
            //记住光标位置(设置完Text后会丢失,所以现在要记住)
            int selectionStart = SelectionStart;
            //设置显示密码
            Text = new string(PasswordChar, textLength);
            //设置光标位置
            SelectionStart = selectionStart;
        }
    }
}

有问题欢迎留言,如果觉得有用请点个”赞”,谢谢!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/161476.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Idea激活码永久有效Idea2017.1.6激活码教程-持续更新,一步到位[通俗易懂]

    Idea激活码永久有效Idea2017.1.6激活码教程-持续更新,一步到位[通俗易懂]Idea激活码永久有效2017.1.6激活码教程-Windows版永久激活-持续更新,Idea激活码2017.1.6成功激活

    2022年6月17日
  • uva-10487 – Closest Sums

    uva-10487 – Closest Sums

    2022年2月2日
  • 获取iframe src里的参数

    获取iframe src里的参数父业面iframe:<divid=”vue”> <iframeid=”mainiframe”width=”100%”height=”100%”frameborder=”no”border=”0″marginwidth=”0″marginheight=”0″scrolling=”auto”src=”../swap/finance/index.html?CID=13123jklkljlajkj”></iframe></div>子页

    2022年7月15日
  • 目前主流的app开发方式

    目前主流的app开发方式移动应用开发三种方式比较随着H5标准的发布以及推广,使得移动应用的开发也受到了很大影响,出于效率、成本等等的原因,移动应用的开发不在完全依赖于“原生”。今天就简单总结一下目前的三大主流移动应用开发类型。APP,一般认为是mobileapplication,也就是移动移动应用程序。1.NativeApp:本地应用程序(原生App)NativeApp是一种基于智能手…

    2022年5月30日
  • switch 中 break 和 continue 的区别[通俗易懂]

    switch 中 break 和 continue 的区别[通俗易懂]1、break用来退出switch,continue本身是不能用在switch里的,他必须结合循环来用,表示跳过本次循环2、switch的case语句最后如果没有加break/continue,则程序会一直往后执行,可以借鉴goto跳转到位置,其实switch本身可以看作某种形式的跳转,而C语言中本身是不执行:的那条语句的,例如,如果程序中出现一条语句wtch…

    2022年6月2日
  • pip安装scrapy失败_scrapy框架运行

    pip安装scrapy失败_scrapy框架运行错误如图所示,running setup.py install for Twisted…..errorTwisted依赖库安装报错,重新下载手动安装一下下载网址:https://www.lfd.uci.edu/~gohlke/pythonlibs注意:看下安装的python是什么版本,我安装的python 3.9.0,就下载cp39,64位的下载安装的版本不对,就会报:Twisted-20.3.0-cp38-cp38-win_amd64.whl is not a support…

    2022年8月18日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号