How to savesetting of all form in a textfile and gain these Savesettings again.

Posted on 2003-03-26
Medium Priority
Last Modified: 2010-04-17
How to savesetting in a textfile and gain these Savesettings again.
and also tell me can i save all form setting in the registry in one line ornot please tell me
Question by:zubair007
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 76

Accepted Solution

GrahamSkan earned 80 total points
ID: 8208954
To save setttings in a file like Myini.ini (as opposed to the registry):

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 Write_Profile_Info(ByVal InAppName As String, ByVal InKeyName As String, ByVal InString As String, ByVal InFileName As String) As Integer
    Dim x As Integer
    On Error GoTo WriteProfileInformationError

    x = WritePrivateProfileString(InAppName, InKeyName, InString, InFileName)

    Exit Function

    WriteProfileInformation = 0
    Exit Function

End Function

******* To retrieve the settings *********:

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

Function Get_Profile_Info(ByVal InAppName As String, ByVal InKeyName As String, InFileName As String) As String
    Dim ReturnedStringLength%
    Dim TempString$, Zero
        'WriteLog "Get_Profile_Info: InAppName: " & InAppName & ", InKeyName: " & InKeyName & ", InFileName: " & InFileName
    lpAppName$ = InAppName
    lpKeyName$ = InKeyName
    lpDefault$ = Space$(250)
    lpReturnedString$ = Space$(250)
    nSize% = Len(lpReturnedString$)
    lpFileName$ = InFileName
    ReturnedStringLength% = GetPrivateProfileString(lpAppName$, lpKeyName$, lpDefault$, lpReturnedString$, nSize%, lpFileName$)
    If ReturnedStringLength% = 0 Then
        Get_Profile_Info = ""
        Exit Function
    End If
    'Windows NT doesn't always return zero length when not found
    'so trim and truncate again, just in case
    TempString$ = Trim$(Left$(lpReturnedString$, ReturnedStringLength%))
    Zero = InStr(TempString$, Chr$(0))
    If Zero > 0 Then
        TempString$ = Left$(TempString$, Zero - 1)
    End If
    'WriteLog "Get_Profile_Info: Returns " & TempString$
    Get_Profile_Info = TempString$
End Function

 *********** To save and restore form settings, hook these events in each form:
Private Sub Form_Unload(Cancel As Integer)
    SaveLayout Me
End Sub

Private Sub Form_Load()
    SetMeUp Me
End Sub

******* to these procedures
Public Sub SetMeUp(Frm As Form)
Dim SaveLeft As Long
Dim SaveTop As Long
Dim UseSettings As Boolean
Const Margin = 0.05
SaveLeft = Val(GetSetting(App.Title, "Form Layouts", Frm.Name & "_Left"))
SaveTop = Val(GetSetting(App.Title, "Form Layouts", Frm.Name & "_Top"))
'check to see if form will restore within screen bounds
    If SaveLeft + Frm.Width > Screen.Width * Margin Then
        If SaveLeft < Screen.Width * (1 - Margin) Then
            If SaveTop > 0 Then
                If SaveTop < Screen.Height * (1 - Margin) Then
                    UseSettings = True
                End If
            End If
        End If
    End If
    If UseSettings Then
        Frm.Left = SaveLeft
        Frm.Top = SaveTop
        If Frm.BorderStyle = vbSizable Then
            If GetSetting(App.Title, "Form Layouts", Frm.Name & "_Height") <> "" Then
                Frm.Height = Val(GetSetting(App.Title, "Form Layouts", Frm.Name & "_Height"))
                Frm.Width = Val(GetSetting(App.Title, "Form Layouts", Frm.Name & "_Width"))
            End If
        End If
        Frm.Left = (Screen.Width - Frm.Width) / 2
        Frm.Top = (Screen.Height - Frm.Height) / 2
    End If
End Sub

Public Sub SaveLayout(Frm As Form)
    SaveSetting App.Title, "Form Layouts", Frm.Name & "_Left", "" & Frm.Left
    SaveSetting App.Title, "Form Layouts", Frm.Name & "_Top", Frm.Top
    If Frm.BorderStyle = vbSizable Then
        SaveSetting App.Title, "Form Layouts", Frm.Name & "_Height", Frm.Height
        SaveSetting App.Title, "Form Layouts", Frm.Name & "_Width", Frm.Width
    End If
End Sub

Author Comment

ID: 8212682
no no bai jan i want to save all setting in the Mytext.txt file only for this that to avoid too much coding that is why i know this process ..........i want to use text file whose extension *.txt
LVL 76

Expert Comment

ID: 8215975
Who's bai jan?

You don't have to use the extension *.ini. It will still work with a name like MyFile.txt.

Expert Comment

ID: 9446776
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
Post your closing recommendations!  No comment means you don't care.

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question