Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 305
  • Last Modified:

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

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
  • 2
1 Solution
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
zubair007Author Commented:
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
Who's bai jan?

You don't have to use the extension *.ini. It will still work with a name like MyFile.txt.
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!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now