Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent editing of TextBox before certain position?

How can a TextBox be prevented from being edited before a given position?

For example, if a TextBox contains the string:

Example Text: The black cat.

How can I prevent the user from editing anything before "The"?

I can try to trap the Backspace key with the KeyPress event, but how can I use MouseClick to prevent the user from moving the cursor to a position before "The".

like image 661
CJ7 Avatar asked Dec 14 '12 10:12

CJ7


1 Answers

You can use a single-line RichTextBox and protect the prefix like this

Private Sub Form_Load()
    Const STR_PREFIX = "Example Text: "

    RichTextBox1.Text = STR_PREFIX & "The black cat."
    RichTextBox1.SelStart = 0
    RichTextBox1.SelLength = Len(STR_PREFIX)
    RichTextBox1.SelProtected = True
    RichTextBox1.SelLength = 0
End Sub
like image 101
wqw Avatar answered Oct 23 '22 08:10

wqw