home *** CD-ROM | disk | FTP | other *** search
- VERSION 2.00
- Begin Form frmVB3INI
- Caption = "VB3 Profile Example"
- ClientHeight = 3600
- ClientLeft = 1395
- ClientTop = 1905
- ClientWidth = 6600
- Height = 4005
- Left = 1335
- ScaleHeight = 3600
- ScaleWidth = 6600
- Top = 1560
- Width = 6720
- Begin CommandButton cmdDeleteKey
- Caption = "Delete Key"
- Height = 495
- Left = 4800
- TabIndex = 8
- Top = 1320
- Width = 1575
- End
- Begin CommandButton cmdAdd
- Caption = "Edit Entry"
- Height = 495
- Left = 600
- TabIndex = 7
- Top = 2520
- Width = 1215
- End
- Begin TextBox txtNewValue
- Height = 285
- Left = 2640
- TabIndex = 6
- Top = 2760
- Width = 3735
- End
- Begin TextBox txtNewKey
- Height = 285
- Left = 2640
- TabIndex = 5
- Top = 2400
- Width = 3735
- End
- Begin CommandButton cmdListKeys
- Caption = "List Keys"
- Height = 495
- Left = 4800
- TabIndex = 2
- Top = 600
- Width = 1575
- End
- Begin ListBox lstKeys
- Height = 1590
- Left = 600
- TabIndex = 0
- Top = 600
- Width = 3975
- End
- Begin Label Label3
- Alignment = 1 'Right Justify
- Caption = "Value"
- Height = 255
- Left = 1920
- TabIndex = 3
- Top = 2760
- Width = 615
- End
- Begin Label Label2
- Alignment = 1 'Right Justify
- Caption = "Key"
- Height = 255
- Left = 1920
- TabIndex = 4
- Top = 2400
- Width = 615
- End
- Begin Label Label1
- Caption = "Keys in [FirstSection]"
- Height = 255
- Left = 600
- TabIndex = 1
- Top = 240
- Width = 2415
- End
- Option Explicit
- Dim FileName$
- Const INIFILE = "private.ini"
- ' Add a new key and value into the INI file section
- Private Sub cmdAdd_Click ()
- Dim success%
- If Len(txtNewKey.Text) = 0 Or Len(txtNewValue.Text) = 0 Then
- MsgBox "Key and value must both be specified"
- Exit Sub
- End If
- ' Write the new key
- success% = WritePrivateProfileStringByKeyName("FirstSection", txtNewKey.Text, txtNewValue.Text, FileName$)
- If success% = 0 Then
- Dim msg$
- msg$ = "Edit failed - this is typically caused by a write protected INI file"
- msg$ = msg$ & Chr$(10) & "Try copying the file and project to your hard disk."
- MsgBox msg$
- Exit Sub
- End If
- cmdListKeys_Click ' Refresh the list box
- End Sub
- ' Delete the selected key
- Private Sub cmdDeleteKey_Click ()
- Dim success%
- If lstKeys.ListIndex < 1 Then
- MsgBox "No selected entries in the list box"
- Exit Sub
- End If
- ' Delete the selected key
- success% = WritePrivateProfileStringToDeleteKey("FirstSection", lstKeys.Text, 0, FileName$)
- If success% = 0 Then
- Dim msg$
- msg$ = "Delete failed - this is typically caused by a write protected INI file"
- msg$ = msg$ & Chr$(10) & "Try copying the file and project to your hard disk."
- MsgBox msg$
- Exit Sub
- End If
- cmdListKeys_Click ' Refresh the list box
- End Sub
- ' List all of the keys in a particular section
- Private Sub cmdListKeys_Click ()
- Dim bytes%
- Dim KeyList$
- KeyList$ = String$(128, 0)
- lstKeys.Clear
- ' Retrieve the list of keys in the section
- bytes% = GetPrivateProfileStringKeys("FirstSection", 0, "", KeyList$, 127, FileName$)
- ' Load sections into the list box
- Dim NullOffset%
- Do
- NullOffset% = InStr(KeyList$, Chr$(0))
- If NullOffset% > 1 Then
- lstKeys.AddItem Mid$(KeyList$, 1, NullOffset% - 1)
- KeyList$ = Mid$(KeyList$, NullOffset% + 1)
- End If
- Loop While NullOffset% > 1
- End Sub
- ' This sample expects to see the INI file in the application
- ' directory. At design time, the current directory is VB, so
- ' we add the app.path to make sure we choose the right place.
- ' The \ handling deals with the unlikely event that the INI file
- ' is in the root directory.
- Private Sub Form_Load ()
- FileName$ = app.Path
- If Right$(FileName$, 1) <> "\" Then FileName$ = FileName$ & "\"
- FileName$ = FileName$ & INIFILE
- End Sub
- Private Sub lstKeys_Click ()
- Dim bytes%
- Dim KeyValue$
- KeyValue$ = String$(128, 0)
- ' Retrieve the list of keys in the section
- KeyValue$ = VBGetPrivateProfileString("FirstSection", lstKeys.Text, FileName$)
- txtNewKey.Text = lstKeys.Text
- txtNewValue.Text = KeyValue$
- End Sub
-