Visual Basic Projects

WritePrivateProfileString: Creating a Quiz Application
Step 3: Building the Quiz Topic Selection Form
     
Posted:   Tuesday August 24, 1999
Updated:   Monday December 26, 2011
     
Applies to:   VB4-32, VB5, VB6, and VB3, VB4-16 with appropriate declarations
Developed with:   VB6, Windows NT4
OS restrictions:   None
Author:   VBnet - Randy Birch
     
 Other project pages:   Step 1: Introduction and BAS Module
Step 2: Building the 'TopScores' Form
Step 3: Building the Quiz Topic Selection Form
Step 4: Building the Main Quiz Form
Form Illustration Layouts
Downloadable KidzQuiz INI file

Related:  

WritePrivateProfileString: INI Files - The Basics
WritePrivateProfileString: INI Files - Saving Entire Sections
     
 Prerequisites
Steps 1 and 2 completed.

This puppy is responsible for reading all the sections from the INI file, and populating the list with the available quiz topics. As coded, it looks for the key word "Questions" in the Topic description, thereby allowing this same INI file to be used for other information in different sections, such as last user, highest score, etc etc.
 Form Code: frmSelect
Add a new form to the project and name it frmSelect. This form has a list (List1), a Label, and two command buttons (cmd(0) and cmd(1) ). Add the following to this form:

Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ©1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'index of the OK command button
Const btnOK = 0

Private Sub cmd_Click(Index As Integer)

 'Regardless of the button, just unload.
 'If a selection was made it was set
 'in the list click event.
  Unload Me
   
End Sub


Private Sub Form_Load()

  Dim r As Long
  Dim pos As Long
  Dim start As Long
  Dim ret As String
  Dim sSection As String
  
 'centre this form on the screen
  Me.Move (Screen.Width - Me.Width) \ 2, (Screen.Height - Me.Height) \ 2
  
 'Get the list of sections form the file.
 'Below, the string is parsed to remove each
 'section for the list, but it only adds those
 'sections with the word "Questions" in it. This
 'will allow this same INI file to be used for
 'other purposes if desired (such as saving user settings).
  ret = Space$(2048)
  r = GetPrivateProfileString(vbNullString, _
                              vbNullString, _
                              "", _
                              ret, _
                              Len(ret), _
                              sIniQFile)
  
 'if something returned (r is > 0)
  If r Then
  
   'trim..
    ret = Left$(ret, r)
    
   'set the Instr() start pos..
    start = 1
    
   '..and get each question set
    Do Until pos >= Len(ret)
    
      pos = InStr(start, ret, Chr$(0))
      
      If pos Then
      
        sSection = Mid$(ret, start, pos - 1)
        
        If InStr(sSection, "Questions") Then
           List1.AddItem sSection
        End If
        
       'reset the start position and continue
        start = pos + 1
      
      End If 'If pos ...
      
    Loop
      
   End If  'If r ...
   
   
  'disable the OK button until a selection is made
   cmd(btnOK).Enabled = False
   
End Sub


Private Sub List1_Click()

  'enable the OK button when a selection is made
   cmd(btnOK).Enabled = List1.ListIndex > -1
   
  'and assign the selection to the ini
  'filename public variable
   iniQuizSection = List1.List(List1.ListIndex)

End Sub


Private Sub List1_DblClick()

 'invoke the OK button
  cmd(btnOK).Value = True
  
End Sub
 Comments
Save this file and head for the home stretch ....Step 4 - Building the Main Quiz Form. (And still don't bother to run the app yet)

 
 

PayPal Link
Make payments with PayPal - it's fast, free and secure!

 
 
 
 

Copyright ©1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy

 

Hit Counter