How to Get Program Manager Group Names into Combo Box in VB (80410)



The information in this article applies to:

  • Microsoft Visual Basic Standard Edition for Windows 2.0
  • Microsoft Visual Basic Standard Edition for Windows 3.0
  • Microsoft Visual Basic Professional Edition for Windows 2.0
  • Microsoft Visual Basic Professional Edition for Windows 3.0
  • Microsoft Visual Basic Standard Edition for Windows 1.0

This article was previously published under Q80410

SUMMARY

To get a list of group names in the Windows 3.0 Program Manager, you can call the Windows API GetPrivateProfileString function from a Visual Basic program. This article describes a method of using the Windows API GetPrivateProfileString function to get all the group names from Program Manager and place them into a Visual Basic combo box.

MORE INFORMATION

Windows initialization (.INI) files contain information that defines your Windows environment. Examples of Windows initialization files are WIN.INI and SYSTEM.INI, which are commonly found in the C:\WINDOWS subdirectory. Windows and Windows-based applications can use the information stored in these files to configure themselves to meet your needs and preferences. For a description of initialization files, read the WININI.TXT file that comes with Microsoft Windows 3.0.

An initialization file is composed of at least an application name and a key name. The contents of Windows initialization files have the following format:
   [Application name]
   keyname=value
				

The GetPrivateProfile family of API functions are used to retrieve information from any initialization file that you specify.

To declare this API function within your program, include the following Declare statement in the global module or the general Declarations section of a Visual Basic form. The entire Declare statement must be on one, single line.
Declare Function GetPrivateProfileString% Lib "Kernel"
  (ByVal lpAppName$, ByVal lpKeyName$, ByVal lpDefault$,
   ByVal lpReturnedString$, ByVal nSize%, ByVal lpFileName$)
				

The formal arguments to these functions are described as follows:
Argument           Description
---------------------------------------------------------------------------
lpAppName$         Name of a Windows-based application that appears in the
                   .INI file.

lpKeyName$         Key name that appears in the .INI file.

lpFileName$        Points to a string that names the .INI file. If
                   lpFileName does not contain a path to the file,
                   Windows searches for the file in the Windows
                   directory.

lpDefault$         Specifies the default value for the given
                   key if the key cannot be found in the
                   .INI file.

lpReturnedString$  Specifies the buffer that receives the character
                   string.

nSize%             Specifies the maximum number of characters
                   (including the last null character) to be copied to
                   the buffer.
				

Code Example

To get the group names from Program Manager into a combo box, do the following:
  1. Start Visual Basic or from the File menu, select New Project (ALT, F, N) if Visual Basic is already running. Form1 will be created by default.
  2. Add a combo box (Combo1) to Form1.
  3. Within the global Declarations section of Form1, add the following Windows API function declaration. Note that the Declare statement below must appear on a single line.
       Declare Function GetPrivateProfileString% Lib "kernel"
         (ByVal lpAppName$, ByVal lpKeyName$,ByVal
          lpDefault$,ByVal lpReturnString$,ByVal nSize%,
          ByVal lpFileName$)
    
    						
  4. Within the Form_Load event procedure for Form1, add the following code:
       Sub Form_Load()
          ' This is the name of the group in the PROGMAN.INI file
          lpAppName$ = "Groups"
    
          ' All group names start with Group: Group1, Group2, etc.
          lpKeyName$ = "Group"
    
          ' If no group found return value in lpDefault$
          lpDefault$ = ""
    
          ' Initialize string
          lpReturnString$ = Space$(128)
          Size% = Len(lpReturnString$)
    
          ' This is the path and name the PROGMAN.INI file.
          lpFileName$ = "c:\windows\progman.ini"
    
          Valid% = 1
          i% = 0
    
          While (Valid%)
    
            i% = i% + 1
    
            ' The following three lines must be typed on a single line
            Valid% = GetPrivateProfileString(lpAppName$, lpKeyName$
                   + LTrim$(Str$(i%)), lpDefault$, lpReturnString$,
                   Size%, lpFileName$)
    
            ' Discard the trailing spaces and null character.
            group$ = Left$(lpReturnString$, Valid%)
    
            ' check to see if string was returned. Change arguments
            ' passed to the Mid$ statement to change what is displayed in combo
            ' box. By setting number to 15 this strips c:\windows\ 
            ' and .GRP
            ' The following 2 lines must be on one line
            If Valid% > 0 Then combo1.AddItem Mid(group$, 12,
                  Len(group$) - 15)
          Wend
    
          ' Set text of combo box to first item in list
          combo1.listindex = 0
    
       End Sub
    
    						
  5. From the Run menu, choose Start (ALT, R, S). The combo box will contain the filenames (without the extension) of the group (.GRP extension) files in the Windows directory. The group name conforms to the MS-DOS filename convention; it is limited to eight characters.

Modification Type:MajorLast Reviewed:12/12/2003
Keywords:KB80410