Private Sub Text1_Change() If Len(Text1.Text) <= 3 Then Text1.Text = "aaa" Text1.SelStart = Len(Text1) Else If Mid(Text1.Text, 1, 3) <> "aaa" Then Text1.Text = "aaa" Text1.SelStart = Len(Text1) End If End If End Sub
一个更好的方法 其中的3代表需要控制字符的长度Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer) If KeyCode = 46 And Text1.SelStart < 3 Then KeyCode = 0 End If End SubPrivate Sub Text1_KeyPress(KeyAscii As Integer) If KeyAscii = 8 And Text1.SelStart = 3 Then KeyAscii = 0 If Text1.SelStart >= 3 Then Else KeyAscii = 0 End If End Sub
>>步骤1----建立新工程,在窗体上放置一个TextBox; >>步骤2----在窗体上创建一菜单,设其Name = a;Visible = false; >>步骤3----编写如下代码: Private Sub Text1_MouseDown(Button As Integer, Shift _ As Integer, X As Single, Y As Single) If Button = vbRightButton Then Text1.Enabled = False Text1.Enabled = True PopupMenu a End If End Sub
获得焦点时除掉"aaa"
失处焦点时添加进去
在文本框前贴一个caption为"aaa"的标签,文本框中不写“aaa”。
If Len(Text1.Text) <= 3 Then
Text1.Text = "aaa"
Text1.SelStart = Len(Text1)
Else
If Mid(Text1.Text, 1, 3) <> "aaa" Then
Text1.Text = "aaa"
Text1.SelStart = Len(Text1)
End If
End If
End Sub
我同意这两种方法。
其中的3代表需要控制字符的长度Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 46 And Text1.SelStart < 3 Then
KeyCode = 0
End If
End SubPrivate Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = 8 And Text1.SelStart = 3 Then KeyAscii = 0
If Text1.SelStart >= 3 Then
Else
KeyAscii = 0
End If
End Sub
>>步骤2----在窗体上创建一菜单,设其Name = a;Visible = false;
>>步骤3----编写如下代码:
Private Sub Text1_MouseDown(Button As Integer, Shift _
As Integer, X As Single, Y As Single)
If Button = vbRightButton Then
Text1.Enabled = False
Text1.Enabled = True
PopupMenu a
End If
End Sub