导航

« photoshop 7 绿色版 (可保存为web格式)seo常用工具网址  »

vb.net 文本查找替换

虚位以待 VB.NET

从网上收集的vb.net文本查找替换代码,效果还可以,可以按回车查找下一个,以下代码是从实例程序中直接复制过来的,想必知道vb.net的朋友都可以看懂,主要是看思路。

折叠ASP/Visual Basic Code复制内容到剪贴板
'==============================================================   
  1. '小牛制作2008   
  2. '===========================================   
  3. 'Design calf 流星记事本   
  4. 'homepage:www.spersky.com   
  5.   
  6. '==============================================================   
  7. Public Class findreplace   
  8.     Public kaishi As Integer  
  9.     Public pos As Integer  
  10.     Public shou As String  
  11.     '窗体加载   
  12.     Private Sub findreplace_Load(ByVal sender As ObjectByVal e As System.EventArgs) Handles MyBase.Load     
  13.   
  14.         Dim tempRect As New Rectangle(frmBiJi.Left + (frmBiJi.Width - Me.Width) / 2, frmBiJi.Top + (frmBiJi.Height - Me.Height) / 2, 369, 173)   
  15.         '  Set the bounds of the form using the Rectangle object.   
  16.         DesktopBounds = tempRect   
  17.   
  18.         If shou <> "" Then  
  19.             txtsearch.Text = shou   
  20.         End If  
  21.         txtsearch.Focus()   
  22.     End Sub  
  23.   
  24.   
  25.     '不给关闭,让窗体隐藏,以便下次调用   
  26.     Private Sub findreplace_Closing(ByVal sender As ObjectByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing   
  27.         e.Cancel = True  
  28.         Me.Hide()   
  29.     End Sub  
  30.     '当按回车时,执行查找下一个功能   
  31.     Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean  
  32.         Dim WM_KEYDOWN As Integer = 256   
  33.         Dim WM_SYSKEYDOWN As Integer = 260   
  34.         If btnreplace.Focused Then  
  35.             Exit Function  
  36.         End If  
  37.         If ((msg.Msg = WM_KEYDOWN) Or (msg.Msg = WM_SYSKEYDOWN)) Then  
  38.   
  39.             Select Case keyData   
  40.   
  41.                 Case Keys.Enter   
  42.                     '截获到回车键   
  43.                     Call xiayige()   
  44.                     Return False  
  45.             End Select  
  46.   
  47.         End If  
  48.   
  49.     End Function  
  50.     '实现查找下一个的过程代码   
  51.     Private Sub xiayige()   
  52.         If ckbupgo.Checked = False Then   '如果选中了"向下"   
  53.             kaishi = frmBiJi.rtbBj.SelectionStart + frmBiJi.rtbBj.SelectionLength + 1   
  54.             shou = txtsearch.Text   
  55.             If ckbcase.Checked = True Then  '如果选中了"区分大小写"   
  56.                 pos = InStr(kaishi, frmBiJi.rtbBj.Text, shou)   
  57.             Else                              '如果没有选中"区分大小写"   
  58.                 pos = InStr(kaishi, frmBiJi.rtbBj.Text.ToLower, shou.ToLower, CompareMethod.Text)   
  59.             End If  
  60.             If pos > 0 Then                   '如果找到内容   
  61.                 frmBiJi.rtbBj.Select(pos - 1, Len(shou))   
  62.                 'frmBiJi.rtbbj.Focus()   
  63.                 lbl1.Text = ""  
  64.             Else  
  65.                 ' MsgBox("找不到 " & """" & shou & """", MsgBoxStyle.Information)   
  66.                 lbl1.Text = "找不到 " & """" & shou & """"  
  67.             End If  
  68.         Else                                 '如果选中了"向上"   
  69.             kaishi = frmBiJi.rtbBj.SelectionStart   
  70.             shou = txtsearch.Text   
  71.             If ckbcase.Checked = True Then  '如果选中了"区分大小写"   
  72.                 pos = InStrRev(frmBiJi.rtbBj.Text, shou, kaishi)   
  73.             Else                              '如果没有选中"区分大小写"   
  74.                 pos = InStrRev(frmBiJi.rtbBj.Text.ToLower, shou.ToLower, kaishi)   
  75.             End If  
  76.             If pos > 0 Then                  '如果找到内容   
  77.                 frmBiJi.rtbBj.Select(pos - 1, Len(shou))   
  78.                 'frmBiJi.rtbbj.Focus()   
  79.                 lbl1.Text = ""  
  80.             Else  
  81.                 'MsgBox("找不到 " & """" & shou & """", MsgBoxStyle.Information)   
  82.                 lbl1.Text = "找不到 " & """" & shou & """"  
  83.             End If  
  84.         End If  
  85.     End Sub  
  86.   
  87.     Private Sub btnreplace_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles btnreplace.Click   
  88.         If frmBiJi.rtbBj.SelectedText <> "" Then  
  89.             frmBiJi.rtbBj.SelectedText = frmBiJi.rtbBj.SelectedText.Replace(frmBiJi.rtbBj.SelectedText, txtreplace.Text)   
  90.             kaishi = kaishi + Len(txtreplace.Text)   
  91.         End If  
  92.         '替换后查找下一个   
  93.         Call xiayige()   
  94.     End Sub  
  95.   
  96.     Private Sub btnreplaceall_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles btnreplaceall.Click   
  97.         shou = txtsearch.Text   
  98.         frmBiJi.rtbBj.Text = frmBiJi.rtbBj.Text.Replace(shou, txtreplace.Text)   
  99.     End Sub  
  100.   
  101.     Private Sub btnfind_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles btnfind.Click   
  102.         'frmBiJi.rtbbj.Focus()   
  103.         Call xiayige()   
  104.   
  105.     End Sub  
  106.   
  107.     Private Sub btnclose_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles btnclose.Click   
  108.         Me.Close()   
  109.   
  110.     End Sub  
  111.   
  112.   
  113.     'Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)   
  114.     '    If Keys.Enter = Keys.Enter Then   
  115.     '        Call xiayige()   
  116.     '    End If   
  117.     'End Sub   
  118.   
  119. End Class  

 

  • 顶一下
虚位以待




原创文章如转载,请注明:转载自落日故乡

本文链接地址:http://www.spersky.com/post/356.html

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

内容搜索


虚位以待

常用链接

最新评论及回复

Powered By Z-Blog

Copyright spersky All Rights Reserved.Theme Moonlight,Calf modified.n 浙ICP备16017820号-1