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

x
  • 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
0
zubair007
Asked:
zubair007
  • 2
1 Solution
 
GrahamSkanRetiredCommented:
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

WriteProfileInformationError:
    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
    Else
        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
0
 
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
0
 
GrahamSkanRetiredCommented:
Who's bai jan?

You don't have to use the extension *.ini. It will still work with a name like MyFile.txt.
0
 
CleanupPingCommented:
zubair007:
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 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0

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