VB问题全功略(上)

来源:岁月联盟 编辑:zhu 时间:2007-02-01
  注释:下面的函数可以去掉文中多余的回车和空行,可以对付非常规的字符(以0Ah作为回车,而不是0Dh,0Ah)

  Private Function FormatStr(strReadyToFormat As String) As String

  Dim strTemp() As String

  Dim strReady As String

  Dim nPos As Long

  Dim i As Long

  On Error Resume Next

  Do

  DoEvents

  注释:有的文件以0Ah作为回车换行标志

  nPos = InStr(1, strReadyToFormat, Chr(10), vbBinaryCompare)

  注释:找到0AH后,表示准备另起一行,先将之前的字符0Dh取出(如果有的话),0Dh表示回车

  strReady = Left(strReadyToFormat, nPos - 1)

  注释:如果前面有0DH,全部去掉

  Do While Asc(Right(strReady, 1)) = 13

  strReady = Left(strReady, Len(strReady) - 1)

  If strReady = "" Then Exit Do

  Loop

  注释:检查是不是一个空行

  If Trim(strReady) <> "" Then

  注释:若是,则写入

  i = i + 1

  ReDim Preserve strTemp(i)

  strTemp(i) = strReady

  End If

  注释:去掉头部的字符串

  strReadyToFormat = Right(strReadyToFormat, Len(strReadyToFormat) - nPos)

  Loop Until nPos = 0 注释:继续向下找

  FormatStr = ""

  For i = 1 To UBound(strTemp)

  FormatStr = FormatStr + strTemp(i)

  Next

  End Function

  End Function  

  
    Dim TEXTlen As Integer

  Dim i As Integer

  Dim temp1 As String

  Dim temp2 As String

  Dim MyCreate As String

  Dim j As Integer

  Dim NextLine As String

  Command1.Enabled = False

  If List1.ListCount = 0 Then Exit Sub

  Form1.MousePointer = 11

  For j = 0 To List1.ListCount - 1

  Label2.Caption = "共 " & Str(List1.ListCount) & "个文件,正在修改第 " & Str(j + 1) & " 个文件。"

  注释:打开一个文件,input方式 #1

  Open List1.List(j) For Input As #1

  注释:打开一个文件,append文件 #2

  Open (App.Path & "/temp.tmp") For Append As #2

  Do Until EOF(1)

  注释:从#1读取一行

  Line Input #1, NextLine

  MyCreate = ""

  TEXTlen = Len(NextLine)

  For i = 1 To TEXTlen

  temp1 = Mid(NextLine, i, 1)

  If Asc(temp1) < 0 Then

  temp2 = Mid(NextLine, i + 1, 1)

  If temp2 <> " " Then

  temp1 = temp1 & " "

  End If

  End If

  MyCreate = MyCreate + temp1

  DoEvents

  Next

  Print #2, MyCreate

  注释:向#2写文件

  Loop

  Close #1

  Close #2

  FileCopy App.Path & "/temp.tmp", List1.List(j)

  Kill App.Path & "/temp.tmp"

  Next 注释:下一个文件

  Form1.MousePointer = 1

  MsgBox "文件已经成功修改!", vbOKOnly + vbInfoBackground, "恭喜!"

  
   If KeyCode = vbKeyF3 Then 注释:F3查找下一个

  注释:下面这个If块在查找下一个匹配字符时很有用

  If txtContext.SelStart = 0 Then 注释:光标位置在文本框最开头

  If txtContext.SelLength > 0 Then

  nPos = 2 注释:如果文本框中有被加亮的字符

  Else

  nPos = 1 注释:注释:如果文本框中没有被加亮的字符

  End If

  Else

  If txtContext.SelLength > 0 Then

  nPos = txtContext.SelStart + 2 注释:如果文本框中有被加亮的字符

  Else

  nPos = txtContext.SelStart + 1 注释:如果文本框中没有被加亮的字符

  End If

  End If

  nPos = InStr(nPos, txtContext.Text, FrmSearch.txtSearch.Text, vbTextCompare)

  If nPos = 0 Then Exit Sub 注释:nPos=0表示没有找到

  注释:加亮找到的字符串

  txtContext.SelStart = nPos - 1

  txtContext.SelLength = Len(FrmSearch.txtSearch.Text)  

  294、如何寻找并加亮找到的字符? 

  If KeyCode = vbKeyF3 Then 注释:F3查找下一个

  注释:下面这个If块在查找下一个匹配字符时很有用

  If txtContext.SelStart = 0 Then 注释:光标位置在文本框最开头

  If txtContext.SelLength > 0 Then

  nPos = 2 注释:如果文本框中有被加亮的字符

  Else

  nPos = 1 注释:注释:如果文本框中没有被加亮的字符

  End If

  Else

  If txtContext.SelLength > 0 Then

  nPos = txtContext.SelStart + 2 注释:如果文本框中有被加亮的字符

  Else

  nPos = txtContext.SelStart + 1 注释:如果文本框中没有被加亮的字符

  End If

  End If

  nPos = InStr(nPos, txtContext.Text, FrmSearch.txtSearch.Text, vbTextCompare)

  If nPos = 0 Then Exit Sub 注释:nPos=0表示没有找到

  注释:加亮找到的字符串

  txtContext.SelStart = nPos - 1

  txtContext.SelLength = Len(FrmSearch.txtSearch.Text)

  
   Public Function RemoveBackslash(s As String) As String

  Dim i As Integer

  i = Len(s)

  If i <> 0 Then

  If Right$(s, 1) = "/" Then

  RemoveBackslash = Left$(s, i - 1)

  Else

  RemoveBackslash = s

  End If

  Else

  RemoveBackslash = ""

  End If

  End Function  

  如何在程序中更改预设的打印机? 

  在上一个单元中,我们使用 Printer 物件来取得机器中预设打印机的机型、驱动程序及连接端口,那是否可以更改预设打印机呢?可以的,不过除了 Printer 物件之外,这次我们要多用一个 Printers 集合物件! 

  先来看一段程序码:  

  Private Sub Form_Load()

  注释:利用 Printers 集合物件取得所有打印机

  For i = 0 To Printers.Count - 1

  注释:在 Combo1 中依序加入打印机名称

  Combo1.AddItem Printers(i).DeviceName

  Next

  注释:将目前的预设打印机放在 Combo1.Text 中

  Combo1.Text = Printer.DeviceName

  End Sub  

  Private Sub Command1_Click()

  注释:未选择

  If Combo1.ListIndex = -1 Then

  MsgBox "打印机未更改,请先选择打印机!"

  Exit Sub

  End If

  注释:使用者选定之打印机设定为预设打印机

  Set Printer = Printers(Combo1.listindex)

  End Sub

  在这一段程序中,我们在 Form_Load 中先利用 Printers 集合物件找出所有的打印机名称,放在 Combo1 中,记得 Combo1 不可将 Sorted 设成 True,这样子的话,Combo1 的索引值 (Index) 便会和 Printers 集合物件的索引值 (Index) 相同!  

  使用者在 Combo1 中选定打印机后,在 Command1_Click 中,我们便可以将 Combo1 的索引值带入Printers 集合物件的索引值,重新设定新的预设打印机。  

  这样子的设定只有在程序中有效,程序结束后,预设打印机会恢复成原来的预设打印机!