Password char in textbox
-
Hi,
Somebody knows how I can set only to the first five characters in a textbox the passoword char.
Example:
*****8965
The characters can’t be replaced with *.
-
Hi lexian
Somthing like this will do the work in vb6
Private Sub Text1_Change()
Text1.PasswordChar = "*"
If Len(Text1.Text) >= 5 Then
Text1.PasswordChar = ""
End If
End SubRegards
DoctorMahdi
-
Hi,
DoctorMahdi I try the code and when the length in the textbox exceed the five characters this happends:
When still length<5 is ok.
*****
But when is length>5 boom! the asterisk go away.
854554545
-
Hi,
I have this code that works fine when the input is manual but when the input is assign like this Textbox1.Text = “85463215” it doesn't work. If somebody wants to help here is a clue.Dim TheTextBoxValue As String
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click MessageBox.Show(TheTextBoxValue) End Sub
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress If Char.IsLetterOrDigit(e.KeyChar) Then If Me.TextBox1.Text.Length < 5 Then TheTextBoxValue = TheTextBoxValue & e.KeyChar e.Handled = True Me.TextBox1.Text = Me.TextBox1.Text & "*" Me.TextBox1.SelectionStart = Me.TextBox1.Text.Length Else TheTextBoxValue = TheTextBoxValue & e.KeyChar End If Else 'code for adjusting for control 'characters such as the backspce..delete etc End If End Sub
-
Why don't you use mask control?!
-
Hi,
Because what the mask control does is place a fixed string in place. Like for example: the mask for a date value is something like “__/__/____” you are not able to write in the space 3 and 6 because there is the “/”character. In my case I want the first 5 characters to be hidden from the user, not replaced with an asterisk. For example: In the textbox I have *****5418 but the real value is 591885418.
This is to hide the first five numbers of the social security number from single users.
If with some properties configurations in a mask control this could be done tell me.
-
Well you could just create a function that returns a masked string. For example:
Private Function MaskedText(ByVal text As String, ByVal maskLength As Integer) As String Dim result As String = "" If text.Length > maskLength Then result = text.Substring(maskLength, text.Length - maskLength) End If Return result.PadLeft(text.Length, "*") End Function
Then you could store the real value in your variable that you already declared then set the textbox1.text = MaskedText(realText , 5) -
If you wanted to get a little fancier and could create your own custom control. This should help with that. It's just a quick example. I've created a class that inherits from TextBox so you can't just drag and drop the control onto the form, but you could use it by adding the textbox to your form in code. It would be nicer I'm sure to create a custom control, but I don't know how so I can't help you with that.
Public Class MyTextBox Inherits TextBox 'The real value of our textbox Private m_Text As String = "" 'Override the Text property so we can do our own formatting Public Overrides Property Text() As String Get Return m_Text End Get Set(ByVal value As String) Const MaskLength As Integer = 5 Dim masked As String = "" 'Store the real value m_Text = value 'If longer then our MaskLength strip the necessary characters If value.Length > MaskLength Then masked = value.Substring(MaskLength, value.Length - MaskLength) End If 'Set our text to the masked value MyBase.Text = masked.PadLeft(value.Length, "*") End Set End Property Protected Overrides Sub OnKeyPress(ByVal e As System.Windows.Forms.KeyPressEventArgs) If Char.IsLetterOrDigit(e.KeyChar) Then m_Text &= e.KeyChar If m_Text.Length < 5 Then e.KeyChar = "*"c End If Else 'code for adjusting for control 'characters such as the backspce..delete etc End If MyBase.OnKeyPress(e) End Sub End Class
To use this class just do the followingPublic Class Form1 WithEvents Text1 As New MyTextBox Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Text1.SetBounds(10, 10, 200, 50) Me.Controls.Add(Text1) End Sub End Class
-
Hi TwoFaced,
This is working but I need to cancel the copy - paste data into the textbox. How I cancel if the user is going to paste something in the textbox.Public Class MyTextBox Inherits System.Windows.Forms.TextBox
'The real value of our textbox Public m_Text As String = "" 'Notify if a key has been press Private uKeyPress As Boolean = False 'Returns the real value Public Function ProtectedText() As String Return m_Text End Function Public Overrides Property Text() As String Get Return MyBase.Text End Get Set(ByVal value As String) MyBase.Text = value End Set End Property Protected Overrides Sub OnKeyPress(ByVal e As System.Windows.Forms.KeyPressEventArgs) MyBase.OnKeyPress(e) Dim CursorLocation As Integer = MyBase.SelectionStart If Char.IsLetterOrDigit(e.KeyChar) Then uKeyPress = True If MyBase.SelectionStart < m_Text.Length Then 'If the cursor is between the string, the character is added in the correct place m_Text = m_Text.Insert(MyBase.SelectionStart, e.KeyChar) Else 'The character is added to the end of the string m_Text &= e.KeyChar End If MyBase.Text = DisplayMask() 'm_Text MyBase.SelectionStart = CursorLocation + 1 e.Handled = True ElseIf e.KeyChar = vbBack Then 'If the backspace key is press uKeyPress = True If CursorLocation > 0 And m_Text.Length > 0 Then 'This remove the selected charecter/s in the string If MyBase.SelectionLength > 0 Then m_Text = m_Text.Remove(CursorLocation, MyBase.SelectionLength) Else m_Text = m_Text.Remove(CursorLocation - 1, 1) End If End If End If End Sub Protected Overrides Sub OnKeyDown(ByVal e As System.Windows.Forms.KeyEventArgs) MyBase.OnKeyDown(e) Dim CursorLocation As Integer = MyBase.SelectionStart Select Case e.KeyCode Case Keys.Back Case Keys.Delete 'If the delete key is press uKeyPress = True If CursorLocation < m_Text.Length Then 'This remove the selected charecter/s in the string If MyBase.SelectionLength > 0 Then m_Text = m_Text.Remove(CursorLocation, MyBase.SelectionLength) Else m_Text = m_Text.Remove(CursorLocation, 1) End If End If Case Keys.Space 'If the space key is press uKeyPress = True m_Text &= " " End Select End Sub Protected Overrides Sub OnTextChanged(ByVal e As System.EventArgs) MyBase.OnTextChanged(e) If uKeyPress Then uKeyPress = False Else 'If data is assign to the textbox not typed, the value is assign to the m_Text If MyBase.Text.Length > 0 Then If MyBase.Text.Substring(0, 1) <> "*" Then m_Text = MyBase.Text MyBase.Text = DisplayMask() uKeyPress = True End If Else m_Text = "" End If End If End Sub Private Function DisplayMask() As String Dim Mask As String = "" If m_Text.Length > 0 And m_Text.Length < 5 Then For i As Integer = 0 To m_Text.Length - 1 Mask &= "*" Next Return m_Text.Replace(m_Text.Substring(0, m_Text.Length), Mask) ElseIf m_Text.Length > = 5 Then Return m_Text.Replace(m_Text.Substring(0, 5), "*****") Else Return Nothing End If End Function
End Class
-
I found this which may help http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/Q_21741197.html
Sorry, I don't think I can help further on canceling the copy paste. The discussion I linked however looked like it might have the information you need.
-
Hi,
TwoFaced, I was trying this:
Select Case e.KeyCode
Case Keys.Control And Keys.V
e.SuppressKeyPress = True
End Select
Thanks for the help. Know is finish and fully functional.
Public Class MyTextBox
Inherits System.Windows.Forms.TextBox
'The real value of our textbox
Public m_Text As String = ""
'Notify if a key has been press
Private uKeyPress As Boolean = False
Public Sub New()
'This removes the context menu strip
'not allowing the copy-paste options
MyBase.ContextMenu = New ContextMenu
End Sub
'Returns the real value
Public Function ProtectedText() As String
Return m_Text
End Function
Protected Overrides Sub OnKeyPress(ByVal e As System.Windows.Forms.KeyPressEventArgs)
MyBase.OnKeyPress(e)
Dim CursorLocation As Integer = MyBase.SelectionStart
If Char.IsLetterOrDigit(e.KeyChar) Then
uKeyPress = True
If MyBase.SelectionStart < m_Text.Length Then
'If the cursor is between the string,
'the character is added in the correct place
m_Text = m_Text.Insert(MyBase.SelectionStart, e.KeyChar)
Else
'The character is added to the end of the string
m_Text &= e.KeyChar
End If
MyBase.Text = DisplayMask()
MyBase.SelectionStart = CursorLocation + 1
e.Handled = True
ElseIf e.KeyChar = vbBack Then
'If the backspace key is press
uKeyPress = True
If CursorLocation > 0 And m_Text.Length > 0 Then
'This remove the selected charecter/s in the string
If MyBase.SelectionLength > 0 Then
m_Text = m_Text.Remove(CursorLocation, MyBase.SelectionLength)
Else
m_Text = m_Text.Remove(CursorLocation - 1, 1)
End If
End If
End If
End Sub
Protected Overrides Sub OnKeyDown(ByVal e As System.Windows.Forms.KeyEventArgs)
MyBase.OnKeyDown(e)
Dim CursorLocation As Integer = MyBase.SelectionStart
Select Case e.KeyCode
Case Keys.Delete
'If the delete key is press
uKeyPress = True
If CursorLocation < m_Text.Length Then
'This remove the selected charecter/s in the string
If MyBase.SelectionLength > 0 Then
m_Text = m_Text.Remove(CursorLocation, MyBase.SelectionLength)
Else
m_Text = m_Text.Remove(CursorLocation, 1)
End If
End If
Case Keys.Space
'If the space key is press
uKeyPress = True
m_Text &= " "
End Select
If e.Control AndAlso (e.KeyCode = Keys.V OrElse e.KeyCode = Keys.C) Then e.SuppressKeyPress = True
End Sub
Protected Overrides Sub OnTextChanged(ByVal e As System.EventArgs)
MyBase.OnTextChanged(e)
If uKeyPress Then
uKeyPress = False
Else
'If data is assign to the textbox not typed
'then the value is assign to the m_Text
If MyBase.Text.Length > 0 Then
If MyBase.Text.Substring(0, 1) <> "*" Then
m_Text = MyBase.Text
MyBase.Text = DisplayMask()
uKeyPress = True
End If
Else
m_Text = ""
End If
End If
End Sub
Private Function DisplayMask() As String
Dim Mask As String = ""
If m_Text.Length > 0 AndAlso m_Text.Length < 5 Then
Dim TextMasked As String = ""
Return TextMasked.PadLeft(m_Text.Length, "*")
ElseIf m_Text.Length >= 5 Then
Dim TextMasked As String = ""
If m_Text.Length > 5 Then
TextMasked = m_Text.Substring(5, m_Text.Length - 5)
End If
Return TextMasked.PadLeft(m_Text.Length, "*")
Else
Return Nothing
End If
End Function
End Class
Post a reply
Quick links
Recent activity
- arif ahmad replied to How to receive data in web ...
- William Thompson replied to What is the name of the Win...
- Sameera Piyadigamage replied to Point of Sale Developers: H...
- Scott Carline replied to 4 x C# Developers for large...
- Rajendra Dhakal replied to Restore SQL Server text dat...
- cloud rainda replied to How to convert between TS f...
Enter your message below
Sign in or Join us (it's free).