home *** CD-ROM | disk | FTP | other *** search
/ Dan Appleman's Visual Bas…s Guide to the Win32 API / Dan.Applmans.Visual.Basic.5.0.Programmers.Guide.To.The.Win32.API.1997.Ziff-Davis.Press.CD / VB5PG32.mdf / vbpg32 / samples4 / ch04 / vb3ini.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1995-05-05  |  5.2 KB  |  161 lines

  1. VERSION 2.00
  2. Begin Form frmVB3INI 
  3.    Caption         =   "VB3 Profile Example"
  4.    ClientHeight    =   3600
  5.    ClientLeft      =   1395
  6.    ClientTop       =   1905
  7.    ClientWidth     =   6600
  8.    Height          =   4005
  9.    Left            =   1335
  10.    ScaleHeight     =   3600
  11.    ScaleWidth      =   6600
  12.    Top             =   1560
  13.    Width           =   6720
  14.    Begin CommandButton cmdDeleteKey 
  15.       Caption         =   "Delete Key"
  16.       Height          =   495
  17.       Left            =   4800
  18.       TabIndex        =   8
  19.       Top             =   1320
  20.       Width           =   1575
  21.    End
  22.    Begin CommandButton cmdAdd 
  23.       Caption         =   "Edit Entry"
  24.       Height          =   495
  25.       Left            =   600
  26.       TabIndex        =   7
  27.       Top             =   2520
  28.       Width           =   1215
  29.    End
  30.    Begin TextBox txtNewValue 
  31.       Height          =   285
  32.       Left            =   2640
  33.       TabIndex        =   6
  34.       Top             =   2760
  35.       Width           =   3735
  36.    End
  37.    Begin TextBox txtNewKey 
  38.       Height          =   285
  39.       Left            =   2640
  40.       TabIndex        =   5
  41.       Top             =   2400
  42.       Width           =   3735
  43.    End
  44.    Begin CommandButton cmdListKeys 
  45.       Caption         =   "List Keys"
  46.       Height          =   495
  47.       Left            =   4800
  48.       TabIndex        =   2
  49.       Top             =   600
  50.       Width           =   1575
  51.    End
  52.    Begin ListBox lstKeys 
  53.       Height          =   1590
  54.       Left            =   600
  55.       TabIndex        =   0
  56.       Top             =   600
  57.       Width           =   3975
  58.    End
  59.    Begin Label Label3 
  60.       Alignment       =   1  'Right Justify
  61.       Caption         =   "Value"
  62.       Height          =   255
  63.       Left            =   1920
  64.       TabIndex        =   3
  65.       Top             =   2760
  66.       Width           =   615
  67.    End
  68.    Begin Label Label2 
  69.       Alignment       =   1  'Right Justify
  70.       Caption         =   "Key"
  71.       Height          =   255
  72.       Left            =   1920
  73.       TabIndex        =   4
  74.       Top             =   2400
  75.       Width           =   615
  76.    End
  77.    Begin Label Label1 
  78.       Caption         =   "Keys in [FirstSection]"
  79.       Height          =   255
  80.       Left            =   600
  81.       TabIndex        =   1
  82.       Top             =   240
  83.       Width           =   2415
  84.    End
  85. Option Explicit
  86. Dim FileName$
  87. Const INIFILE = "private.ini"
  88. ' Add a new key and value into the INI file section
  89. Private Sub cmdAdd_Click ()
  90.     Dim success%
  91.     If Len(txtNewKey.Text) = 0 Or Len(txtNewValue.Text) = 0 Then
  92.         MsgBox "Key and value must both be specified"
  93.         Exit Sub
  94.     End If
  95.     ' Write the new key
  96.     success% = WritePrivateProfileStringByKeyName("FirstSection", txtNewKey.Text, txtNewValue.Text, FileName$)
  97.     If success% = 0 Then
  98.         Dim msg$
  99.         msg$ = "Edit failed - this is typically caused by a write protected INI file"
  100.         msg$ = msg$ & Chr$(10) & "Try copying the file and project to your hard disk."
  101.         MsgBox msg$
  102.         Exit Sub
  103.     End If
  104.     cmdListKeys_Click   ' Refresh the list box
  105. End Sub
  106. ' Delete the selected key
  107. Private Sub cmdDeleteKey_Click ()
  108.     Dim success%
  109.     If lstKeys.ListIndex < 1 Then
  110.         MsgBox "No selected entries in the list box"
  111.         Exit Sub
  112.     End If
  113.     ' Delete the selected key
  114.     success% = WritePrivateProfileStringToDeleteKey("FirstSection", lstKeys.Text, 0, FileName$)
  115.     If success% = 0 Then
  116.         Dim msg$
  117.         msg$ = "Delete failed - this is typically caused by a write protected INI file"
  118.         msg$ = msg$ & Chr$(10) & "Try copying the file and project to your hard disk."
  119.         MsgBox msg$
  120.         Exit Sub
  121.     End If
  122.     cmdListKeys_Click   ' Refresh the list box
  123. End Sub
  124. ' List all of the keys in a particular section
  125. Private Sub cmdListKeys_Click ()
  126.     Dim bytes%
  127.     Dim KeyList$
  128.     KeyList$ = String$(128, 0)
  129.     lstKeys.Clear
  130.     ' Retrieve the list of keys in the section
  131.     bytes% = GetPrivateProfileStringKeys("FirstSection", 0, "", KeyList$, 127, FileName$)
  132.     ' Load sections into the list box
  133.     Dim NullOffset%
  134.     Do
  135.         NullOffset% = InStr(KeyList$, Chr$(0))
  136.         If NullOffset% > 1 Then
  137.             lstKeys.AddItem Mid$(KeyList$, 1, NullOffset% - 1)
  138.             KeyList$ = Mid$(KeyList$, NullOffset% + 1)
  139.         End If
  140.     Loop While NullOffset% > 1
  141. End Sub
  142. ' This sample expects to see the INI file in the application
  143. ' directory. At design time, the current directory is VB, so
  144. ' we add the app.path to make sure we choose the right place.
  145. ' The \ handling deals with the unlikely event that the INI file
  146. ' is in the root directory.
  147. Private Sub Form_Load ()
  148.     FileName$ = app.Path
  149.     If Right$(FileName$, 1) <> "\" Then FileName$ = FileName$ & "\"
  150.     FileName$ = FileName$ & INIFILE
  151. End Sub
  152. Private Sub lstKeys_Click ()
  153.     Dim bytes%
  154.     Dim KeyValue$
  155.     KeyValue$ = String$(128, 0)
  156.     ' Retrieve the list of keys in the section
  157.     KeyValue$ = VBGetPrivateProfileString("FirstSection", lstKeys.Text, FileName$)
  158.     txtNewKey.Text = lstKeys.Text
  159.     txtNewValue.Text = KeyValue$
  160. End Sub
  161.