当前位置:编程学习 > VB >>

怎么用VB制作一个搜索程序

                              在Text1中输入要搜索的关键字       不点击command  在Text2中出现搜索到的内容                   搜索到在内容原来在D:\123.txt里   直接做出来的给我成品和代码都可以                       最好发下代码和成品

补充:3楼的  假如  我要找“千里走单骑是关羽”   我在Text1输入“千里”    Text2要让他出现这整句话
		
追问:我试试啊

答案:添加command1.command2.list1.text1         然后复制下面代码'窗体中>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 
Option Explicit
Dim PicHeight%, hLB&, FileSpec$, UseFileSpec%
Dim TotalDirs%, TotalFiles%, Running%
Dim WFD As WIN32_FIND_DATA, hItem&, hFile&
Const vbBackslash = "\"
Const vbAllFiles = "*.*"
Const vbKeyDot = 46

Private Sub Form_Load()
hLB& = List1.hwnd
SendMessage hLB&, LB_INITSTORAGE, 30000&, ByVal 30000& * 200
End Sub
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = vbKeyEscape And Running% Then Running% = False
End Sub

Private Sub Form_Unload(Cancel As Integer)
    Set Form1 = Nothing
    End
End Sub
Private Sub command1_Click()
    If Running% Then: Running% = False: Exit Sub
    Dim drvbitmask&, maxpwr%, pwr%
    On Error Resume Next
    FileSpec$ = InputBox("填入文件名:可以使用通配符 * 与 ?搜查文件,如*.exe")
    If Len(FileSpec$) = 0 Then Exit Sub
    MousePointer = 11
    Running% = True
    UseFileSpec% = True
    Command1.Caption = "停止!"
    Command2.Enabled = False
    List1.Clear
    drvbitmask& = GetLogicalDrives()
    If drvbitmask& Then
        maxpwr% = Int(Log(drvbitmask&) / Log(2))
        For pwr% = 0 To maxpwr%
            If Running% And (2 ^ pwr% And drvbitmask&) Then _
                Call SearchDirs(Chr$(vbKeyA + pwr%) & ":\")
        Next
    End If
    Running% = False
    UseFileSpec% = False
    Command1.Caption = "&Find File(s)..."
    Command2.Enabled = True
    MousePointer = 0
    Text1.Text = "Find File(s): " & List1.ListCount & " items found matching " & """" & FileSpec$ & """"
    Beep
End Sub
Private Sub command2_Click()
    If Running% Then: Running% = False: Exit Sub
    Dim searchpath$
    On Error Resume Next
    searchpath$ = InputBox("填入文件夹:", "设置文件夹", "C:\")
    If Len(searchpath$) < 2 Then Exit Sub
    If Mid$(searchpath$, 2, 1) <> ":" Then Exit Sub
    If Right$(searchpath$, 1) <> vbBackslash Then searchpath$ = searchpath$ & vbBackslash
    If FindClose(FindFirstFile(searchpath$ & vbAllFiles, WFD)) = False Then
        MsgBox searchpath$, vbInformation, "Path is invalid": Exit Sub
    End If
    MousePointer = 11
    Running% = True
    Command2.Caption = "停止(&S)"
    Command1.Enabled = False
    List1.Clear
    TotalDirs% = 0
    TotalFiles% = 0
    Call SearchDirs(searchpath$)
    Running% = False
    Command2.Caption = "设置文件夹(&F)..."
    Command1.Enabled = True
    MousePointer = 0
    MsgBox "文件夹: " & TotalDirs% & vbCrLf & _
                 "文件: " & TotalFiles%, , _
                 "文件夹设置: " & searchpath$
    
End Sub

Private Sub SearchDirs(curpath$)
    Dim dirs%, dirbuf$(), i%
    Text1.Text = "Searching " & curpath$
    DoEvents
    If Not Running% Then Exit Sub
    hItem& = FindFirstFile(curpath$ & vbAllFiles, WFD)
    If hItem& <> INVALID_HANDLE_VALUE Then
        Do
            If (WFD.dwFileAttributes And vbDirectory) Then
                If Asc(WFD.cFileName) <> vbKeyDot Then
                    TotalDirs% = TotalDirs% + 1
                    If (dirs% Mod 10) = 0 Then ReDim Preserve dirbuf$(dirs% + 10)
                    dirs% = dirs% + 1
                    dirbuf$(dirs%) = Left$(WFD.cFileName, InStr(WFD.cFileName, vbNullChar) - 1)
                End If
            
            ElseIf Not UseFileSpec% Then
                TotalFiles% = TotalFiles% + 1
            End If
        
        Loop While FindNextFile(hItem&, WFD)
        
        Call FindClose(hItem&)
    
    End If
    If UseFileSpec% Then
        SendMessage hLB&, WM_SETREDRAW, 0, 0
        Call SearchFileSpec(curpath$)
        SendMessage hLB&, WM_VSCROLL, SB_BOTTOM, 0
        SendMessage hLB&, WM_SETREDRAW, 1, 0
    End If
    For i% = 1 To dirs%: SearchDirs curpath$ & dirbuf$(i%) & vbBackslash: Next i%
End Sub
Private Sub SearchFileSpec(curpath$)
    hFile& = FindFirstFile(curpath$ & FileSpec$, WFD)
    If hFile& <> INVALID_HANDLE_VALUE Then
        Do
            DoEvents
            If Not Running% Then Exit Sub
            SendMessage hLB&, LB_ADDSTRING, 0, _
 &nbs

上一个:如何使用VB制作自动登陆器?
下一个:一VB题,急需答案!!

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,