Reputation: 113
I want to use *
character in inputbox but an error prompts
conversion from "*" to string invalid
How can I make my inputbox hide typed text into password characters?
Here is my code
Dim value As String
value = InputBox("Security Check", " Enter password", "*")
If value = "123456" Then
numr.Enabled = True
End If
End Sub
Upvotes: 5
Views: 86199
Reputation: 435
Unfortunately, the InputBox
function doesn't have this parameter. VB.Net is reading your "*"
as the value for the DefaultResponse
parameter, or what value
will equal if the user just accepts the default entry.
As a matter of fact, InputBox
, while still found in the VisualBasic Namespace, is not considered up to date coding practice since 2003, but is still used by many who are or were accustomed to VB6 (including myself). It's most recent entry in the MSDN Visual Basic Reference is for the 2008 version of Visual Studio, and is not found in the current (2017) Visual Basic Language Reference.
The standard way of using an *
or other password character in .Net, is to use a Windows Forms TextBox, and then use either the PasswordChar
or UseSystemPasswordChar
properties to change the nature of the TextBox inside the Form. These can be accessed in the in the Form Design Properties window, or as properties of the TextBox within your code.
Upvotes: 0
Reputation: 50
You have to define it yourself this is the Code I wrote to make a custom Password Input Box I defined it as a class and then inherited the form class and gave it custom properties By doing this I have created a property that determines if access is granted or not. you can create encryption and communicate with Database for password retrieval but this simply shows how to use the custom control.
Public Class Form1
Dim test As New CustomForm("workflow")
Public Class CustomForm
Inherits Form
Property SecretPassword As String
Property GrantAccess As Boolean
Sub New(Password As String)
GrantAccess = False
Me.SecretPassword = Password
Dim lbl As New Label
lbl.Text = "Password"
Me.Controls.Add(lbl)
Me.Text = "***PASSWORD INPUT REQUIRED***"
Dim frmSZ As New Size(400, 100)
Me.Size = frmSZ
Dim IBox As New TextBox
AddHandler IBox.KeyDown, AddressOf TextBox1_KeyDown
Dim ibox20 As New Point(100, 0)
IBox.Location = ibox20
IBox.PasswordChar = "*"
Me.Controls.Add(IBox)
Me.Show()
End Sub
Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs)
If e.KeyCode = Keys.KeyCode.Enter Then
Try
Dim passswordInput As String = sender.text
If passswordInput = Me.SecretPassword Then
GrantAccess = True
Me.Dispose()
Else
MsgBox("Sorry the password you entered is not correct please try again. The password is case sensitive make sure your caps lock is not on.")
End If
Catch ex As Exception
End Try
End If
End Sub
End Class
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
MsgBox(test.GrantAccess)
End Sub
End Class
Upvotes: 1
Reputation: 326
This is not possible with the built in Function InputBox. Ths value you are setting "*" is the defaultvalue of that function. http://msdn.microsoft.com/en-us/library/6z0ak68w(v=vs.90).aspx
Here is something you could do. http://www.vbforums.com/showthread.php?627996-Accepting-password-characters-for-InputBox-function
Upvotes: 1