Viết Code


thdang@tlnet.com.vn

[ Home Trang đầu ]  [ Thiết kế giao diện ]

Form1

Option Explicit

Khai báo các biến toàn cục cho chương trình

Public WTitle As String
'Tiêu đề của file HTML

Public BeginText As String
'Dòng văn bản xuất hiện ở đầu danh sách

Public EndText As String
'Dòng văn bản xuất hiện ở cuối danh sách

Public AlignText As String
'Canh lề các liên kết, gồm "Left", "Right", "Center"

Public Launch As Integer
'Xem file sau khi tạo xong. Gồm 3 giá trị: 1-Xem bằng IE. 2-Xem bằng Notepad. 3-Không xem file sau khi tạo xong

- Bắt đầu tạo file HTML theo đường dẫn từ lblPath & Text1 được lưu giữ bằng biến F. Mở file để ghi theo dạng TextOnly sẽ chứa các HTML tags theo cú pháp các liên kết (Link).

Do trong file HTML có sử dụng khá nhiều dấu nháy kép ("). Mà trong Visual Basic cũng có dùng, để tránh lẫn lộn ta nên dùng chr(34) thay cho dấu nháy kép.

Private Sub cmdCreate_Click()
    On Error GoTo ErrHandle
    Form1.MousePointer = 11
    Dim i As Integer
    Dim k
    Dim F As String
    F = lblPath.Caption & Trim(Text1)
        'Lấy đường dẫn và tên tập tin
    Open F For Output As #1
    Print #1, "<html>"
    Print #1, "<Title>"; WTitle; "</Title>"
    'Ghi tiêu đề của trang Web
    Print #1, "<body>"
    Print #1, "<p align="; Chr(34); AlignText; Chr(34); ">"
    Print #1, "<font size="; Chr(34); 5; Chr(34); "><b>"; BeginText & "<br>";
    'Ghi dòng tiêu đề ở đầu danh sách
    Print #1, UCase(Dir1.Path); "</b></font></p>"
   
'Ghi đường dẫn
          ' Bắt đầu ghi các liên kết
    For i = 0 To List1.ListCount - 1
       
'Ghi các liên đến các file đã chọn
        If List1.SelCount <> 0 Then
            If List1.Selected(i) = True Then
                Print #1, "<p align="; Chr(34); AlignText; Chr(34);
                Print #1, "><a href="; Chr(34);
                Print #1, List1.List(i); Chr(34);
                Print #1, ">"; File1.List(i); "</a></p>"
            End If
        Else
           
'Nếu không chọn file nào xem như chọn tất cả
            Print #1, "<p align="; Chr(34); AlignText;
            Print #1, Chr(34); "><a href="; Chr(34);
            Print #1, List1.List(i); Chr(34);
            Print #1, ">"; File1.List(i); "</a></p>"
        End If
    Next i
    Print #1, "<p align="; Chr(34); "left"; Chr(34); ">"; EndText
   
'Ghi dòng văn bản ở cuối danh sách
    Print #1, "<br>Total: " & _
    IIf(List1.SelCount = 0, List1.ListCount, List1.SelCount) _
    & " link(s)."; "</p>"
   
'In tổng số các liên kết trong trang Web
    Print #1, "</body></html>"
    Close #1
   
'Đóng danh sách
    Select Case Launch
        Case 1: k = Shell("explorer.exe " & F, vbNormalFocus)
       
'Gọi Internet Explorer
        Case 2: k = Shell("notepad.exe " & F, vbNormalFocus)
       
'Gọi Notepad
    End Select
    Form1.MousePointer = 0
    Text1.SetFocus
    Exit Sub
    ErrHandle:
       
'Xử lý khi có lỗi
        Form1.MousePointer = 0
        MsgBox "Can not create file.", vbCritical
        Drive1.SetFocus
End Sub

'Bỏ chọn tất cả các file trong danh sách

Private Sub cmdDeselect_Click()
    Dim i As Integer
    For i = List1.ListCount - 1 To 0 Step -1
        List1.Selected(i) = False
    Next i
End Sub

Private Sub cmdExit_Click()
    Unload Me
End Sub

'Chọn tất cả các file trong danh sách

Private Sub cmdSelect_Click()
    Dim i As Integer
    For i = 0 To List1.ListCount - 1
        List1.Selected(i) = True
    Next i
    List1.Refresh
End Sub

'Gọi Form2

Private Sub cmdSettings_Click()
    Form2.Show 1
End Sub

'Xử lý khi người dùng gõ vào các ký hiệu * ? để lọc file

Private Sub Combo1_Change()
    On Error GoTo ErrHandle
    Dim i As Integer
    File1.Pattern = Combo1.Text
    'Add to List Box
    List1.Clear
    For i = 0 To File1.ListCount - 1
        List1.AddItem File1.List(i), i
    Next i
    ErrHandle:
End Sub

'Xử lý khi người dùng chọn các loại file (có sẵn) trong Combobox

Private Sub Combo1_Click()
    Dim i As Integer
    File1.Pattern = Combo1.Text
    'Add to List Box
    AddToList
End Sub

'Xử lý khi người dùng chọn ổ đĩa, thư mục

Private Sub Dir1_Change()
    File1.Path = Dir1.Path
    lblPath.Caption = IIf(Len(Dir1.Path) = 3, Dir1.Path, Dir1.Path + "\")
    AddToList
End Sub

Private Sub Drive1_Change()
    On Error GoTo ErrHandle
    Dir1.Path = Drive1.Drive
    ErrHandle:
End Sub

Private Sub Form_Load()
    Me.Caption = "Indexer - " & "v" & App.Major & "." & App.Minor & App.Revision
    WTitle = "Indexer - List of Directory"
    BeginText = ""
    EndText = ""
    AlignText = "Left"
    Launch = 1
    File1.Path = Dir1.Path
    lblPath.Caption = IIf(Len(Dir1.Path) = 3, Dir1.Path, Dir1.Path + "\")
    AddToList
End Sub

'Nạp các file từ FileListBox sang ListBox, để tránh tình trạng xuất hiện liên kết đến chính file vừa tạo (khi file này đã được tạo ra trước đó) tôi phải dùng thêm Kill() nếu file đó trùng tên với file sắp tạo.

Public Sub AddToList()
    Dim i As Integer
    On Error Resume Next
    Dim F As String
    F = lblPath.Caption & Trim(Text1)
    Kill (F)
    File1.Refresh
    List1.Clear
    For i = 0 To File1.ListCount - 1
        List1.AddItem File1.List(i), i
    Next i
End Sub

'Tự động select văn bản mỗi khi Textbox nhận được focus, để tiện cho người dùng.

Private Sub Text1_GotFocus()
    Text1.SelStart = 0
    Text1.SelLength = Len(Text1)
End Sub

Form2

Option Explicit

Private Sub cmdCancel_Click()
    Unload Me
End Sub

Private Sub cmdOK_Click()
    Form1.WTitle = txtTitle.Text
    'Gán biến WTitle bằng nội dung của txtTitle
    Form1.BeginText = txtBegin.Text
   
'Gán biến BeginText bằng nội dung của txtBegin
    Form1.EndText = txtEnd.Text

    'Gán biến EndText bằng nội dung của txtEnd
    Form1.Launch = IIf(optIE.Value, 1, IIf(optNod.Value, 2, 3))
    'Nếu optIE được chọn thì gán Launch = 1, optNod -> 2, optNone -> 3
    Form1.AlignText = cmbAlign.Text
    Unload Me
End Sub

'Tự động select văn bản mỗi khi Textbox nhận được focus, để tiện cho người dùng.

Private Sub txtBegin_GotFocus()
    txtBegin.SelStart = 0
    txtBegin.SelLength = Len(txtBegin)
End Sub

Private Sub txtEnd_GotFocus()
    txtEnd.SelStart = 0
    txtEnd.SelLength = Len(txtEnd)
End Sub

Private Sub txtTitle_GotFocus()
    txtTitle.SelStart = 0
    txtTitle.SelLength = Len(txtTitle)
End Sub

Chương trình mẫu

[ Home Trang đầu ]  [ Thiết kế giao diện ]


chủ nhật, tháng hai 03, 2002 05:51:01 CH
Nguyễn Hồ Thiên Đăng


PcLeHoan 1996 - 2002
Mirror : http://www.pclehoan.com
Mirror : http://www.lehoanpc.net

Mirror : http://www.ktlehoan.com