home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic 6 Unleashed…sional Reference Edition) / Visual_Basic_6_Unleashed_Professional_Reference_Edition_Sams_1999.iso / Source / CHAP29 / 309X3205.TXT < prev    next >
Encoding:
Text File  |  1998-05-05  |  2.0 KB  |  74 lines

  1.  
  2. Private Sub cmdDelete_Click()
  3.  
  4. ' Make sure the user entered a section name.
  5. If txtSectionName.Text = "" Then
  6.     MsgBox "Please enter a section name first."
  7.     Exit Sub
  8. End If
  9. ' Check for a key name only when accessing the
  10. ' Registry.
  11. If optType(1).Value = True Then
  12.     If txtKeyName.Text = "" Then
  13.         MsgBox "Please enter a key name first."
  14.         Exit Sub
  15.     End If
  16. End If
  17.  
  18. ' For INI files, call the INIDelete procedure. For
  19. ' the Registry, call the RegDelete procedure.
  20. If optType(0).Value = True Then
  21.     lblStatus = INIDelete()
  22. Else
  23.     lblStatus = RegDelete()
  24. End If
  25.  
  26. End Sub
  27.  
  28.  
  29. Private Function INIDelete() As String
  30.  
  31. Dim lonStatus As Long
  32. Dim lonYesNo As Long
  33. Dim strKeyName As String
  34. Dim strSectionName As String
  35.  
  36. ' Remove brackets from section name, if necessary.
  37. strSectionName = FixSectionName(txtSectionName.Text)
  38.  
  39. If txtKeyName.Text = "" Then
  40.     ' No specific key name was specified, so the
  41.     ' entire section will be deleted. Ask the user
  42.     ' if this is okay as a safety measure.
  43.     lonYesNo = MsgBox("Delete all keys in section?", vbYesNo)
  44.     If lonYesNo = vbNo Then
  45.         INIDelete = "Delete operation aborted"
  46.         Exit Function
  47.     End If
  48.     ' When deleting a section, the key name passed
  49.     ' to the API function should be vbNullString.
  50.     strKeyName = vbNullString
  51. Else
  52.     ' When deleting a specific key, the key name
  53.     ' is passed to the API function.
  54.     strKeyName = txtKeyName.Text
  55. End If
  56.  
  57. ' Use the WritePrivateProfileString API function to
  58. ' delete specific keys or entire sections.
  59. lonStatus = WritePrivateProfileString(strSectionName, _
  60.     strKeyName, vbNullString, dlgGetFilename.FileName)
  61.     
  62. ' Display the status of the operation and update
  63. ' the file contents list box if successful.
  64. If lonStatus = 0 Then
  65.     INIDelete = "Error - Key could not be deleted"
  66. Else
  67.     INIDelete = "Key deleted successfully"
  68.     txtKeyName.Text = ""
  69.     txtValue.Text = ""
  70.     ReadFile
  71. End If
  72.         
  73. End Function
  74.