Source Code
Form Load Coding
TextBox4.Text = "S.No. "
TextBox4.SelectionStart = TextBox4.Text.Length
End Form load
Private prefix As String = "S.No."
Private Sub TextBox4_TextChanged(sender As Object, e As EventArgs) Handles TextBox4.TextChanged
RemoveHandler TextBox4.TextChanged, AddressOf TextBox4_TextChanged
' Check karein ke spaces hata kar text prefix se start ho raha hai ya nahi
If Not TextBox4.Text.TrimStart().StartsWith(prefix) Then
' Agar prefix delete ho jaye to purana text ya sirf prefix wapas le aayein
TextBox4.Text = prefix
TextBox4.SelectionStart = TextBox4.Text.Length
End If
AddHandler TextBox4.TextChanged, AddressOf TextBox4_TextChanged
End Sub
Private Sub TextBox4_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox4.KeyDown
Dim cursorPosition As Integer = TextBox4.SelectionStart
' Yeh maloom karne ke liye ke prefix kahan se shuru ho raha hai (spaces ke baad)
Dim prefixStartIndex As Integer = TextBox4.Text.IndexOf(prefix)
' 1. Backspace Protection: Agar cursor prefix ke start par ya andar ho
If e.KeyCode = Keys.Back Then
If cursorPosition <= prefixStartIndex + prefix.Length AndAlso cursorPosition > prefixStartIndex Then
' Prefix ke andar delete karne se rokein
e.SuppressKeyPress = True
End If
End If
' 2. Delete Key Protection
If e.KeyCode = Keys.Delete Then
If cursorPosition >= prefixStartIndex AndAlso cursorPosition < prefixStartIndex + prefix.Length Then
e.SuppressKeyPress = True
End If
End If
End Sub
Post a Comment