• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: | 推荐给好友 上一篇 | 下一篇

如何取得文本框中包含指定字符串的行

发布: 2007-7-01 21:48 | 作者: admin | 来源: | 查看: 15次 | 进入软件测试论坛讨论

领测软件测试网

      文本框属性为允许多行显示时,由于是软回车实现的分行,无法用SPLIT(TEXT1.TEXT,VBCRLF)准确地取出指定行的内容。本文利用SENDMESSAGE 系列函数,通过发送文本框消息,实现了获取包含指定字符串的行,并演示了如何获取文本框中文本总行数和任意指定行的文本内容。

´add a textbox with "multiline=true","scrollbars=2".

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 Function SendMessageByNum Lib "USER32" _
    Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, _
    ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare Function SendMessageByString Lib "USER32" Alias _
    "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam _
     As Long, ByVal lParam As String) As Long

Private Const EM_LINEINDEX = &HBB
Private Const EM_GETLINECOUNT = &HBA
Private Const EM_GETLINE = &HC4
Private Const EM_LINELENGTH = &HC1

Function GetLineText(ByVal txtbox As TextBox, ByVal LineIndex As Long) As String ´返回指定行的内容
  Dim lc As Long, linechar As Long
  linechar = SendMessageByNum(txtbox.hWnd, EM_LINEINDEX, LineIndex, 0)
  lc = SendMessageByNum(txtbox.hWnd, EM_LINELENGTH, linechar, 0) + 1
  GetLineText = String(lc + 2, 0)
  Mid(GetLineText, 1, 1) = Chr(lc And &HFF)
  Mid(GetLineText, 2, 1) = Chr(lc \ &H100)
  lc = SendMessageByString(txtbox.hWnd, EM_GETLINE, LineIndex, GetLineText)
  GetLineText = Left(GetLineText, lc)
End Function

 

Function getlinewithstr(ByVal txtbox As TextBox, ByVal mystr As String) As String
Dim linecount As Long, temp() As String, i As Long
linecount = SendMessage(txtbox.hWnd, EM_GETLINECOUNT, 0, 0) ´返回行数
ReDim temp(1 To linecount)
For i = 1 To linecount
temp(i) = "第" & i & "行:" & GetLineText(txtbox, i - 1) ´添加行号
Next
getlinewithstr = Join(Filter(temp, mystr), vbCrLf) ´ 字符串过滤
Erase temp
End Function

Private Sub Command1_Click()
MsgBox getlinewithstr(Text1, "CSDN"), 0, "包含“CSDN”的行"
End Sub

Private Sub Form_Load()
Dim a(25) As String, i As Long
For i = 0 To 25
a(i) = String(50, Chr(i + 97))
Next
Text1.Text = Join(a, "CSDN")

End Sub


延伸阅读

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备10010545号-5
技术支持和业务联系:info@testage.com.cn 电话:010-51297073

软件测试 | 领测国际ISTQBISTQB官网TMMiTMMi认证国际软件测试工程师认证领测软件测试网