XAML VB中的密码消息框

ni65a41a  于 2023-08-01  发布在  其他
关注(0)|答案(1)|浏览(88)

当用户执行特定的操作(如保存数据)时。我需要一个消息框出现,要求用户在该消息框中输入密码,以验证数据。
有可能吗?如果是的话,是怎么做到的?

yhived7q

yhived7q1#

您可以创建自己的对话框。
范例:

Option Strict On
    Option Explicit On
    Option Infer Off
    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim pwD As New PasswordDialogBox
            If pwD.ShowDialog() = Windows.Forms.DialogResult.OK Then
                MessageBox.Show("The user entered the following password: '" & pwD.Password & "'", "Password Confirmed", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Else
                MessageBox.Show("The user cancelled.", "User Cancel", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If
        End Sub
    End Class
    Public Class PasswordDialogBox
        Inherits Form
        Friend WithEvents tbPassword As New TextBox With {.PasswordChar = "*"c, .Parent = Me}
        Friend WithEvents Label1 As New Label With {.Parent = Me}
        Friend WithEvents okButton As New Button With {.Text = "OK", .Parent = Me}
        Friend Shadows WithEvents cancelButton As New Button With {.Text = "Cancel", .Parent = Me}
        Public Property Password As String
        Sub New()
            Me.FormBorderStyle = Windows.Forms.FormBorderStyle.FixedToolWindow
            Me.Size = New Size(200, 150)
            Me.Text = "Enter Password"
            tbPassword.Left = Me.ClientRectangle.Width \ 2 - tbPassword.ClientRectangle.Width \ 2
            tbPassword.Top = Me.ClientRectangle.Height \ 2 - tbPassword.ClientRectangle.Height \ 2
            Label1.AutoSize = True
            Label1.Text = "Please enter a password"
            Label1.Left = (Me.ClientRectangle.Width \ 2) - (Label1.ClientRectangle.Width \ 2)
            okButton.Left = Me.ClientRectangle.Width - 5 - okButton.ClientRectangle.Width
            okButton.Top = Me.ClientRectangle.Height - 5 - okButton.Height
            cancelButton.Left = 5
            cancelButton.Top = Me.ClientRectangle.Height - 5 - cancelButton.Height
        End Sub
        Private Sub okButton_Click(sender As Object, e As EventArgs) Handles okButton.Click
            If PasswordMeetsCriteria(tbPassword.Text) Then
                Me.Password = tbPassword.Text
                Me.DialogResult = Windows.Forms.DialogResult.OK
            Else
                MessageBox.Show("Password is invalid, please re-enter your password or cancel.", "Invalid Password", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        End Sub
        Function PasswordMeetsCriteria(password As String) As Boolean
            Dim validCharacters As String = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ`1234567890-=~!@#$%^&*()_+,./;'[]\<>?:""{}"
            For Each c As Char In password
                If validCharacters.IndexOf(c) = -1 Then Return False
            Next
            Return True
        End Function
        Private Sub cancelButton_Click(sender As Object, e As EventArgs) Handles cancelButton.Click
            Me.DialogResult = Windows.Forms.DialogResult.Cancel
        End Sub
    End Class

字符串

相关问题