我想在txtbox中按向左或向右箭头时,向左或向右的一个数字或文字被选中,如何实现?
因为如果不是这样的话,要左删键或del键删除左边或右边的数字或文字才能改动txtbox中的数字或文字.

解决方案 »

  1.   

    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
      

  2.   

    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
      

  3.   

    还是中海的方法好一点拉。《简单》
    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
      

  4.   

    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!
      

  5.   

    改一下不就成了吗?
    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
      

  6.   

    一点失误改正:
    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