Re: [RESOLVED] Save Settings
You can use IniFile class to save any setting you want with proper format you decide.
For example to save any form location, size and window state, add the following two methods to IniFile class
Code:
Public Sub ReadFormPosition(ByVal strSectionName As String, ByVal strFileName As String, ByVal frm As Form, Optional ByVal blnReadSize As Boolean = True, Optional ByVal blnReadWindowState As Boolean = True)
'Window state|Left|Top|Width|Height
Dim strPos() As String
strPos = Split(ReadValue(strSectionName, frm.Name & "Window", CStr(frm.WindowState) & "|" & CStr(frm.Left) & "|" & CStr(frm.Top) & "|" & CStr(frm.Width) & "|" & CStr(frm.Height), strFileName), "|")
frm.Left = CSng(strPos(1))
frm.Top = CSng(strPos(2))
If blnReadSize Then
frm.Width = CSng(strPos(3))
frm.Height = CSng(strPos(4))
End If
If blnReadWindowState Then
frm.WindowState = CInt(strPos(0))
End If
End Sub
Public Sub WriteFormPosition(ByVal strSectionName As String, ByVal strFileName As String, ByVal frm As Form)
'Window state|Left|Top|Width|Height
If frm.Visible Then
If frm.WindowState = vbNormal Then ' Write all properties
WriteValue strSectionName, frm.Name & "Window", CStr(frm.WindowState) & "|" & CStr(frm.Left) & "|" & CStr(frm.Top) & "|" & CStr(frm.Width) & "|" & CStr(frm.Height), strFileName
ElseIf frm.WindowState = vbMaximized Then 'Write only window state and preserve old size and location
WriteValue strSectionName, frm.Name & "Window", CStr(frm.WindowState) & Mid$(ReadValue(strSectionName, frm.Name & "Window", "0|150|150|12000|90000", strFileName), 2), strFileName
End If
End If
End Sub
Usage:
in any form use code like this
Code:
Private Sub Form_Load()
cINI.ReadFormPosition "SectionName", mstrSettingFile, Me
End Sub
Private Sub Form_Unload(Cancel As Integer)
cINI.WriteFormPosition "SectionName", mstrSettingFile, Me
End Sub
Re: [RESOLVED] Save Settings
Quote:
Originally Posted by
4x2y
You can use
IniFile class to save any setting you want with proper format you decide.
For example to save any form location, size and window state, add the following two methods to
IniFile class
Code:
Public Sub ReadFormPosition(ByVal strSectionName As String, ByVal strFileName As String, ByVal frm As Form, Optional ByVal blnReadSize As Boolean = True, Optional ByVal blnReadWindowState As Boolean = True)
'Window state|Left|Top|Width|Height
Dim strPos() As String
strPos = Split(ReadValue(strSectionName, frm.Name & "Window", CStr(frm.WindowState) & "|" & CStr(frm.Left) & "|" & CStr(frm.Top) & "|" & CStr(frm.Width) & "|" & CStr(frm.Height), strFileName), "|")
frm.Left = CSng(strPos(1))
frm.Top = CSng(strPos(2))
If blnReadSize Then
frm.Width = CSng(strPos(3))
frm.Height = CSng(strPos(4))
End If
If blnReadWindowState Then
frm.WindowState = CInt(strPos(0))
End If
End Sub
Public Sub WriteFormPosition(ByVal strSectionName As String, ByVal strFileName As String, ByVal frm As Form)
'Window state|Left|Top|Width|Height
If frm.Visible Then
If frm.WindowState = vbNormal Then ' Write all properties
WriteValue strSectionName, frm.Name & "Window", CStr(frm.WindowState) & "|" & CStr(frm.Left) & "|" & CStr(frm.Top) & "|" & CStr(frm.Width) & "|" & CStr(frm.Height), strFileName
ElseIf frm.WindowState = vbMaximized Then 'Write only window state and preserve old size and location
WriteValue strSectionName, frm.Name & "Window", CStr(frm.WindowState) & Mid$(ReadValue(strSectionName, frm.Name & "Window", "0|150|150|12000|90000", strFileName), 2), strFileName
End If
End If
End Sub
Usage:
in any form use code like this
Code:
Private Sub Form_Load()
cINI.ReadFormPosition "SectionName", mstrSettingFile, Me
End Sub
Private Sub Form_Unload(Cancel As Integer)
cINI.WriteFormPosition "SectionName", mstrSettingFile, Me
End Sub
Thank you dear 4x2y for this nice codes, I'm new in programming therefore I don't understand codes like this. These codes are very complicate for me and I couldn't use it in my project (I didn't understand how to use it) . . . But however Many thanks for codes you are very kind :)
Sincerely
Mahmood Kh Pirani
Re: [RESOLVED] Save Settings
To use IniFile class with any project you have:
(1) Open your project with VB6 IDE
(2) Go to Project menu, select Add Class Module, select Existing tab and browse for IniFile.cls
Now you already know how to use it from above sample project.
Re: [RESOLVED] Save Settings
I know that, to a large degree, this is a matter of personal taste. But personally, I'd only create an INI file as a last resort. As with many cases, this is a matter of understanding the scope and lifetime of the data you wish to save.
In another thread, I recently outlined my view on scope and lifetime of various data. That can be found here. You'll notice that any mention of an INI file is the last bullet-point on my list.
Best Regards,
Elroy
Re: [RESOLVED] Save Settings
@Elroy. Externally persisted settings (INI, db, txt, etc) are often easier to migrate than those saved in the registry. That is an advantage if it applies. If moderate security is needed, both internal/external, encoding can be considered.
Re: [RESOLVED] Save Settings
@LaVolpe: I suppose you're correct, but it does often come down to the scope (and lifetime) I'm after. For me, sure, I've got a database, and there are tables in it specifically for storing all kinds of settings.
However, this is also a multi-user program (all using a common database). For instance, the first screen that comes up shows the patient list. And I like to have it "default" to the last patient that was being examined. But ... this "last patient" will be different depending on whose workstation is being used, and also who's logged into a particular workstation. Therefore, this is a perfect scenario for making use of the registry.
I must admit that I use all combinations of that list I linked to in the other thread. It truly just depends on the scope and lifetime I'm after. There's only one place where I actually make use of an INI file, but I won't outline it here. In all other cases, stuffing settings in the database or stuffing them into the registry works ideally.
And yes, regarding migration, which occasionally happens when a file server is replaced, all the settings at that level are best stuffed into the database.
Best Regards,
Elroy
Re: [RESOLVED] Save Settings
External settings are usually best in my work. We have workstations and servers that are replaced every few years. Vast majority of app settings were ini/config based. This made it easy to transfer to new equipment. The more recently developed apps use an SQL database and that database is exported and uploaded on the new equipment as needed. However, the database location/ip and connection information is stored externally to file. Being government and high system turnover/upgrades, along with restricted user rights, the external settings work best for us.
Edited: A nightmare scenario shortly after I was hired. We upgraded and replaced some servers. Several compiled, homegrown applications were hardcoded with the server name they connected to (no source code remained). Well, that server was gone and new one set up using a different naming convention. So fortunate they were written in VB and I could find their text in the executable. Even more luck... the new server name was bigger than the old, but the old server name was just longer than the new server's IP address. I just "hacked the exe" and overwrote the old server name with the new server's IP (adding a leading zero or two to fill out the space) and presto: up & running within a short time. Those apps have since been rewritten. I think we can all agree that the worst place to store settings (that could possibly change) is within the app itself
Re: [RESOLVED] Save Settings
Quote:
Originally Posted by
LaVolpe
I think we can all agree that the worst place to store settings (that could possibly change) is within the app itself
hahaha, yes, on THAT we absolutely agree.