Use INI files in VB to implement a daily post dialog

zhaozj2021-02-16  50

First create a new project file, add a module, two forms. The form is named frmmain and fmtips, respectively. Add a button to frmmain, double-click the following code: Private Sub CMDok_Click () Unload Mend Sub Add the following controls on the FRMTIPS form: three buttons, named: cmdok, cmdpretip, and cmdnexttip, the caption value is set to: "OK", "Previous" and "Next" (in order to make the program more intuitive, the author applies a picture on the CMDok button, see Figure 1). , Two check boxes, named: Chkiftips Add a Picturebox as a background, a Label as a title, and finally design is shown in the figure.

Then add a module in which the following code is entered: Option ExplicitPublic TipFileName As String 'prompt information file public inIfilename As String' The following two API functions, the author suggests that from the VB combo API browser Copy, otherwise, if there is a letter written wrong, the program can't run correctly! Public Declare Function GetPrivateProfileString Lib "kernel32" Alias ​​_ "GetPrivateProfileStringA" (ByVal lPAPPlicationName As String, ByVal lPKeyName As Any, ByVal lPDefault As String, ByVal lPReturnedString As String, ByVal nSize As Long, ByVal lPFileName As String) As LongPublic Declare Function WritePrivateProfileString Lib _ "kernel32" Alias ​​"WritePrivateProfileStringA" (ByVal lPAPPlicationName As String, ByVal lPKeyName As Any, ByVal lPString As Any, ByVal lPFileName As String) As LongSub GetFile () Dim AppName As StringAppName = App.PathIf Right (AppName, 1) <> "/" TENAPNAME = Appname & "/" end iftipfilename = appname & "tipofday.txt" 'Please create a text file in advance, and put it in the same directory with the project file (this file is for reference only) InIfileName = Appname & "TIPOFDAY.INI" 'this document we do not control, the system will automatically create the End SubPublic Function sGetINI (INIFileName As String, sSection As String, sKey As String, sDefault As String) As StringDim sTemP As String * 256Dim nLength As LongsTemP = Space $ (256) NLENGTH = GetPrivateProfileString (SSECTION, SKEY, SDEFAULT, STEMP, 255, INI FileName) sGetINI = Left $ (sTemP, nLength) End FunctionPublic Sub writeINI (INIFileName As String, sSection As String, sKey As String, sValue As String) Dim n As LongDim sTemP As StringsTemP = sValue 'replacing the carriage return with a space / line For n = 1 to Len (Svalue) IF MID $ (Svalue, N, 1) = VBCR or MID $ (Svalue, N, 1) = VBLF THENMID $ (SVALUE, N) = "" End ifnext nn =

WritePrivateProfileString (sSection, sKey, sTemP, INIFileName) End SubSub main () Dim ifStartTips As StringDim sNumUPPer As String, sNumOneZhu As StringCall GetFileLoad frmMainifStartTips = sGetINI (INIFileName, "Others", "ifStartTips", "YES") frmMain.ShowIf ifStartTips = "Yes" thenfrmtip.show vbmodal, frmmainfrmtip.chkiftips.value = vbChecked IFEND SUB Added code in frmtip: Option Explicit 'The prompt database in memory. DIM TIPS AS New Collection 'prompt file name const tip_file = "tipofDay.txt"' The index of the prompt collection currently displayed. DIM CURRENTTIP As long, ifXext as booleanprivate sub donextttip () if Chkifrnd.Value = vbChecked Then 'Randomly selects a prompt. CURRENTTIP = INT (Tips.count * RND) 1) Else 'or you can traverse CURRENTTIP = CURRENTTIP 1IF TIPS.COUNT

DoNextTipLoadTips = TrueEnd FunctionPrivate Sub chkIfRnd_Click () Dim ifRndShow As String 'stored at the next start whether the random message appears ifRndShow = IIf (chkIfRnd.Value = vbChecked, "YES", "NO") Call writeINI (INIFileName, "Others", "ifRndShow", ifRndShow) End SubPrivate Sub chkIfTips_Click () Dim ifStartTips As String 'stored is displayed at the next start this form ifStartTips = IIf (chkIfTips.Value = 1, "YES", "NO") Call writeINI (INIFileName, "Others", "ifStartTips", ifStartTips) End SubPrivate Sub cmdNextTip_Click () ifNext = TrueCall DoNextTipEnd SubPrivate Sub cmdOK_Click () Unload MeEnd SubPrivate Sub cmdPreTip_Click () ifNext = FalseCall DoPreTipEnd SubPrivate Sub Form_Load () Dim ifStartTips As String, ifRndShow As String ' Whether to display the prompt information when starting ifstarttips = sgetini (IniFileName, "Others", "?") If ifstarttips = "?",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,. Ifstarttips) Endiffs.Value = IIF (ifstarttips = "no", vbunchecked, vbChecked) 'When the display is randomly displaying IFRNDSHOW = sgetini (IFILENAME, "Others", "IFRNDSHOW", "?") If IFRNDSHOW = "?" THENIFRNDSHOW = "YES" CALL WRITEINI (IFRNDSHOW ", IFRNDSHOW) Endness.Value = Iif (ifrndshow =" no ", vbunchecked , vbChecked) 'Randomly looks for ifXExt = truerandomize' reads the prompt file and randomly displays a prompt.

If LoadTips (TipFileName) = False ThenlblTipText.Caption = "file" & Tip_FILE & "not found!" End IfEnd SubPublic Sub DisPlayCurrentTip () If Tips.Count> 0 ThenIf Tips.Item (CurrentTip) = "" ThenIf ifNext = True ThenCall DonextTIPELSECALL DOPRETIPEND IFEND IFELBLTIPTEXT.CAPTION = Tips.Item (CURRENTTIP) End IFEND SUB Related Resources: VB Read and write typologies for INI files

转载请注明原文地址:https://www.9cbs.com/read-16973.html

New Post(0)