Wpf VB中的密码消息框
当用户执行某些特定操作(如保存数据)时。我需要出现一个messagebox,要求用户在该messagebox中输入密码以验证数据Wpf VB中的密码消息框,wpf,vb.net,xaml,Wpf,Vb.net,Xaml,当用户执行某些特定操作(如保存数据)时。我需要出现一个messagebox,要求用户在该messagebox中输入密码以验证数据 可能吗?如果是这样,怎么做?您可以创建自己的对话框 例如: Option Strict On Option Explicit On Option Infer Off Public Class Form1 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Cli
可能吗?如果是这样,怎么做?您可以创建自己的对话框 例如:
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
可能吗?对怎么做?这取决于你的代码。明显地也许你应该雇个人来做这个改变……好的,谢谢,至少我知道这是可能的。我只是想知道怎么做。我四处寻找过定制的MessageBox,但还没有找到一个解决方案,用一个或多个文本框来近似MessageBox的行为。您需要编写自己的表单,并使用ShowDialog方法来显示它。您还需要使用公共属性在表单之间提供某种类型的值传递。完整答案太宽泛了。使用输入框这将非常适合您的需要,因为它会返回用户的输入。请检查下面的自定义密码对话框。谢谢,我将查看代码,看看我可以从中提取什么,以便将其实现到我的系统中。不客气!如果您对此有任何疑问,请告诉我:-