请问如何实现程序打开时dirlistbox中显示的目录是上次程序运行时选择的路径

gush 2005-04-14 09:04:27
如题,先行谢过!
...全文
102 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
roger_xiong 2005-04-15
  • 打赏
  • 举报
回复
如楼上说,每次程序退出的时候要保存程序退出时所选择的路径,下次恢复就好了。至于保存可以保存到数据库,也可以保存到 INI 文件,下面给出保存到 INI 文件的简单例子

'***********************************
'** 窗体代码,放一个DirList
'***********************************
Option Explicit

Private strIniFile As String

Private Sub Form_Load()

strIniFile = App.Path & IIf(Right(App.Path, 1) = "\", "", "\") & "Setting.ini"

Dim strPath As String
strPath = mdlIniFile.ReadINI("Main", "Path", strIniFile)
Dir1.Path = IIf(strPath = "", App.Path, strPath)
End Sub

Private Sub Form_Unload(Cancel As Integer)
Call mdlIniFile.WriteINI("Main", "Path", Dir1.Path, strIniFile)
End Sub




'***********************************
'** 模块代码,模块名:mdlIniFile
'***********************************

'******************************************************************************
'**
'** 模块:mdlIniFile
'** 作用:操作 INI 文件
'**
'******************************************************************************

Option Explicit

Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
Private 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 Long

Public Function ReadINI(sSection As String, sKeyName As String, sINIFileName As String) As String

On Local Error Resume Next

Dim sRet As String
Dim ls As String

sRet = String(255, " ")

ls = Left(sRet, GetPrivateProfileString(sSection, ByVal sKeyName, "", sRet, Len(sRet), sINIFileName))
ls = Trim(ls)

If Right(ls, 1) = Chr(0) Then ls = Left(ls, Len(ls) - 1)

ReadINI = ls
End Function

Public Function WriteINI(sSection As String, sKeyName As String, sNewString As String, sINIFileName As String) As Boolean

On Local Error Resume Next

Call WritePrivateProfileString(sSection, sKeyName, sNewString, sINIFileName)

WriteINI = (Err.Number = 0)
End Function



EisenHammer 2005-04-14
  • 打赏
  • 举报
回复
每次运行程序时选择了路径就用SaveSetting语句把路径保存到注册表中去,下次打开的时候用GetSetting 函数得到上次保存的路径就可以了。

具体语法参考MSDN。

1,451

社区成员

发帖
与我相关
我的任务
社区描述
VB 控件
社区管理员
  • 控件
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧