当用户执行特定的操作时,例如保存数据。我需要一个消息框,要求用户在该消息框中输入密码以验证数据。
有可能吗?如果是这样,怎么样?
答案 0 :(得分:0)
您可以制作自己的DialogBoxes。
示例:
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