Reputation: 23275
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"
.
Upvotes: 6
Views: 663
Reputation:
Set your textbox to locked, then try this.
Private Sub Text2_KeyDown(KeyCode As Integer, Shift As Integer)
Dim TextMin As Integer
TextMin = 3
If Text2.SelStart > TextMin Then
Text2.Locked = False
ElseIf Text2.SelStart <= TextMin Then
Text2.Locked = True
End If
end sub
Upvotes: 0
Reputation: 2951
the following example doesn't accept keyboard input when you are before the marked start position, and when clicked inside the box before that position, it moves to the start position
these are 2 different answers upon unwanted actions .. you might want to use the same action in the _keypress and _click events
'1 form with
' 1 textbox : name=Text1
Option Explicit
Private mintStart As Integer
Private Sub Form_Load()
Text1.Text = "Example text: The black cat"
mintStart = Len("Example text: ")
End Sub
Private Sub Form_Resize()
Text1.Move 0, 0, ScaleWidth, ScaleHeight
End Sub
Private Sub Text1_Click()
With Text1
If .SelStart < mintStart Then
.SelStart = mintStart
End If
End With 'Text1
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
If Text1.SelStart < mintStart Then
KeyAscii = 0
End If
End Sub
Upvotes: 0
Reputation: 11991
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
Upvotes: 4
Reputation: 2213
Assumptions:
you want a static 'non-editable' string to be in the TextBox
control
you can click on this string, select it and it will look just like any other text in the text box, only difference is you will not be able to change it
the TextBox
name is txt1
Dim str As String Dim oldStr As String Private Sub Form_Load() str = "The" ' static string that you do not want to be edited oldStr = str + " black cat" ' default string to start with in the text box End Sub Private Sub txt1_Change() If UCase(Left(txt1.Text, Len(str))) <> UCase(str) Then ' the static string was edited, so we restore it to previously 'good' value txt1.Text = oldStr Else ' string was changed, but the change is 'good'. Save the new value oldStr = txt1.Text End If End Sub
This code will prevent the predefined string (str
) from being edited in the text box.
Upvotes: 0
Reputation: 4993
As long as it's a static text item you have pre-defined then I would take this approach:
Have the default value of the texbox as "The black cat " so the user can immediately see it.
Then use the OnGotFocus event of the textbox to remove the first 14 characters (The black cat ) with a space at the end. The user can then freely type what they want (this will preserve anything they have already typed if editing for a second or further time)
TextBox = Right(TextBox, Len(Textbox) - 14)
Then using the OnLostFocus event you can place the 14 characters back at the beginning of the textbox.
TextBox = "The black cat " & TextBox.Value
This method should avoid any complications from the user clicking the mouse anywhere in the field, you also don't need to track the physical data with the Change events.
Upvotes: 0
Reputation: 4069
Ok, 2ways. 1) On the change event, test to see if "the" is still at the beginning, and if not, add it. 2) Put the "the" in a label before the text box. You can even format it so it looks like the same control to the user.
Upvotes: 0
Reputation: 16101
You can use the TextBox_Changed
Event and then check its Text
property. If it does not start with "The" you put back "The black cat".
Upvotes: 0