Private Sub Text1_GotFocus() Me.Text1.SelLength = 1 End SubPrivate Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer) Me.Text1.SelLength = 1 End Sub
Private Sub Text1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single) Me.Text1.SelLength = 1 End Sub
一个按钮,一个textbox:Dim ischange As BooleanPrivate Sub Command1_Click() ischange = Not ischange Text1.SetFocus End SubPrivate Sub Form_Load() ischange = False Command1.Caption = "文本框输入状态切换" End SubPrivate Sub Text1_GotFocus() If ischange Then Me.Text1.SelLength = 1 Else Me.Text1.SelLength = 0 End If End Sub
Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer) If ischange Then Me.Text1.SelLength = 1 Else Me.Text1.SelLength = 0 End If End Sub
Private Sub Text1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single) If ischange Then Me.Text1.SelLength = 1 Else Me.Text1.SelLength = 0 End If End Sub
和和,那么简单的程序,我能看懂:)。下面你那个回答可以不用咯。 我稍改造了一下: Private Sub Text1_GotFocus() Me.Text1.SelLength = 1 End SubPrivate Sub Text1_KeyPress(KeyAscii As Integer) Me.Text1.SelLength = 1 End SubPrivate Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer) If KeyCode = 37 Then Me.Text1.SelStart = Me.Text1.SelStart - 1 End If
Me.Text1.SelLength = 1 End Sub
Private Sub Text1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single) Me.Text1.SelLength = 1 End Sub 不过,感觉效果不是非常好,有点儿滞的。不知是不是还有别的更好的方法,比如用API,或是别的VB标准控件代替text控件。
最后再次改进: Private Sub MaskEdBox1_GotFocus() MaskEdBox1.SelLength = 1 End SubPrivate Sub MaskEdBox1_KeyDown(KeyCode As Integer, Shift As Integer) If (KeyCode >= 37 And KeyCode <= 40) And MaskEdBox1.SelStart > 0 Then MaskEdBox1.SelStart = MaskEdBox1.SelStart - 1 End If MaskEdBox1.SelLength = 1 End Sub这样就比较好了,在这儿我用的是MaskEdBox控件,text控件上应也一样。 谢谢rainstormmaster提供思路。
Me.Text1.SelLength = 1
End SubPrivate Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
Me.Text1.SelLength = 1
End Sub
Private Sub Text1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
Me.Text1.SelLength = 1
End Sub
ischange = Not ischange
Text1.SetFocus
End SubPrivate Sub Form_Load()
ischange = False
Command1.Caption = "文本框输入状态切换"
End SubPrivate Sub Text1_GotFocus()
If ischange Then
Me.Text1.SelLength = 1
Else
Me.Text1.SelLength = 0
End If
End Sub
Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
If ischange Then
Me.Text1.SelLength = 1
Else
Me.Text1.SelLength = 0
End If
End Sub
Private Sub Text1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
If ischange Then
Me.Text1.SelLength = 1
Else
Me.Text1.SelLength = 0
End If
End Sub
我稍改造了一下:
Private Sub Text1_GotFocus()
Me.Text1.SelLength = 1
End SubPrivate Sub Text1_KeyPress(KeyAscii As Integer)
Me.Text1.SelLength = 1
End SubPrivate Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
If KeyCode = 37 Then
Me.Text1.SelStart = Me.Text1.SelStart - 1
End If
Me.Text1.SelLength = 1
End Sub
Private Sub Text1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
Me.Text1.SelLength = 1
End Sub
不过,感觉效果不是非常好,有点儿滞的。不知是不是还有别的更好的方法,比如用API,或是别的VB标准控件代替text控件。
Private Sub MaskEdBox1_GotFocus()
MaskEdBox1.SelLength = 1
End SubPrivate Sub MaskEdBox1_KeyDown(KeyCode As Integer, Shift As Integer)
If (KeyCode >= 37 And KeyCode <= 40) And MaskEdBox1.SelStart > 0 Then
MaskEdBox1.SelStart = MaskEdBox1.SelStart - 1
End If
MaskEdBox1.SelLength = 1
End Sub这样就比较好了,在这儿我用的是MaskEdBox控件,text控件上应也一样。
谢谢rainstormmaster提供思路。