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 / vb4ini.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1995-07-07  |  6.9 KB  |  208 lines

  1. VERSION 4.00
  2. Begin VB.Form frmVB4INI 
  3.    Caption         =   "VB4 Profile Example"
  4.    ClientHeight    =   3135
  5.    ClientLeft      =   1080
  6.    ClientTop       =   1485
  7.    ClientWidth     =   6600
  8.    BeginProperty Font 
  9.       name            =   "MS Sans Serif"
  10.       charset         =   0
  11.       weight          =   700
  12.       size            =   8.25
  13.       underline       =   0   'False
  14.       italic          =   0   'False
  15.       strikethrough   =   0   'False
  16.    EndProperty
  17.    Height          =   3540
  18.    Left            =   1020
  19.    LinkTopic       =   "Form1"
  20.    ScaleHeight     =   3135
  21.    ScaleWidth      =   6600
  22.    Top             =   1140
  23.    Width           =   6720
  24.    Begin VB.CommandButton cmdShowSections 
  25.       Caption         =   "List Sections"
  26.       Height          =   495
  27.       Left            =   4800
  28.       TabIndex        =   9
  29.       Top             =   1800
  30.       Width           =   1575
  31.    End
  32.    Begin VB.CommandButton cmdDeleteKey 
  33.       Caption         =   "Delete Key"
  34.       Height          =   495
  35.       Left            =   4800
  36.       TabIndex        =   8
  37.       Top             =   1200
  38.       Width           =   1575
  39.    End
  40.    Begin VB.CommandButton cmdAdd 
  41.       Caption         =   "Edit Entry"
  42.       Height          =   495
  43.       Left            =   600
  44.       TabIndex        =   7
  45.       Top             =   2520
  46.       Width           =   1215
  47.    End
  48.    Begin VB.TextBox txtNewValue 
  49.       Height          =   285
  50.       Left            =   2640
  51.       TabIndex        =   6
  52.       Top             =   2760
  53.       Width           =   3735
  54.    End
  55.    Begin VB.TextBox txtNewKey 
  56.       Height          =   285
  57.       Left            =   2640
  58.       TabIndex        =   5
  59.       Top             =   2400
  60.       Width           =   3735
  61.    End
  62.    Begin VB.CommandButton cmdListKeys 
  63.       Caption         =   "List Keys"
  64.       Height          =   495
  65.       Left            =   4800
  66.       TabIndex        =   2
  67.       Top             =   600
  68.       Width           =   1575
  69.    End
  70.    Begin VB.ListBox lstKeys 
  71.       Height          =   1590
  72.       Left            =   600
  73.       TabIndex        =   0
  74.       Top             =   600
  75.       Width           =   3975
  76.    End
  77.    Begin VB.Label Label3 
  78.       Alignment       =   1  'Right Justify
  79.       Caption         =   "Value"
  80.       Height          =   255
  81.       Left            =   1920
  82.       TabIndex        =   3
  83.       Top             =   2760
  84.       Width           =   615
  85.    End
  86.    Begin VB.Label Label2 
  87.       Alignment       =   1  'Right Justify
  88.       Caption         =   "Key"
  89.       Height          =   255
  90.       Left            =   1920
  91.       TabIndex        =   4
  92.       Top             =   2400
  93.       Width           =   615
  94.    End
  95.    Begin VB.Label Label1 
  96.       Caption         =   "Keys in [FirstSection]"
  97.       Height          =   255
  98.       Left            =   600
  99.       TabIndex        =   1
  100.       Top             =   240
  101.       Width           =   2415
  102.    End
  103. Attribute VB_Name = "frmVB4INI"
  104. Attribute VB_Creatable = False
  105. Attribute VB_Exposed = False
  106. Option Explicit
  107. Dim FileName$
  108. Const INIFILE = "private.ini"
  109. ' Add a new key and value into the INI file section
  110. Private Sub cmdAdd_Click()
  111.     Dim success%
  112.     If Len(txtNewKey.TEXT) = 0 Or Len(txtNewValue.TEXT) = 0 Then
  113.         MsgBox "Key and value must both be specified"
  114.         Exit Sub
  115.     End If
  116.     ' Write the new key
  117.     success% = WritePrivateProfileStringByKeyName("FirstSection", txtNewKey.TEXT, txtNewValue.TEXT, FileName$)
  118.     If success% = 0 Then
  119.         Dim msg$
  120.         msg$ = "Edit failed - this is typically caused by a write protected INI file"
  121.         msg$ = msg$ & Chr$(10) & "Try copying the file and project to your hard disk."
  122.         MsgBox msg$
  123.         Exit Sub
  124.     End If
  125.     cmdListKeys_Click   ' Refresh the list box
  126. End Sub
  127. ' Delete the selected key
  128. Private Sub cmdDeleteKey_Click()
  129.     Dim success%
  130.     If lstKeys.ListIndex < 1 Then
  131.         MsgBox "No selected entries in the list box"
  132.         Exit Sub
  133.     End If
  134.     ' Delete the selected key
  135.     success% = WritePrivateProfileStringToDeleteKey("FirstSection", lstKeys.TEXT, 0, FileName$)
  136.     If success% = 0 Then
  137.         Dim msg$
  138.         msg$ = "Delete failed - this is typically caused by a write protected INI file"
  139.         msg$ = msg$ & Chr$(10) & "Try copying the file and project to your hard disk."
  140.         MsgBox msg$
  141.         Exit Sub
  142.     End If
  143.     cmdListKeys_Click   ' Refresh the list box
  144. End Sub
  145. ' List all of the keys in a particular section
  146. Private Sub cmdListKeys_Click()
  147.     Dim characters As Long
  148.     Dim KeyList$
  149.     KeyList$ = String$(128, 0)
  150.     lstKeys.Clear
  151.     ' Retrieve the list of keys in the section
  152.     ' Note that characters is always a long here - in 16 bit mode
  153.     ' VB will convert the integer result of the API function into
  154.     ' a long - no problem.
  155.     characters = GetPrivateProfileStringKeys("FirstSection", 0, "", KeyList$, 127, FileName$)
  156.     ' Load sections into the list box
  157.     Dim NullOffset%
  158.     Do
  159.         NullOffset% = InStr(KeyList$, Chr$(0))
  160.         If NullOffset% > 1 Then
  161.             lstKeys.AddItem Mid$(KeyList$, 1, NullOffset% - 1)
  162.             KeyList$ = Mid$(KeyList$, NullOffset% + 1)
  163.         End If
  164.     Loop While NullOffset% > 1
  165. End Sub
  166. Private Sub cmdShowSections_Click()
  167. #If Win32 Then
  168.     Dim characters As Long
  169.     Dim SectionList$
  170.     SectionList$ = String$(128, 0)
  171.     ' Retrieve the list of keys in the section
  172.     characters = GetPrivateProfileStringSections(0, 0, "", SectionList$, 127, FileName$)
  173.     ' Load sections into the list box
  174.     Dim NullOffset%
  175.     Dim MsgString$
  176.     Do
  177.         NullOffset% = InStr(SectionList$, Chr$(0))
  178.         If NullOffset% > 1 Then
  179.             
  180.             MsgString$ = MsgString$ & Chr$(10) & Mid$(SectionList$, 1, NullOffset% - 1)
  181.             SectionList$ = Mid$(SectionList$, NullOffset% + 1)
  182.         End If
  183.     Loop While NullOffset% > 1
  184.     MsgBox MsgString$, 0, "Sections in file"
  185. #End If
  186. End Sub
  187. ' This sample expects to see the INI file in the application
  188. ' directory. At design time, the current directory is VB, so
  189. ' we add the app.path to make sure we choose the right place.
  190. ' The \ handling deals with the unlikely event that the INI file
  191. ' is in the root directory.
  192. Private Sub Form_Load()
  193.     FileName$ = App.Path
  194.     If Right$(FileName$, 1) <> "\" Then FileName$ = FileName$ & "\"
  195.     FileName$ = FileName$ & INIFILE
  196. #If Win16 Then
  197.     ' We can't show sections on Win16
  198.     cmdShowSections.Visible = False
  199. #End If
  200. End Sub
  201. Private Sub lstKeys_Click()
  202.     Dim KeyValue$
  203.     ' Retrieve the list of keys in the section
  204.     KeyValue$ = VBGetPrivateProfileString("FirstSection", lstKeys.TEXT, FileName$)
  205.     txtNewKey.TEXT = lstKeys.TEXT
  206.     txtNewValue.TEXT = KeyValue$
  207. End Sub
  208.