VB中默认的是Shift+方向键就可选中文字,如果你只用方向键,显得不太规范。 一定要这样的话,也可以用sendkeys,如: Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer) If KeyCode = vbKeyRight And Shift = 0 Then KeyCode = 0 SendKeys "+{right}" End If End Sub
Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer) select case keycode case vbkeyright if text1.sellength<len(text1.text) then text1.sellenght=text1.sellength +1 end if case vbkeyleft if text1.sellength>0 then text1.sellength=text1.sellength-1 end if end select End Sub
还是中海的方法好一点拉。《简单》 Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer) If KeyCode = vbKeyRight And Shift = 0 Then KeyCode = 0: SendKeys "+{right}" If KeyCode = vbKeyLeft And Shift = 0 Then KeyCode = 0: SendKeys "+{left}" End Sub
Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer) select case keycode case vbkeyright if text1.sellength<len(text1.text) then text1.sellenght=text1.sellength +1 end if case vbkeyleft if text1.sellength>0 then text1.sellength=text1.sellength-1 end if end select End SubGOOD!
改一下不就成了吗? Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer) If KeyCode = vbKeyRight And Shift = 0 Then KeyCode = 0: SendKeys "+{right}": Text1.SelStart = Text1.SelStart + 1: Text1.SelLength = 0 If KeyCode = vbKeyLeft And Shift = 0 Then KeyCode = 0: SendKeys "+{left}": Text1.SelLength = 0
End Sub
一点失误改正: Private Sub Text2_KeyDown(KeyCode As Integer, Shift As Integer) If KeyCode = vbKeyRight And Shift = 0 Then KeyCode = 0: SendKeys "+{right}": If Text2.SelLength <> 0 Then Text2.SelStart = Text2.SelStart + 1: Text2.SelLength = 0 If KeyCode = vbKeyLeft And Shift = 0 Then KeyCode = 0: SendKeys "+{left}": Text2.SelLength = 0 End Sub
一定要这样的话,也可以用sendkeys,如:
Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyRight And Shift = 0 Then
KeyCode = 0
SendKeys "+{right}"
End If
End Sub
select case keycode
case vbkeyright
if text1.sellength<len(text1.text) then
text1.sellenght=text1.sellength +1
end if
case vbkeyleft
if text1.sellength>0 then
text1.sellength=text1.sellength-1
end if
end select
End Sub
Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyRight And Shift = 0 Then KeyCode = 0: SendKeys "+{right}"
If KeyCode = vbKeyLeft And Shift = 0 Then KeyCode = 0: SendKeys "+{left}"
End Sub
select case keycode
case vbkeyright
if text1.sellength<len(text1.text) then
text1.sellenght=text1.sellength +1
end if
case vbkeyleft
if text1.sellength>0 then
text1.sellength=text1.sellength-1
end if
end select
End SubGOOD!
Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyRight And Shift = 0 Then KeyCode = 0: SendKeys "+{right}": Text1.SelStart = Text1.SelStart + 1: Text1.SelLength = 0
If KeyCode = vbKeyLeft And Shift = 0 Then KeyCode = 0: SendKeys "+{left}": Text1.SelLength = 0
End Sub
Private Sub Text2_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyRight And Shift = 0 Then KeyCode = 0: SendKeys "+{right}": If Text2.SelLength <> 0 Then Text2.SelStart = Text2.SelStart + 1: Text2.SelLength = 0
If KeyCode = vbKeyLeft And Shift = 0 Then KeyCode = 0: SendKeys "+{left}": Text2.SelLength = 0
End Sub