【问题标题】:How can I use Visual Studio to show the hint “Enter your password” in a password field, but then use stars to hide the input when user types?如何使用 Visual Studio 在密码字段中显示“输入您的密码”提示,然后在用户键入时使用星号隐藏输入?
【发布时间】:2014-07-18 10:38:42
【问题描述】:

我正在创建一个基于 C# windows 的程序,在该程序中我需要确保当用户输入他/她的密码时,它会显示带有这样的星号:*******强>。

【问题讨论】:

  • Winforms? WPF?控制台应用?
  • 初始文本通常称为水印。搜索那个词 + 文本框 + 你正在使用的任何技术都应该找到解决方案。
  • 我们需要知道您使用的框架才能正确回答,但基本上您想使用密码框并设置提示属性。

标签: c# passwords


【解决方案1】:

在前端创建一个名为textbox1的文本框后尝试代码

using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
public class Form2
{

    private void Form2_Load(System.Object sender, System.EventArgs e)
    {
        TextBox1.Text = "Enter your security Password";
    }

    public void change(TextBox txt_pwd)
    {
        txt_pwd.Text = "";
        txt_pwd.PasswordChar = "*";
    }

    private void TextBox1_Click(object sender, System.EventArgs e)
    {
        change(TextBox1);
    }

    private void TextBox1_GotFocus(object sender, System.EventArgs e)
    {
        change(TextBox1);
    }
    public Form2()
    {
        Load += Form2_Load;
    }
}

【讨论】:

  • 你确定他用的是win forms吗?
【解决方案2】:

每个包含 UI 元素的框架都有一个名为 PasswordBox 或类似名称的控件。

基本上,你应该使用那个。

此控件包含一个属性,允许您选择要显示的特殊字符以表示框内的字符,例如PaswwordChar

最后,ToolTip 属性将允许您根据问题标题设置提示。

例如:

PasswordBox pw = new PasswordBox();

pw.MaxLength = 25;

pw.PasswordChar = '*';

pw.ToolTip = "I'm a password!"

将创建一个密码框,用开始符号 (*) 替换其中的每个字符,并在鼠标指针悬停在它上面时显示一个黄色的小弹出窗口,显示“我是密码!”消息。

此外,该控件将不允许超过 25 个字符的密码。

以上链接是针对 WPF 的,但无论你使用 winforms、silverlight 还是其他一些 UI 框架,逻辑都不会改变。

编辑:

如果您要查找的是密码框的水印属性,那么您应该检查现有的答案,例如WPF Watermark PasswordBox from Watermark TextBox

【讨论】:

  • 他希望文本在密码文本框中​​,通常称为水印或提示文本或空文本或类似内容。
猜你喜欢
  • 1970-01-01
  • 2020-03-19
  • 1970-01-01
  • 2021-04-17
  • 1970-01-01
  • 2011-10-17
  • 2012-08-09
  • 2011-12-06
  • 1970-01-01
相关资源
最近更新 更多