vb中如何读取textbox中指定的一行
http://zhidao.baidu.com/question/35001653.html?si=2http://zhidao.baidu.com/question/4...
http://zhidao.baidu.com/question/35001653.html?si=2
http://zhidao.baidu.com/question/41310393.html?si=1
中可能有关,但看不懂啊,新手,呵呵
愿详解
愿主席运营官 秋色烽火 详解每一句代码的作用,不胜感激! 展开
http://zhidao.baidu.com/question/41310393.html?si=1
中可能有关,但看不懂啊,新手,呵呵
愿详解
愿主席运营官 秋色烽火 详解每一句代码的作用,不胜感激! 展开
展开全部
TextBox 是以 vbCr+vbLf 为分行符号, 如果我们要逐一读取 TextBox 每一行, 无非是寻找 vbCr+vbLf 的所在位置, 然后取出每一行的字串, 不过这个方法不快,而且如果我们要读取第 N 行数据,还是要从第 1、2、…N-1 行逐一读起,实在麻烦。 还好 Windows API 提供有读取 TextBox 第 N 行的功能, 细节如下:
1、API 的声明:
Public Const EM_GETLINE = &HC4
Public Const EM_LINELENGTH = &HC1
Public 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
Private Declare Sub RtlMoveMemory Lib "KERNEL32" (lpvDest As Any, lpvSource As Any, ByVal cbCopy As Long)
2. 程序范例:
Sub TB_GetLine(ByVal hWnd As Long, ByVal whichLine As Long, Line As String)
Dim length As Long, bArr() As Byte, bArr2() As Byte, lc As Long
lc = SendMessage(hWnd, EM_LINEINDEX, whichLine, ByVal 0&)
length = SendMessage(hWnd, EM_LINELENGTH, lc, ByVal 0&)
If length > 0 Then
ReDim bArr(length + 1) As Byte,bArr2(length - 1) As Byte
Call RtlMoveMemory(bArr(0), length, 2) '准备一个存储器,传递消息之前先在存储器的前两个字节填入存储器的长度。
Call SendMessage(hWnd, EM_GETLINE,whichLine, bArr(0))
Call RtlMoveMemory(bArr2(0), bArr(0), length)
Line = StrConv(bArr2, vbUnicode)
Else
Line = ""
End If
End Sub
' 假设要读取 Text1 第 5 行的数据
Dim S As String
Call TB_GetLine( Text1.hWnd, 5, S )
' 传回值 S 即等于第 5 行的数据
( 注:TextBox 的行次是从 0 起算。)
站长在去掉有关bArr2的定义后,并将Line=Strconv(bArr2,vbUnicode)改为Line=StrConv(bArr,vbUnicode)发现程序照常运行,似乎bArr2在本程序中是多余的。不知哪位大虾能告知本人bArr2在这里的作用。
1、API 的声明:
Public Const EM_GETLINE = &HC4
Public Const EM_LINELENGTH = &HC1
Public 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
Private Declare Sub RtlMoveMemory Lib "KERNEL32" (lpvDest As Any, lpvSource As Any, ByVal cbCopy As Long)
2. 程序范例:
Sub TB_GetLine(ByVal hWnd As Long, ByVal whichLine As Long, Line As String)
Dim length As Long, bArr() As Byte, bArr2() As Byte, lc As Long
lc = SendMessage(hWnd, EM_LINEINDEX, whichLine, ByVal 0&)
length = SendMessage(hWnd, EM_LINELENGTH, lc, ByVal 0&)
If length > 0 Then
ReDim bArr(length + 1) As Byte,bArr2(length - 1) As Byte
Call RtlMoveMemory(bArr(0), length, 2) '准备一个存储器,传递消息之前先在存储器的前两个字节填入存储器的长度。
Call SendMessage(hWnd, EM_GETLINE,whichLine, bArr(0))
Call RtlMoveMemory(bArr2(0), bArr(0), length)
Line = StrConv(bArr2, vbUnicode)
Else
Line = ""
End If
End Sub
' 假设要读取 Text1 第 5 行的数据
Dim S As String
Call TB_GetLine( Text1.hWnd, 5, S )
' 传回值 S 即等于第 5 行的数据
( 注:TextBox 的行次是从 0 起算。)
站长在去掉有关bArr2的定义后,并将Line=Strconv(bArr2,vbUnicode)改为Line=StrConv(bArr,vbUnicode)发现程序照常运行,似乎bArr2在本程序中是多余的。不知哪位大虾能告知本人bArr2在这里的作用。
参考资料: 来源网络
2015-08-03 · 知道合伙人互联网行家
关注
展开全部
以下在Form需一个TextBox,并设定MultiLine = True, 一个Command Button
Private Sub Command1_Click()
Dim str5 As String
str5 = GetaLine(Text1, 1) '取得第二行的字串,以0为基底
End Sub
'以下在.Bas
Option Explicit
Const EM_GETLINE = &HC4
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 Function GetaLine(Text1 As TextBox, ByVal ntx As Long) As String
Dim str5(255) As Byte '如果您的字串 > 255 byte请自行增加该Byte Array
Dim str6 As String, i As Long
str5(0) = 255 '字串的前两个Byte存该字串的最大长度
str5(0) = 255
i = SendMessage(Text1.hwnd, EM_GETLINE, ntx, str5(0))
If i = 0 Then
GetaLine = ""
Else
str6 = StrConv(str5, vbUnicode)
GetaLine = Left(str6, InStr(1, str6, Chr(0)) - 1)
End If
End Function
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询