Khách v Khách viếng thăm
| Tiêu đề: Thiết kế chương trình Program Starter 12/2/2008, 2:53 pm | |
| - Mở Visual Basic, chọn Standard EXE. Nhấn Ctrl+T, chọn kiểm Microsoft Common Dialog Control 6.0 I. thiết kế giao diệnBạn thiết kế giao diện như sau: gồm 2 form, là FrmMain và FrmAdd, bạn có thể theo hình mà thiết kế. (cách thêm 1 form mới: Project > Add Form) Form 1:Các thiết lập: Form1:
- Name: FrmMain - BorberStyle: 1 – Fixed Single - Caption: Program Starter - KeyPreview: True - MiniButton: True Label1:
- Name: LabTime - Caption: Time Label2:
- Name: LabProgram - Caption: Program List1:
- Name: LstTime List2:
- Name: LstProgram Command1:
- Name: CmdAdd - Caption: Add Command2:
- Name: CmdEdit - Caption: Edit - Enabled: False Command3:
- Name: CmdDel - Caption: Delete - Enabled: False Timer1:
- Name: TimTimer - Interval: 1000 Chỉnh sửa đôi chút, bạn được như sau: Form2:Các thiết lập: Form2:
- Name: FrmAdd - BorberStyle: 4 – Fixed ToolWindow - Caption: Add Item Label1:
- Name: LabTime - Cpation: Time: Label2:- Name: LabProgram - Cpation: Program: Text1:
- Name: TxtTime - Text: (để trống) Text2:- Name: TxtProgram - Locked: True - Text: (để trống) Commad1:
- Name: CmdOK - Caption: OK Command2:
- Name: CmdCancel - Caption: Cancel Command3:
- Name: CmdBrowse - Caption: … (ba dấu chấm) CommonDialog1:- Name: CdlDialog - CancelError: True - DialogTitle: Open your file… - Filter: Execution File (*.exe)|*.exe Thêm mắn, thêm muối, sẽ ra như sau: II.Viết code xử lý- Mở cửa sổ code ra (View > Code), dán 2 đoạn mã này: FrmMain: - Code:
-
Public Edit As Boolean Public ItemCount As Integer Dim ByteFile As Variant Dim AppPath As String Dim i As Integer
Private Sub CmdAdd_Click() Edit = False FrmAdd.Show vbModal End Sub
Private Sub CmdDel_Click() LstTime.RemoveItem LstTime.ListIndex LstProgram.RemoveItem LstProgram.ListIndex LstTime.ListIndex = -1 LstProgram.ListIndex = -1 CmdDel.Enabled = False CmdEdit.Enabled = False ItemCount = ItemCount - 1 End Sub
Private Sub CmdEdit_Click() Edit = True With FrmAdd .TxtTime = LstTime.List(LstTime.ListIndex) .TxtProgram = LstProgram.List(LstProgram.ListIndex) .Show vbModal End With End Sub
Private Sub Form_Initialize() If Right$(App.Path, 1) <> "\" Then AppPath = App.Path & "\" Else AppPath = App.Path End If End Sub
Private Sub Form_Load() Dim Data As New PropertyBag On Error Resume Next Open AppPath & "Data.dat" For Binary As #1 Get #1, , ByteFile Close #1 Data.Contents = ByteFile ItemCount = Data.ReadProperty("Count") For i = 1 To ItemCount LstTime.AddItem Data.ReadProperty("Time" & i) LstProgram.AddItem Data.ReadProperty("Program" & i) Next End Sub
Private Sub Form_Unload(Cancel As Integer) Dim Data As New PropertyBag On Error Resume Next TimTimer.Enabled = False Data.WriteProperty "Count", ItemCount For i = 1 To ItemCount Data.WriteProperty "Time" & i, LstTime.List(i - 1) Data.WriteProperty "Program" & i, LstProgram.List(i - 1) Next ByteFile = Data.Contents Open AppPath & "Data.dat" For Binary As #1 Put #1, , ByteFile Close #1 End Sub
Private Sub LstTime_Click() LstProgram.ListIndex = LstTime.ListIndex CmdEdit.Enabled = True CmdDel.Enabled = True End Sub
Private Sub LstTime_KeyDown(KeyCode As Integer, Shift As Integer) LstTime_Click End Sub
Private Sub LstProgram_Click() LstProgram.ToolTipText = LstProgram.List(LstProgram.ListIndex) LstTime.ListIndex = LstProgram.ListIndex CmdEdit.Enabled = True CmdDel.Enabled = True End Sub
Private Sub LstProgram_KeyDown(KeyCode As Integer, Shift As Integer) LstProgram_Click End Sub
Private Sub TimTimer_Timer() TimTimer.Tag = CStr(Time) Me.Caption = "Program Starter - " & TimTimer.Tag For i = 0 To LstTime.ListCount - 1 If LstTime.List(i) = TimTimer.Tag Then Shell LstProgram.List(i), vbNormalFocus Next End Sub FrmAdd - Code:
-
Private Sub CmdBrowse_Click() On Error GoTo Err With CdlDialog .ShowOpen If Dir$(.FileName, vbArchive vbHidden vbReadOnly vbSystem) <> "" Then TxtProgram.Text = .FileName Else MsgBox "Your file not exist !", vbExclamation, "Error: file not exist" End If End With Exit Sub Err: If Err = cdlCancel Then Exit Sub Else MsgBox Err.Description, vbCritical, "Error: " & Err.Number End If End Sub
Private Sub CmdCancel_Click() Unload Me End Sub
Private Sub CmdOK_Click() If TxtTime.Text <> "" And TxtProgram.Text <> "" Then With FrmMain If .Edit = True Then .LstTime.List(.LstTime.ListIndex) = TxtTime.Text .LstProgram.List(.LstProgram.ListIndex) = TxtProgram.Text Else .LstTime.AddItem TxtTime.Text .LstProgram.AddItem TxtProgram.Text .ItemCount = .ItemCount 1 End If .LstTime.ListIndex = -1 .LstProgram.ListIndex = -1 .CmdEdit.Enabled = False .CmdDel.Enabled = False End With Unload Me Else MsgBox "Please enter your data !", vbExclamation, "Error: no have data !" End If End Sub
Private Sub Form_Load() TxtTime.Text = FrmMain.TimTimer.Tag End Sub - Cuối cùng là dịch ra file exe, vào File, chọn Make Project1.exe, chọn nơi chứa và tên file là xong. |
|