进入旧版 | 服务项目 | 成功案例 | 联系方式 | 过客留言 | 友情链接
   
设为首页
加入收藏
联系我们
网站首页 | 新闻资讯 | 操作系统 | 办公软件 | 网络软件 | 工具软件 | 媒体动画 | 网页制作 | 网站开发 | 程序开发 | 平面设计
Photoshop视频教程 | Word入门 | Flash入门 | JScript | VBScript | ASP | PHP | ADO | 网页特效 | 3DS MAX6.0命令 | 系统进程
您当前的位置:GOODSGY电脑学习网 -> 程序开发 -> VB -> 文章内容  
VB应用程序中实现“查找和替换”功能

一、前言

  尽管VisualBasic并不是我最喜欢的开发工具,但我喜欢它简单而又丰富的库集。每当开发一个需要处理大量文本数据的应用程序时,需要具有拼写错误纠正功能,例如,微软的Word程序,当运行"拼写检查"时,将提供给你一个改正错误的机会(尽管是建议),它同时也提供了"查找替换"工具,用以进行用户定义的单词替换。这篇文章我将解释如何在VB应用程序中实现"查找替换"功能。

二、前提条件

  在解释代码的时候,我假定读者朋友们已经有使用VisualBasic的经验,熟悉VisualStudio开发环境中各种内置的控件及库函数(尽管我使用的不多)。我已经尝试着尽可能地简化程序代码,用不了多久你就可以明白程序的逻辑。如果想对一些库函数(如参数,语法)进一步详细地理解,可以参阅MSDN。图一是程序运行后的效果图:

->

三、基础工作

  首先创建一个标准的EXE类型的VB工程,将默认窗体更名为frmMainForm,在默认窗体上增添一个菜单,具体设置如下(符号"&"用于加速键,单词mnu后的名字用来说明菜单项的名字(在代码中使用)):

->&Edit
...&FindandReplacemnuFindandreplace
E&xitmnuExit->

  向默认窗体添加一个TextBox控件,命名为txtClientArea。使用鼠标调整控件位置和尺寸,使它覆盖窗体的整个客户区,在属性窗口将这个TextBox控件的MultiLine属性设置为"True"。

  使用Project>AddForm菜单向工程中添加另外一个窗体,将这个窗体命名为"frmFindReplace",并在属性窗口中将它的BorderStyle属性设置为"4-FixedToolWindow"。现在,添加两个TextBox控件,并分别命名为"txtSearchTerm"和"txtReplaceWithString"。添加一个复选框,命名为chkCaseSense。最后,添加一个命令按钮控件,命名为"cmdReplace"。

  在frmMainForm窗体中添加如下代码:

->PrivateSubmnuExit_Click()
 End
EndSub

PrivateSubmnuFindandreplace_Click()
 frmFindReplace.FindnReplacetxtClientArea
EndSub->

  从上面代码中可以非常明显地看出,当点击Exit菜单时,我们想终结应用程序,当点击"FindandReplace"菜单时,想通过共用接口frmFindReplace及FindnReplace()方法来激活frmFindReplace窗体。这个公用的接口使查找算法具有普遍性,使用这个接口时,需要提供一个TextBox作为参数(在这里面,搜寻将被执行)。通过使用你自己的TextBox的名字来代替txtClientArea的名字,可以在多个文本框内执行"查找替换"功能,而不用更改代码。"查找和替换"的实现代码主要是在frmFindReplace窗体内,这个模块的代码如下:

->'Thisvariableisusedformakingthealgorithmgeneric.
DimtxtClientAsTextBox

'ThismethodisthepublicinterfacetoSnRfunctionality.

PublicSubFindnReplace(ByRefTbAsTextBox)
 SettxtClient=Tb
 Me.Show,txtClient.Parent
EndSub

PrivateSubcmdReplace_Click()
 DimCaseSenseAsInteger
 DimSourceTextAsString
 DimSourceTextCopyAsString
 DimCntAsInteger

 'Checkforthecasesensitivityoptions
 If(chkCaseSense.Value=vbChecked)Then
  CaseSense=0
 Else
  CaseSense=1
 EndIf

 'Onecontainstheoriginaltextandanothercontainsreplaced
 '(updated)one.
 'Usedtocheckwhetherareplacementwasdoneornot.
 SourceText=txtClient.Text
 SourceTextCopy=SourceText

 IfLen(SourceText)=0Then
  ExitSub
 EndIf

 OnErrorGoToErrHandler
 DimSearchTermLenAsInteger
 DimFndPosAsInteger

 SearchTermLen=Len(txtSearchTerm.Text)
 'Searchfromthebeginingofthedocument.
 Cnt=1

 'Thisisendlessloop(terminatedonaconditioncheckedinside
 'theloopbody).
 While(1)

 FndPos=InStr(Cnt,SourceText,txtSearchTerm.Text,CaseSense)

 'Whenamatchisfound,replaceitappropriately.
 If(FndPos>0)Then
  SourceText=ReplaceFun(SourceText,FndPos,Len(txtSearchTerm.Text),txtReplaceWithString.Text)
  Cnt=FndPos SearchTermLen
 Else
  Cnt=Cnt 1
 EndIf

 'Whetherareplacementwasdoneatallornot
 If(Cnt>=Len(SourceText))Then
  txtClient.Text=SourceText
  If(SourceTextCopy<>SourceText)Then
   MsgBox"Finishedreplacingalloccurrences.",vbInformation vbOKOnly,"ReplacedAll"
  Else
   MsgBox"Nomatchingstringsfound.Notextreplaced.",vbInformation vbOKOnly,"NoReplacement"
  EndIf
  UnloadMe
  ExitSub
 EndIf
 'ElseRestartfromhenceforth
 Wend
 ExitSub

ErrHandler:
 Response=MsgBox("Anerrorocurredwhilesearching.Informthedeveloperwithdetails.",_
vbExclamation vbOKOnly,"ErrorSearching")
EndSub

PrivateSubForm_Load()
 'DefaultSearchTermmustbetheoneselectedbytheuserin
 'MainForm
 IfLen(txtClient.SelText)<>0Then
  txtSearchTerm.Text=txtClient.SelText
 EndIf
EndSub

FunctionReplaceFun(SourceAsString,FromPosAsInteger,_
LengthAsInteger,StringTBReplaced_
AsString)AsString
 'Replacesasourcestringwithnewoneappropriately
 DimResultStrAsString

 ResultStr=Left(Source,FromPos-1)
 ResultStr=ResultStr&StringTBReplaced
 ResultStr=ResultStr&Right(Source,Len(Source)-FromPos-Length 1)

 ReplaceFun=ResultStr
EndFunction

PrivateSubtxtReplaceWithString_Change()
 CallEnableDisableReplaceButton
EndSub

PrivateSubtxtReplaceWithString_GotFocus()
 'Selectthecontentsofthetextbox
 IfLen(txtReplaceWithString.Text)<>0Then
  txtReplaceWithString.SelStart=0
  txtReplaceWithString.SelLength=Len(txtReplaceWithString.Text)
 EndIf
EndSub

PrivateSubtxtSearchTerm_Change()
 CallEnableDisableReplaceButton
EndSub

PrivateSubEnableDisableReplaceButton()
 IfLen(txtSearchTerm.Text)<>0_
  AndLen(txtReplaceWithString.Text)<>0Then
  cmdReplace.Enabled=True
 Else
  cmdReplace.Enabled=False
 EndIf
EndSub

PrivateSubtxtSearchTerm_GotFocus()
 'Selectthecontentsoftextbox
 IfLen(txtSearchTerm.Text)<>0Then
  txtSearchTerm.SelStart=0
  txtSearchTerm.SelLength=Len(txtSearchTerm.Text)
 EndIf
EndSub->


www.goodsgy.com

[1] [2]  下一页

在百度中搜索:VB应用程序中实现“查找和替换”功能
在Google中搜索:VB应用程序中实现“查找和替换”功能
在Yahoo中搜索:VB应用程序中实现“查找和替换”功能

收藏到网摘:新浪VIVI 365key 我摘 POCO网摘 博采中心 YouNote 和讯网摘 天天收藏
[] [返回上一页] [打 印] [收 藏]

 相关文章    最新文章
· [图文] 利用“替换”功能来实现Word表格的..
· PowerPoint不用VBA也制作交互课件
· [图文] PowerPoint中用VBA制作课堂小测验
· 在Excel中利用VBA创建多级选单
· 普通用户Excel使用VBA的几个误区
· Excel中调用VBA选择目标文件夹
· EXCEL VBA中字符串查找并改变颜色
· Excel VBA技巧之快速删除所有名称
· 在Excel中用VBA实现定时提醒功能
· 近半Vista下杀毒软件未过VB100
 
· sql 的随机函数newID()和RAND()
· 知己知彼 了解VB编写病毒的大体方法
· 经验交流:关于软件设计的一点心得体会
· VB里怎么连续循环播放音乐
· 在VB中调用CHM帮助的几种方法
· VisualBasic变态用法之函数指针
· 在VB中用DAO实现数据库编程
· Data控件使用有admin888的Access数据库
· 教你在CoolBar中显示指定的图片
· VB中拖动没有标题栏的窗体

∷相关文章评论∷    (评论内容只代表网友观点,与本站立场无关!) [更多评论…]
站内搜索

精彩图文
  网站导航  
操作系统 办公软件 网络软件
Vista Windows2003 WindowsXP Windows2000/NT Windows9X/ME Linux 其他 Word Excel Powerpoint Outlook 金山系列 其他 网页浏览 上传下载 联络聊天 邮件工具 服务器软件 网络辅助
工具软件 媒体动画 网页制作
系统工具 媒体工具 压缩工具 图文处理 文件管理 其他 3DMAX Authorware Director Maya 视频处理 其他 Flash Dreamweaver FireWorks FrontPage LiveMotion Golive HTML/CSS 其它
网站开发 平面设计 程序设计
ASP JSP PHP CGI JavaScript VBScript XML/SOAP Web服务器 Photoshop PhotoImpact CorelDraw Illustrator Freehand 设计欣赏 其他 VB VC .NET C/C++ DELPHI JAVA

冀ICP备05019428号
Copyright © 2004-2008 电脑学习网 Inc.All rights reserved.
TEL:13832340607
QQ:39873155
E_Mail:goodsgy(#)hotmail.com   (把(#)替换成@)
MSN:goodsgy(#)hotmail.com   (把(#)替换成@)