选择显示字体大小

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

文本框属性为允许多行显示时,由于是软回车实现的分行,无法用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   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons