VB怎样实现按方向键,光标在控件TEXT1和TEXT2之间移动
当在文本框TEXT1控件里有输入字符时候光标在字符间移动当文本框没有字符时光标在文本框控件text1和text2之间移动请问具体如何实现给思路或给代码都行还有说明一下,以...
当在文本框TEXT1控件里有输入字符时候
光标在字符间移动
当文本框没有字符时
光标在文本框控件text1和text2之间移动
请问具体如何实现
给思路或给代码都行
还有说明一下,以下代码是无法实现上述功能的
光标只会在字符间移动,移动到TEXT2就是不行
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = 39 Then
Text2.SetFocus
End If
End Sub 展开
光标在字符间移动
当文本框没有字符时
光标在文本框控件text1和text2之间移动
请问具体如何实现
给思路或给代码都行
还有说明一下,以下代码是无法实现上述功能的
光标只会在字符间移动,移动到TEXT2就是不行
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = 39 Then
Text2.SetFocus
End If
End Sub 展开
3个回答
展开全部
以下代码是用文本框实现的,RichTextBox同样处理。
vb '获得光标所在的行和列
' 一般的比较完善的文本编辑器一般都有在状态栏中显示当前光标所在行和列的功能?利用SendMessage向TextBox
'控件发送编辑控件类型消息。也可以实现这样的功能。下面首先来看程序,然后再分析。
' 首先在VB中建立一个新工程,并在Form1中加入一个TextBox控件和两个Label控件。将TextBox控件的MultiLine
'属性设置为True?然后在Form1的代码窗口中加入如下代码:
Option Explicit
Private Declare Function SendMessage Lib "user32" Alias "SendMessageW" _
(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _
lParam As Any) As Long
Private Declare Function SendMessageByRef Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Long, ByVal wMsg As Long, wParam As Long, _
lParam As Long) As Long
Const EM_LINEFROMCHAR = &HC9
Const EM_LINEINDEX = &HBB
Const EM_GETLINE = &HC4
Const EM_GETSEL = &HB0
Dim iLineX, iLineY As Long
Sub GetCurPos(txtA As TextBox)
Dim l, l1, l2 As Long
Dim astr As String * 256
l = SendMessage(txtA.hwnd, EM_LINEINDEX, -1, 0)
iLineY = SendMessage(txtA.hwnd, EM_LINEFROMCHAR, l, 0)
SendMessageByRef txtA.hwnd, EM_GETSEL, l1, l2
iLineX = l1 - l
Label1.Caption = "列:" + Str(iLineX)
Label2.Caption = "行:" + Str(iLineY)
End Sub
Private Sub Form_Load()
Dim strTest As String
Label1.Height = 300: Label2.Height = 300
Text1.Left = 0: Text1.Top = 0
Text1.Text = ""
Label1.Caption = ""
Label2.Caption = ""
strTest = "test,test,test"
Text1.Text = strTest & vbCrLf & strTest & vbCrLf & strTest
End Sub
Private Sub Form_Resize()
Label1.Top = Me.ScaleHeight - 300
Label2.Top = Me.ScaleHeight - 300
Label1.Left = 0: Label2.Left = 1200
Label1.Width = 1200
Label2.Width = 1200
Text1.Width = Me.ScaleWidth
Text1.Height = Me.ScaleHeight - Label1.Height
End Sub
Private Sub Text1_Click()
GetCurPos Text1
End Sub
Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
GetCurPos Text1
End Sub
---------------------------------------------------------------
Const EM_GETSEL = &HB0
Const EM_LINEFROMCHAR = &HC9
Const EM_LINEINDEX = &HBB
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'过程名:GetCaretPos
'功 能:取得文本框中光标的位置
'参 数: TextHwnd-文本框句柄
' LineNo-光标所在行号
' ColNo-光标所在列号
'返 回:无
'日 期:2001-2-10
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Sub GetCaretPos(ByVal TextHwnd As Long, LineNo As Long, ColNo As Long)
Dim I As Long, j As Long
Dim lParam As Long, wParam As Long
Dim k As Long
'首先向文本框传递EM_GETSEL消息以获取从起始位置到光标所在位置的字符数
I = SendMessage(TextHwnd, EM_GETSEL, wParam, lParam)
j = I / 2 ^ 16
'再向文本框传递EM_LINEFROMCHAR消息根据获得的字符数确定光标以获取所在行数
LineNo = SendMessage(TextHwnd, EM_LINEFROMCHAR, j, 0)
LineNo = LineNo + 1
'向文本框传递EM_LINEINDEX消息以获取所在列数
k = SendMessage(TextHwnd, EM_LINEINDEX, -1, 0)
ColNo = j - k + 1
End Sub
----------------------------------------------------------------
使用:
在richtextbo的鼠标,键盘事件,中写
Dim LineNo As Long, ColNo As Long
Call GetCaretPos(RichText1.hwnd, LineNo, ColNo)
Label1.Caption = LineNo '显示行
Label2.Caption = ColNo '显示列
vb '获得光标所在的行和列
' 一般的比较完善的文本编辑器一般都有在状态栏中显示当前光标所在行和列的功能?利用SendMessage向TextBox
'控件发送编辑控件类型消息。也可以实现这样的功能。下面首先来看程序,然后再分析。
' 首先在VB中建立一个新工程,并在Form1中加入一个TextBox控件和两个Label控件。将TextBox控件的MultiLine
'属性设置为True?然后在Form1的代码窗口中加入如下代码:
Option Explicit
Private Declare Function SendMessage Lib "user32" Alias "SendMessageW" _
(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _
lParam As Any) As Long
Private Declare Function SendMessageByRef Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Long, ByVal wMsg As Long, wParam As Long, _
lParam As Long) As Long
Const EM_LINEFROMCHAR = &HC9
Const EM_LINEINDEX = &HBB
Const EM_GETLINE = &HC4
Const EM_GETSEL = &HB0
Dim iLineX, iLineY As Long
Sub GetCurPos(txtA As TextBox)
Dim l, l1, l2 As Long
Dim astr As String * 256
l = SendMessage(txtA.hwnd, EM_LINEINDEX, -1, 0)
iLineY = SendMessage(txtA.hwnd, EM_LINEFROMCHAR, l, 0)
SendMessageByRef txtA.hwnd, EM_GETSEL, l1, l2
iLineX = l1 - l
Label1.Caption = "列:" + Str(iLineX)
Label2.Caption = "行:" + Str(iLineY)
End Sub
Private Sub Form_Load()
Dim strTest As String
Label1.Height = 300: Label2.Height = 300
Text1.Left = 0: Text1.Top = 0
Text1.Text = ""
Label1.Caption = ""
Label2.Caption = ""
strTest = "test,test,test"
Text1.Text = strTest & vbCrLf & strTest & vbCrLf & strTest
End Sub
Private Sub Form_Resize()
Label1.Top = Me.ScaleHeight - 300
Label2.Top = Me.ScaleHeight - 300
Label1.Left = 0: Label2.Left = 1200
Label1.Width = 1200
Label2.Width = 1200
Text1.Width = Me.ScaleWidth
Text1.Height = Me.ScaleHeight - Label1.Height
End Sub
Private Sub Text1_Click()
GetCurPos Text1
End Sub
Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
GetCurPos Text1
End Sub
---------------------------------------------------------------
Const EM_GETSEL = &HB0
Const EM_LINEFROMCHAR = &HC9
Const EM_LINEINDEX = &HBB
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'过程名:GetCaretPos
'功 能:取得文本框中光标的位置
'参 数: TextHwnd-文本框句柄
' LineNo-光标所在行号
' ColNo-光标所在列号
'返 回:无
'日 期:2001-2-10
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Sub GetCaretPos(ByVal TextHwnd As Long, LineNo As Long, ColNo As Long)
Dim I As Long, j As Long
Dim lParam As Long, wParam As Long
Dim k As Long
'首先向文本框传递EM_GETSEL消息以获取从起始位置到光标所在位置的字符数
I = SendMessage(TextHwnd, EM_GETSEL, wParam, lParam)
j = I / 2 ^ 16
'再向文本框传递EM_LINEFROMCHAR消息根据获得的字符数确定光标以获取所在行数
LineNo = SendMessage(TextHwnd, EM_LINEFROMCHAR, j, 0)
LineNo = LineNo + 1
'向文本框传递EM_LINEINDEX消息以获取所在列数
k = SendMessage(TextHwnd, EM_LINEINDEX, -1, 0)
ColNo = j - k + 1
End Sub
----------------------------------------------------------------
使用:
在richtextbo的鼠标,键盘事件,中写
Dim LineNo As Long, ColNo As Long
Call GetCaretPos(RichText1.hwnd, LineNo, ColNo)
Label1.Caption = LineNo '显示行
Label2.Caption = ColNo '显示列
参考资料: http://www.ebookcn.org/archives/vb/7f529e8baf5afecb564e7aa1c2376bbc.php
展开全部
何必用方向键呢?破坏了一些操作
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
If Text1.Text = "" And KeyCode = 39 Then
Text2.SetFocus
End If
End Sub
Private Sub Text2_KeyDown(KeyCode As Integer, Shift As Integer)
If Text2.Text = "" And KeyCode = 39 Then
Text1.SetFocus
End If
End Sub
If Text1.Text = "" And KeyCode = 39 Then
Text2.SetFocus
End If
End Sub
Private Sub Text2_KeyDown(KeyCode As Integer, Shift As Integer)
If Text2.Text = "" And KeyCode = 39 Then
Text1.SetFocus
End If
End Sub
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询