Private WithEvents fw As IO.FileSystemWatcher
創(chuàng)新互聯(lián)建站專注于企業(yè)營(yíng)銷型網(wǎng)站建設(shè)、網(wǎng)站重做改版、船營(yíng)網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、HTML5建站、商城開(kāi)發(fā)、集團(tuán)公司官網(wǎng)建設(shè)、外貿(mào)網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁(yè)設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為船營(yíng)等各大城市提供網(wǎng)站開(kāi)發(fā)制作服務(wù)。
Private Shared Sub OnChanged(source As Object, e As IO.FileSystemEventArgs) Handles fw.Changed '更改指定路徑中的目錄和文件時(shí)引發(fā)該事件
MsgBox(e.Name)
End Sub
設(shè)計(jì)一個(gè)窗口,添加一個(gè)名為textBox1的System.Windows.Forms.TextBox,
添加一個(gè)名為button1的System.Windows.Forms.Button。
為button1的單擊事件添加如下處理函數(shù):
Sub Button1Click(sender As Object, e As EventArgs)
'f是你的文本文件的文件名
Const f As String="t.txt"
Dim sw As System.IO.StreamWriter=Nothing
Try
If Not System.IO.File.Exists(f) Then
sw=System.IO.File.CreateText(f)
Else
sw=New System.IO.StreamWriter(f,True)
End If
sw.WriteLine(textBox1.Text)
Finally
If sw IsNot Nothing Then
sw.close()
End If
End Try
End Sub
1.新建一張表格,在表格中導(dǎo)入學(xué)生的成績(jī)表,如圖所示,將建立一個(gè)按鈕,通過(guò)它來(lái)實(shí)現(xiàn)查找。
2.單擊菜單欄中的“開(kāi)發(fā)工具”——插入——表單控件——按鈕,在出現(xiàn)的十字箭頭上拖住畫出一個(gè)按鈕,如圖所示。
3.在彈出的查找紅對(duì)話框中選擇“錄制”,在彈出的“錄制新宏”對(duì)話框中,修改宏名稱為“查找”,單擊確定。
4. 單擊“開(kāi)發(fā)工具”——查看代碼,打開(kāi)VBA編輯器,如圖所示。
5.現(xiàn)在輸入代碼:
Sub 查找()
Dim jieguo As String, p As String, q As String
Dim c As Range
jieguo = Application.InputBox(prompt:="請(qǐng)輸入要查找的值:", Title:="查找", Type:=2)
If jieguo = "False" Or jieguo = "" Then Exit Sub
Application.ScreenUpdating = False
Application.DisplayAlerts = False
With ActiveSheet.Cells
? Set c = .Find(jieguo, , , xlWhole, xlByColumns, xlNext, False)
? If Not c Is Nothing Then
? ? ? p = c.Address
? ? ? Do
? ? ? ? ? c.Interior.ColorIndex = 4
? ? ? ? ? q = q c.Address vbCrLf
? ? ? ? ? Set c = .FindNext(c)
? ? ? Loop While Not c Is Nothing And c.Address p
? End If
End With
MsgBox "查找數(shù)據(jù)在以下單元格中:" vbCrLf vbCrLf _
? q, vbInformation + vbOKOnly, "查找結(jié)果"
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
6.現(xiàn)在回到EXCEL表格,右擊按鈕,選擇“編輯文字”,修改按鈕名稱為“查找按鈕”。
7. 現(xiàn)在單擊查找按鈕,出現(xiàn)對(duì)話框“請(qǐng)輸入要查找的值”,在方框中輸入“男”,單擊確定,出現(xiàn)對(duì)話框“查找數(shù)據(jù)在一下單元格中”,單擊確定,對(duì)應(yīng)單元格就變成了亮色。
獲取方法,參考實(shí)例如下:
'獲取路徑名各部分: 如: c:\dir1001\aaa.txt
'獲取路徑路徑 c:\dir1001\
Public Function GetFileName(FilePathFileName As String) As String '獲取文件名 aaa.txt
On Error Resume Next
Dim i As Integer, J As Integer
i Len(FilePathFileName)
J InStrRev(FilePathFileName, "\")
GetFileName Mid(FilePathFileName, J + 1, i)
End Function
''獲取路徑路徑 c:\dir1001\
Public Function GetFilePath(FilePathFileName As String) As String '獲取路徑路徑 c:\dir1001\
On Error Resume Next
Dim J As Integer
J InStrRev(FilePathFileName, "\")
GetFilePath Mid(FilePathFileName, 1, J)
End Function
'獲取文件名但不包括擴(kuò)展名 aaa
Public Function GetFileNameNoExt(FilePathFileName As String) As String '獲取文件名但不包括擴(kuò)展名 aaa
On Error Resume Next
Dim i As Integer, J As Integer, k As Integer
i Len(FilePathFileName)
J InStrRev(FilePathFileName, "\")
k InStrRev(FilePathFileName, ".")
If k 0 Then
GetFileNameNoExt Mid(FilePathFileName, J + 1, i - J)
Else
GetFileNameNoExt Mid(FilePathFileName, J + 1, k - J - 1)
End If
End Function
'===== '獲取擴(kuò)展名 .txt
Public Function GetFileExtName(FilePathFileName As String) As String '獲取擴(kuò)展名 .txt
On Error Resume Next
Dim i As Integer, J As Integer
i Len(FilePathFileName)
J InStrRev(FilePathFileName, ".")
If J 0 Then
GetFileExtName ".txt"
Else
GetFileExtName Mid(FilePathFileName, J, i)
End If
End Function
說(shuō)明:以下代碼在Microsoft Visual Basic 2005 (簡(jiǎn)體中文版)中通過(guò)。
創(chuàng)建新項(xiàng)目:
在窗體上添加文本框2個(gè):TextBox1,TextBox2
TextBox1 -- 用來(lái)編輯要寫入的文本文件的內(nèi)容,或顯示打開(kāi)的文本文件的內(nèi)容
TextBox2 -- 用來(lái)輸入要打開(kāi)或要寫入的文件名(包括盤符,路徑)(例如:c:\123.txt)
在窗體上添加2個(gè)按鈕:Button1,Button2
Button1 -- 寫入文件
Button2 -- 打開(kāi)文件
代碼如下:
Imports System.IO
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim w As New StreamWriter(TextBox2.Text)
w.Write(TextBox1.Text)
w.Close()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim r As New StreamReader(TextBox2.Text)
Dim s As String
TextBox1.Text = ""
Do While r.Peek -1 '是否到文件尾
s = r.ReadLine
' MessageBox.Show(r.Peek)
TextBox1.Text = TextBox1.Text s vbCrLf
Loop
r.Close()
End Sub
End Class
補(bǔ)充:你要把讀出的數(shù)據(jù)賦值給一個(gè)變量,只要:聲明一個(gè)變量為數(shù)值類型,然后只要讀取一行就可以了,把這行數(shù)據(jù)經(jīng)過(guò)轉(zhuǎn)換成數(shù)值后賦給這個(gè)變量.
Function ByteToBin(m As Byte) As String ' 將字節(jié)型數(shù)據(jù)轉(zhuǎn)換成八位二進(jìn)制字符串
Dim c$
c$ = ""
Do While m 0
r = m Mod 2
m = m \ 2
c$ = r c$
Loop
c$ = Right("00000000" c$, 8)
ByteToBin = c$
End Function
Function Reverse(m As String) As String ' 將八位二進(jìn)制字符串顛倒順序
Dim i%, x$
x = ""
For i = 1 To 8
x = Mid(m, i, 1) x
Next i
Reverse = x
End Function
Function BinToByte(m As String) As Byte ' 將八位二進(jìn)制串轉(zhuǎn)換成十進(jìn)制
Dim x As String * 1, y%, z%
z = 0
For i = 1 To 8
x = Mid(m, i, 1)
y = x * 2 ^ (8 - i)
z = z + y
Next i
BinToByte = z
End Function
Private Sub Command1_Click()
Dim x As Byte, i%, fname$
fname = InputBox("請(qǐng)輸入要加密的文件名!注意加上路徑名:")
If Dir(fname) = "" Then
MsgBox "文件不存在!"
Exit Sub
End If
Open fname For Binary As #1 ' 以二進(jìn)制訪問(wèn)模式打開(kāi)待加密文件
For i = 1 To LOF(1) ' LOF函數(shù)是求文件長(zhǎng)度的內(nèi)部函數(shù)
Get #1, i, x ' 取出第i個(gè)字節(jié)
x = BinToByte(Reverse(ByteToBin(x))) ' 這里調(diào)用了三個(gè)自定義函數(shù)
Put #1, i, x ' 將加密后的這個(gè)字節(jié)寫回到文件原位置
Next i
Close
MsgBox "完成!"
End Sub
退出:Unload Me
網(wǎng)上幫你找的,如果不符合你的要求,你可以再去網(wǎng)上在搜搜,有很多代碼的。