文本框属性为允许多行显示时,由于是软回车实现的分行,无法用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 getl.netext(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
getl.netext = string(lc + 2, 0)
mid(getl.netext, 1, 1) = chr(lc and &hff)
mid(getl.netext, 2, 1) = chr(lc \ &h100)
lc = sendmessagebystring(txtbox.hwnd, em_getline, lineindex, getl.netext)
getl.netext = left(getl.netext, 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 & "行:" & getl.netext(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
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器