LoadResString Function |
LoadResString(resID)
resID
Use: Required
Data Type: Variant
A numeric value specifying the resource ID of the string to load.
A String.
Retrieves a string from a resource file (.RES) that can be assigned to a string variable or to the string property of a control, such as a label caption.
The strings to be retrieved by LoadResString must be included in a resource (.RES ) file. Each must be assigned a unique identifier, which is typically represented by a numeric constant.
The identifier 1 is reserved for use by the application icon.
LoadResString is part of the VB Runtime Library and isn't available in VBA applications.
Using resource strings is the ideal way to internationalize your application, as the following example demonstrates.
Unlike the LoadResData function, LoadResString returns an ANSI string that can be directly assigned to a form or control property.
VB6 includes an add-in Resource Editor; see the sidebar in the previous entry for more information.
This example demonstrates how to use LoadResString in conjunction with the GetLocaleInfo API call to internationalize a VB application. This example assumes that a resource file containing menu caption strings in various languages has been created and added to the project. A Form_Load event could call the getLanguage function, which in turns calls the relevant API function to return the current language of the machine. This value then passes a code to a function that retrieves the relevant menu caption and assigns it to the menu object. This example uses the language string constant purely to make the code easier to read; however, in a real application, you should return the language ID number from the API call by using the LOCALE_ILANGUAGE constant.
Option Explicit Public Const LOCALE_SLANGUAGE = &H2 Public Const LOCALE_ILANGUAGE = &H1 Declare Function GetLocaleInfo Lib "kernel32" Alias _ "GetLocaleInfoA" (ByVal Locale As Long, _ ByVal LCType As Long, ByVal lpLCData As String, _ ByVal cchData As Long) As Long Public Function getLanguage() As Boolean Dim lReturn As Long Dim lLocID As Long Dim lType As Long Dim sData As String Dim lDataLen As Long lDataLen = 0 'passing 0 as the data length returns the required 'size of the string lReturn = GetLocaleInfo(lLID, LOCALE_SLANGUAGE, sData, _ lDataLen) 'create a null terminated buffer of the correct length sData = String(lReturn, 0) & vbNullChar 'assign the length lDataLen = lReturn 'call the API funtion again; this time sData will 'be assigned the Language name lReturn = GetLocaleInfo(lLID, LOCALE_SLANGUAGE, sData, _ lDataLen) 'determine which language is being used. Select Case UCase$(Left$(sData, 6)) 'pass across a code to the SetCaptions function Case Is = "ENGLIS" SetCaptions 1000 Case Is = "FRENCH" SetCaptions 2000 Case Is = "GERMAN" SetCaptions 3000 Case Is = "SPANIS" SetCaptions 4000 End Select End Function Private Function SetCaptions(iCode As Integer) As Boolean 'assign strings from the Res file for each caption 'i.e., The English "Open File" caption will have an 'ID of 1001 mnuOpenFile.Caption = LoadResString(iCode + 1) mnuCloseFile.Caption = LoadResString(iCode + 2) mnuEdit.Caption = LoadResString(iCode + 3) mnuCompleteQuestions.Caption = LoadResString(iCode + 4) mnuDelete.Caption = LoadResString(iCode + 5) End Function