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 / samples5 / ch04 / vb5ini.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1996-11-03  |  6.9 KB  |  207 lines

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