如何保存对话框的状态

maolz6 2004-12-06 12:11:57
比如我勾选了一个选项框,但是下次再启动,它又变成不勾选了,一般这种情况是该如何操作的,请指点
...全文
126 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
laviewpbt 2004-12-06
  • 打赏
  • 举报
回复
最简单的方法
Private Sub Form_Load()
Check1.Value = GetSetting(App.Title, Me.Name, "check1", 0)
End Sub

Private Sub Form_Unload(Cancel As Integer)
Call SaveSetting(App.Title, Me.Name, "check1", Check1.Value)
End Sub
happy_sea 2004-12-06
  • 打赏
  • 举报
回复
在程序中用SetIniN保存程序设置,下次运行时用GetIniN读取设置,然后据此设置选项框的状态即可。
记得给IniFile赋值,这是用来保存设置的文件名,比如:IniFile=app.path+"\Myini.ini"
happy_sea 2004-12-06
  • 打赏
  • 举报
回复
Option Explicit
Public IniFile As String
Public nChecked As Integer
Public Declare Function GetPrivateProfileInt Lib "kernel32" Alias "GetPrivateProfileIntA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal nDefault As Long, ByVal lpFileName As String) As Long
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 Long
Public 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


Function GetIniS(ByVal SectionName As String, ByVal KeyWord As String, ByVal DefString As String) As String

Dim ResultString As String * 144, Temp As Integer
Dim s As String, i As Integer
Temp% = GetPrivateProfileString(SectionName, KeyWord, "", ResultString, 144, IniFile)
'检索关键词的值
If Temp% > 0 Then '关键词的值不为空
s = ""
For i = 1 To 144
If Asc(Mid$(ResultString, i, 1)) = 0 Then
Exit For
Else
s = s + Mid$(ResultString, i, 1)
End If
Next
Else
Temp% = WritePrivateProfileString(SectionName, KeyWord, DefString, IniFile)
'将缺省值写入INI文件
s = DefString
End If
GetIniS = s
End Function
Function GetIniN(ByVal SectionName As String, ByVal KeyWord As String, ByVal DefValue As Long) As Long

Dim d As Long, s As String
d = DefValue
GetIniN = GetPrivateProfileInt(SectionName, KeyWord, DefValue, IniFile)
If d <> DefValue Then
s = "" & d
d = WritePrivateProfileString(SectionName, KeyWord, s, IniFile)
End If
End Function
Sub SetIniS(ByVal SectionName As String, ByVal KeyWord As String, ByVal ValStr As String)

Dim res%
res% = WritePrivateProfileString(SectionName, KeyWord, ValStr, IniFile)
End Sub
Sub SetIniN(ByVal SectionName As String, ByVal KeyWord As String, ByVal ValInt As Long)
Dim res%, s$
s$ = Str$(ValInt)
res% = WritePrivateProfileString(SectionName, KeyWord, s$, IniFile)
End Sub
happy_sea 2004-12-06
  • 打赏
  • 举报
回复
保存在ini文件中是最好的方法。
maolz6 2004-12-06
  • 打赏
  • 举报
回复
多谢两位
happy_sea 2004-12-06
  • 打赏
  • 举报
回复
具体实现步骤:首先把以上代码添加到模块中,然后在窗体中:

Option Explicit

IniFile = IIf(Right(App.Path, 1) = "\", App.Path + "MySet.ini", App.Path + "\MySet.ini") '设置IniFile

Private Sub Form_Load() '运行程序时读取设置并设置Check1是否勾选
Check1.Value = GetIniN("LastSet", "IfChecked", 1)
End Sub

Private Sub Form_Unload(Cancel As Integer) '在退出程序时保存当前设置
SetIniN "LastSet", "IfChecked", Check1.Value
End Sub

要存取数值型的用SetIniN和GetIniN,如果是字符串型的用SetIniS和GetIniS。

1,502

社区成员

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

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