Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在文本框中隐藏密码(最后一个字母除外)_C# - Fatal编程技术网

C# 在文本框中隐藏密码(最后一个字母除外)

C# 在文本框中隐藏密码(最后一个字母除外),c#,C#,我想制作一个带有屏蔽(*)文本框的登录屏幕,但我想用星号(*)屏蔽每个字符,但最后键入的字母除外(应以明文显示) 例如,当密码为123且我键入1时,文本框的内容将显示为1(因为这是最后键入的字母)。但是当我在1之后输入2时,文本框的内容将显示为*2,当我键入3时,文本框的内容将显示为**3 但我无法解决算法: private void txtPw_TextChanged(object sender, EventArgs e) { string inputString =

我想制作一个带有屏蔽(
*
)文本框的登录屏幕,但我想用星号(
*
)屏蔽每个字符,但最后键入的字母除外(应以明文显示)

例如,当密码为
123
且我键入
1
时,文本框的内容将显示为
1
(因为这是最后键入的字母)。但是当我在
1
之后输入
2
时,文本框的内容将显示为
*2
,当我键入
3
时,文本框的内容将显示为
**3

但我无法解决算法:

private void txtPw_TextChanged(object sender, EventArgs e)
    {
        string inputString = txtPw.Text;
        int index = inputString.Length;
       // char lastChar = inputString[inputString.Length-1];         
        txtPw.Text = "";           
        tempPassword += lastChar;
        for(int i=0;i<(index-1);i++)
        {
            txtPw.Text += "*";
        }
        txtPw.Text += lastChar;     
    }
private void txtPw_TextChanged(对象发送方,事件参数e)
{
字符串inputString=txtPw.Text;
int index=inputString.Length;
//char lastChar=inputString[inputString.Length-1];
txtPw.Text=“”;
tempPassword+=lastChar;

例如(int i=0;i虽然这不是一个完美的答案,但如果您只允许用户更改字符串中输入的最后一个字符(即密码末尾的退格或结尾的新字母),则可以在询问时屏蔽字符。为了可用性,它还使用了一个名为“currentPassword”的全局变量以便以后检查它是否正确

此代码可以进一步优化和修改,以减少使用子函数的行数,但我尝试将其保留在一个中,以便更容易地复制和粘贴

亲切问候,, 影子

string currentPassword=“”;
私有void textBox1\u TextChanged(对象发送方,事件参数e)
{
var currentInputBox=sender as TextBox;//如果使用多个文本框并使用相同的函数,则处理文本事件输入的更通用方法
var inputString=currentInputBox.Text;
if(inputString.Length>currentPassword.Length)//将新字母添加到全局变量currentPassword,然后屏蔽使用的原始文本框
{
var newChar=inputString.Substring(inputString.Length-1);
currentPassword+=newChar;
var newBoxText=“”;
对于(int i=0;i0))//从currentPassword字段中删除字符,直到它与textbox字段的长度匹配,然后在末尾显示单个字符
{
currentPassword=currentPassword.Remove(inputString.Length,currentPassword.Length-inputString.Length);
var newBoxText=“”;
对于(int i=0;i
因此,讨论了如何使用jQuery在web上创建iPhone风格的密码输入框。但是,根据代码,我假设您正在尝试在WinForms或WPF中执行类似操作

因此,一般解决方案需要以下几点:

  • 截获事件的代码
  • 类上的一个字段,其中包含键入的文本(您在上面的事件中截获了该文本-请注意,您还需要捕获退格)
  • 在文本框中插入一个字符,但在短暂(可能是1秒)延迟后用项目符号字符[•]替换它的逻辑
  • 因此,您将得到两个字符串—打印在文本框中的项目符号字符串和实际字符串。如果您使用WPF,您甚至可以编写一个奇特的值转换器来自动处理此问题


    作为一个考虑因素,您还需要考虑如果用户选择一个子字符串并决定替换文本会发生什么情况(您是否在键入时显示该特定字符)?为您编写一个完整控件似乎有点难,但希望这能给您一些关于如何继续的想法。

    首先,
    lastChar
    被注释掉,因此您不能在`tempPassword+=lastCharI中访问它,我建议您获取字段的长度,如果它大于1,您可以添加尽可能多的
    *
    如您所愿,但最后一个密码将不起作用,因为TextChanged不适用。无论您在任何位置进行了什么更改,插入或剪切,都会调用该密码。因此,您无法跟踪真实密码。通过适当的检查查看KeyDown或KeyPressed,可能会阻止除允许外的任何键,即不进行编辑,而是退格and没有定位。@Taw有一个很好的观点,事实上你应该得到一个堆栈溢出。@Mayhem不不,你弄错了,我故意对它进行了注释,以显示哪一行给了我错误
    string currentPassword = "";
    
    private void textBox1_TextChanged(object sender, EventArgs e)
            {
                var currentInputBox = sender as TextBox; // More general way to handle inputs for a text event if using multiple text boxes all with the same function
                var inputString = currentInputBox.Text;
    
                if (inputString.Length > currentPassword.Length) // Adds the new letter to the global variable currentPassword and then masks the original text box used
                {
                    var newChar = inputString.Substring(inputString.Length - 1);
                    currentPassword += newChar;
                    var newBoxText = "";
                    for (int i = 0; i < currentPassword.Length - 1; i++)
                    {
                        newBoxText += "*";
                    }
                    newBoxText += newChar;
                    currentInputBox.Text = newBoxText;
                }
                else if ((inputString.Length < currentPassword.Length) && (inputString.Length > 0)) // Removes characters from the currentPassword field til it matches the length of the textbox field and then exposes a single character at the end
                {
                    currentPassword = currentPassword.Remove(inputString.Length, currentPassword.Length - inputString.Length);
                    var newBoxText = "";
                    for (int i = 0; i < currentPassword.Length - 1; i++)
                    {
                        newBoxText += "*";
                    }
                    newBoxText += currentPassword.Substring(currentPassword.Length - 1);
                    currentInputBox.Text = newBoxText;
                }
            }