vb中如何判断text文件中光标的位置,并在光标所在行的下一行插入一行数
text文件中有多行数字,每行数字的格式都一样,我想把光标随即放到某行的后边,然后再此行后边再插入一行数字该如何实现。eg:1111222233334444若将鼠标放到3...
text文件中有多行数字,每行数字的格式都一样,我想把光标随即放到某行的后边,然后再此行后边再插入一行数字该如何实现。
eg:11 11
22 22
33 33
44 44
若将鼠标放到33 33后边,则应该在33 33与44 44之间插入一行,应该如何实现 展开
eg:11 11
22 22
33 33
44 44
若将鼠标放到33 33后边,则应该在33 33与44 44之间插入一行,应该如何实现 展开
4个回答
展开全部
反正我只知道可以通过api函数,知道文本框中的光标位于第几行第几列,用法如下:
Private Const EM_GETSEL = &HB0
Private Const EM_LINEFROMCHAR = &HC9
Private 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
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
P = j
'向文本框传递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
Private Const EM_GETSEL = &HB0
Private Const EM_LINEFROMCHAR = &HC9
Private 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
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
P = j
'向文本框传递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
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
反正我只知道可以通过api函数,知道文本框中的光标位于第几行第几列,用法如下:
Private Const EM_GETSEL = &HB0
Private Const EM_LINEFROMCHAR = &HC9
Private 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
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
P = j
Private Const EM_GETSEL = &HB0
Private Const EM_LINEFROMCHAR = &HC9
Private 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
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
P = j
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Private Sub Text1_Click()
If Text1.SelStart = 0 Then Text1.SelStart = 1
p = InStr(Text1.SelStart, Text1, vbCrLf)
If p > 0 Then
Text1 = Left(Text1, p - 1) & vbCrLf & vbCrLf & Mid(Text1, p + 1)
Text1.SelStart = p + 1
Text1.SetFocus
End If
End Sub
Private Sub Text1_GotFocus()
Text1_Click
End Sub
If Text1.SelStart = 0 Then Text1.SelStart = 1
p = InStr(Text1.SelStart, Text1, vbCrLf)
If p > 0 Then
Text1 = Left(Text1, p - 1) & vbCrLf & vbCrLf & Mid(Text1, p + 1)
Text1.SelStart = p + 1
Text1.SetFocus
End If
End Sub
Private Sub Text1_GotFocus()
Text1_Click
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用text1.selstart属性
追问
我想用vb做一个把plc程序转化为数字代码的界面,plc程序是通过界面上的按钮输入进去的,现在碰到的问题是,想插入一行的时候,通过按钮根本就插不进去,这该怎么办?
追答
不知道你的pic程序是干什么用的,不过你说不能插入一行,我想这个还是可以解决的。你试试用split函数将你的多行文本分离成数组,然后在数组中插入你要加入的文本,然后再用join函数拼到一块行不行
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询