vb.net 使用 CheckBox 切换 TextBox 的屏蔽和取消屏蔽

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14460903/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 11:59:01  来源:igfitidea点击:

Toggle the masking and unmasking of a TextBox using a CheckBox

vb.netcheckboxtextboxmasking

提问by ABANDOND ACOUNT

I have a TextBox, which has a CheckBoxoperation to mask the containing text. This works with the following code:

我有一个TextBox,它有一个CheckBox操作来屏蔽包含的文本。这适用于以下代码:

Private Sub CheckBox2_Checked(ByVal sender As Object, ByVal e As EventArgs) Handles CheckBox2.CheckedChanged
    TextBox14.PasswordChar = "*"
End Sub

It works well, but I want to also be able to uncheck theCheckBoxand then have the recognizable text return. How can I achieve this?

它运行良好,但我也希望能够取消选中CheckBox,然后返回可识别的文本。我怎样才能做到这一点?

回答by Steven Doggart

You can do so by simply setting the PasswordCharproperty back to a null character, like this:

您可以通过简单地将PasswordChar属性设置回空字符来实现,如下所示:

Private Sub CheckBox2_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) Handles CheckBox2.CheckedChanged
    If CheckBox2.Checked Then
        TextBox14.PasswordChar = "*"c
    Else
        TextBox14.PasswordChar = ControlChars.NullChar
    End If
End Sub

The CheckedChangedevent occurs every time the Checkedproperty changes. So, when the user unchecks the CheckBox, it will raise that event too, so you need to check to see whether or not the control is currently checked.

CheckedChanged每次Checked属性更改时都会发生该事件。因此,当用户取消CheckBox选中时,它也会引发该事件,因此您需要检查该控件当前是否被选中。

回答by tcarvin

The docos actually state:

文档实际上指出:

The character used to mask characters entered in a single-line TextBox control. Set the value of this property to 0 (character value) if you do not want the control to mask characters as they are typed. Equals 0 (character value) by default.

用于屏蔽在单行 TextBox 控件中输入的字符的字符。如果您不希望控件在键入时屏蔽字符,请将此属性的值设置为 0(字符值)。默认情况下等于 0(字符值)。

Found here: http://msdn.microsoft.com/en-us/library/system.windows.forms.textbox.passwordchar(v=vs.110).aspx

在这里找到:http: //msdn.microsoft.com/en-us/library/system.windows.forms.textbox.passwordchar(v=vs.110).aspx

In VB.NET, that would be easiest done by setting PasswordCharto vbNullChar.

在 VB.NET 中,通过设置PasswordCharvbNullChar.

回答by David Carrigan

I found just toggling the password character wasn't enough. In my case I was masking a connection string. With the lack of spaces in my text I had an issue going back and forth. My text would be cut off and wasn't wrapping properly.

我发现仅仅切换密码字符是不够的。在我的情况下,我屏蔽了一个连接字符串。由于我的文本中缺少空格,我在来回切换时遇到了问题。我的文字会被切断并且没有正确换行。

Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs)
    Dim beforeText As String = TextBox1.Text

    TextBox1.Text = ""
    TextBox1.PasswordChar = IIf(CheckBox1.Checked, Convert.ToChar(0), "*"c)
    TextBox1.Text = beforeText
End Sub

I imagine if you used a font like Console this would not be a problem as all character widths are constant.

我想如果您使用像 Console 这样的字体,这不会成为问题,因为所有字符宽度都是恒定的。