Jumat, 19 Oktober 2012

Menyimpan dan Mengambil Data ke dan dari Registry



Pada contoh berikut Anda dapat mengetahui cara menyimpan dan mengambil setting program yang disimpan di Registry.
Code::
'Deskripsi: Menyimpan dan mengambil data ke dan dari Registry dengan
'           memakai fungsi SaveSetting, GetSetting, dan GetAllSettings.
'           Khusus GetAllSettings, merupakan fungsi untuk mengambil
'           semua data di "bawah" suatu Section tertentu, jadi tanpa
'           harus mengambil satu per satu item/setting dengan GetSetting.
'Persiapan: 1. Buat 1 Project baru dengan 1 Form.
'           2. Tambahkan 3 TextBox, masing-masing beri nama dengan
'              Text1, Text2, dan Text3.
'           3. Tambahkan 1 ComboBox, beri nama dengan Combo1.
'           4. Tambahkan 2 CheckBox, masing-masing beri nama dengan
'              Check1 dan Check2.
'           5. Tambahkan 4 OptionButton, masing-masing beri nama dengan
'              Option1(0), Option1(1), Option2, dan Option3.
'           6. Tambahkan 1 ListBox, beri nama dengan List1.
'           7. Tambahkan 4 Commandbutton, masing-masing beri nama dengan
'              - cmdSaveSetting
'              - cmdGetSetting
'              - cmdDeleteSetting
'              - cmdSaveQuit
'           8. Copy-kan coding berikut ke dalam editor form yang bertalian.
'--------------------------------------------------------------------------
 
'Penggunaan setiap fungsi:
 
'SaveSetting(AppName As String, _
             Section As String, _
             Setting As String)
 
'GetSetting(AppName As String, _
            Section As String, _
            Key As String, _
            Default As String)
 
'GetAllSettings(AppName As String, _
                Section As String)
 
'Untuk melihat hasil dari contoh ini, jalankan Registry dari menu 
'Start->Run ketikkan: Regedit  lalu tekan Enter. Buka folder/direktori 
'di explorer Regedit yaitu:
'HKEY_CURRENT_USER\Software\VB and VBA Program Settings\prjRegedit
'kemudian periksa subfolder "Form" dan "TestRegedit".
'Khusus untuk Section "TestRegedit", seluruh nilai yang diambil dapat
'Anda lihat di List1.
'--------------------------------------------------------------------------
 
Private Sub cmdDeleteSetting_Click()
  On Error Resume Next
   DeleteSetting App.Title, "TestRegedit"
   MsgBox "Berhasil dihapus!", vbInformation, "Hapus OK"
End Sub
 
Private Sub cmdGetSetting_Click()
   Form_Load
End Sub
 
Private Sub cmdSaveQuit_Click()
   SimpanForm1
   SimpanLetakForm
   End
End Sub
 
Private Sub cmdSaveSetting_Click()
   SimpanForm1
   SimpanLetakForm
   MsgBox "Berhasil disimpan!", vbInformation, "Simpan OK"
End Sub
 
Private Sub Form_Load()
Dim AtasForm, KiriForm As Integer
  On Error Resume Next
   AtasForm = Screen.Height / 2 - Me.Height / 2
   KiriForm = Screen.Width / 2 - Me.Width / 2
   Me.Left = GetSetting(App.Title, "Form", "Kiri", KiriForm)
   Me.Top = GetSetting(App.Title, "Form", "Atas", AtasForm)
   Me.Width = GetSetting(App.Title, "Form", "Lebar", 5000)
   Me.Height = GetSetting(App.Title, "Form", "Tinggi", 5000)
    
   Dim avntSettings As Variant
   Dim intX As Integer
   avntSettings = GetAllSettings(App.Title, "TestRegedit")
   List1.Clear
   For intX = 0 To UBound(avntSettings, 1)
      List1.AddItem avntSettings(intX, 1)
   Next intX
    
   Text1 = List1.List(0)
   Text2 = List1.List(1)
   Text3 = List1.List(2)
   Check1 = List1.List(3)
   Check2 = List1.List(4)
   Option1(0) = List1.List(5)
   Option1(1) = List1.List(6)
   Option2 = List1.List(7)
   Option3 = List1.List(8)
   
   Combo1.List(0) = GetSetting(App.Title, "TestRegedit", "Combo1(0)", "")
   Combo1.List(1) = GetSetting(App.Title, "TestRegedit", "Combo1(1)", "")
   Combo1.List(2) = GetSetting(App.Title, "TestRegedit", "Combo1(2)", "")
   Combo1.Text = Text3.Text
End Sub
 
Sub SimpanForm1()
 
   SaveSetting App.Title, "TestRegedit", "Text1", Text1
   SaveSetting App.Title, "TestRegedit", "Text2", Text2
   SaveSetting App.Title, "TestRegedit", "Text3", Combo1.Text
   SaveSetting App.Title, "TestRegedit", "Check1", Check1.Value
   SaveSetting App.Title, "TestRegedit", "Check2", Check2.Value
   SaveSetting App.Title, "TestRegedit", "Option1(0)", Option1(0).Value
   SaveSetting App.Title, "TestRegedit", "Option1(1)", Option1(1).Value
   SaveSetting App.Title, "TestRegedit", "Option2", Option2.Value
   SaveSetting App.Title, "TestRegedit", "Option3", Option3.Value
   
   If Combo1.List(0) = "" Then
      SaveSetting App.Title, "TestRegedit", "Combo1(0)", Combo1.Text
   ElseIf Combo1.List(0) = Combo1.Text Or _
          Combo1.List(1) = Combo1.Text Or _
          Combo1.List(2) = Combo1.Text Then
      SaveSetting App.Title, "TestRegedit", "Combo1(0)", Combo1.List(0)
      SaveSetting App.Title, "TestRegedit", "Combo1(1)", Combo1.List(1)
      SaveSetting App.Title, "TestRegedit", "Combo1(2)", Combo1.List(2)
   Else
      SaveSetting App.Title, "TestRegedit", "Combo1(2)", Combo1.List(1)
      SaveSetting App.Title, "TestRegedit", "Combo1(1)", Combo1.List(0)
      SaveSetting App.Title, "TestRegedit", "Combo1(0)", Combo1.Text
   End If
    
End Sub
 
Sub SimpanLetakForm()
  If Me.WindowState <> vbMinimized Then
     SaveSetting App.Title, "Form", "Kiri", Me.Left
     SaveSetting App.Title, "Form", "Atas", Me.Top
     SaveSetting App.Title, "Form", "Lebar", Me.Width
     SaveSetting App.Title, "Form", "Tinggi", Me.Height
  End If
End Sub

0 komentar:

Posting Komentar

 

© 2011 e - Tutorial | by Moeh Fitrah