home *** CD-ROM | disk | FTP | other *** search
- VERSION 5.00
- Begin VB.Form frmVB5INI
- Caption = "VB5 Profile Example"
- ClientHeight = 3135
- ClientLeft = 1080
- ClientTop = 1485
- ClientWidth = 6600
- BeginProperty Font
- Name = "MS Sans Serif"
- Size = 8.25
- Charset = 0
- Weight = 700
- Underline = 0 'False
- Italic = 0 'False
- Strikethrough = 0 'False
- EndProperty
- LinkTopic = "Form1"
- PaletteMode = 1 'UseZOrder
- ScaleHeight = 3135
- ScaleWidth = 6600
- Begin VB.CommandButton cmdShowSections
- Caption = "List Sections"
- Height = 495
- Left = 4800
- TabIndex = 9
- Top = 1800
- Width = 1575
- End
- Begin VB.CommandButton cmdDeleteKey
- Caption = "Delete Key"
- Height = 495
- Left = 4800
- TabIndex = 8
- Top = 1200
- Width = 1575
- End
- Begin VB.CommandButton cmdAdd
- Caption = "Edit Entry"
- Height = 495
- Left = 600
- TabIndex = 7
- Top = 2520
- Width = 1215
- End
- Begin VB.TextBox txtNewValue
- Height = 285
- Left = 2640
- TabIndex = 6
- Top = 2760
- Width = 3735
- End
- Begin VB.TextBox txtNewKey
- Height = 285
- Left = 2640
- TabIndex = 5
- Top = 2400
- Width = 3735
- End
- Begin VB.CommandButton cmdListKeys
- Caption = "List Keys"
- Height = 495
- Left = 4800
- TabIndex = 2
- Top = 600
- Width = 1575
- End
- Begin VB.ListBox lstKeys
- Height = 1590
- Left = 600
- TabIndex = 0
- Top = 600
- Width = 3975
- End
- Begin VB.Label Label3
- Alignment = 1 'Right Justify
- Caption = "Value"
- Height = 255
- Left = 1920
- TabIndex = 3
- Top = 2760
- Width = 615
- End
- Begin VB.Label Label2
- Alignment = 1 'Right Justify
- Caption = "Key"
- Height = 255
- Left = 1920
- TabIndex = 4
- Top = 2400
- Width = 615
- End
- Begin VB.Label Label1
- Caption = "Keys in [FirstSection]"
- Height = 255
- Left = 600
- TabIndex = 1
- Top = 240
- Width = 2415
- End
- Attribute VB_Name = "frmVB5INI"
- Attribute VB_GlobalNameSpace = False
- Attribute VB_Creatable = False
- Attribute VB_PredeclaredId = True
- Attribute VB_Exposed = False
- 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 characters As Long
- Dim KeyList$
- KeyList$ = String$(128, 0)
- lstKeys.Clear
- ' Retrieve the list of keys in the section
- ' Note that characters is always a long here - in 16 bit mode
- ' VB will convert the integer result of the API function into
- ' a long - no problem.
- characters = 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
- Private Sub cmdShowSections_Click()
- #If Win32 Then
- Dim characters As Long
- Dim SectionList$
- SectionList$ = String$(128, 0)
- ' Retrieve the list of keys in the section
- characters = GetPrivateProfileStringSections(0, 0, "", SectionList$, 127, FileName$)
- ' Load sections into the list box
- Dim NullOffset%
- Dim MsgString$
- Do
- NullOffset% = InStr(SectionList$, Chr$(0))
- If NullOffset% > 1 Then
-
- MsgString$ = MsgString$ & Chr$(10) & Mid$(SectionList$, 1, NullOffset% - 1)
- SectionList$ = Mid$(SectionList$, NullOffset% + 1)
- End If
- Loop While NullOffset% > 1
- MsgBox MsgString$, 0, "Sections in file"
- #End If
- 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
- #If Win16 Then
- ' We can't show sections on Win16
- cmdShowSections.Visible = False
- #End If
- End Sub
- Private Sub lstKeys_Click()
- Dim KeyValue$
- ' Retrieve the list of keys in the section
- KeyValue$ = VBGetPrivateProfileString("FirstSection", lstKeys.Text, FileName$)
- txtNewKey.Text = lstKeys.Text
- txtNewValue.Text = KeyValue$
- End Sub
-