Deprecated: The behavior of unparenthesized expressions containing both '.' and '+'/'-' will change in PHP 8: '+'/'-' will take a higher precedence in /home/iano/public_html/tpforums-vb5/forum/includes/class_core.php on line 5842

PHP Warning: Use of undefined constant MYSQL_NUM - assumed 'MYSQL_NUM' (this will throw an Error in a future version of PHP) in ..../includes/init.php on line 165

PHP Warning: Use of undefined constant MYSQL_ASSOC - assumed 'MYSQL_ASSOC' (this will throw an Error in a future version of PHP) in ..../includes/init.php on line 165

PHP Warning: Use of undefined constant MYSQL_BOTH - assumed 'MYSQL_BOTH' (this will throw an Error in a future version of PHP) in ..../includes/init.php on line 165

PHP Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in ..../includes/functions_navigation.php on line 588

PHP Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in ..../includes/functions_navigation.php on line 612

PHP Warning: Use of undefined constant misc - assumed 'misc' (this will throw an Error in a future version of PHP) in ..../global.php(29) : eval()'d code(6) : eval()'d code on line 1

PHP Warning: Use of undefined constant index - assumed 'index' (this will throw an Error in a future version of PHP) in ..../global.php(29) : eval()'d code(6) : eval()'d code on line 1

PHP Warning: Use of undefined constant misc - assumed 'misc' (this will throw an Error in a future version of PHP) in ..../includes/class_bootstrap.php(1422) : eval()'d code(4) : eval()'d code on line 1

PHP Warning: Use of undefined constant index - assumed 'index' (this will throw an Error in a future version of PHP) in ..../includes/class_bootstrap.php(1422) : eval()'d code(4) : eval()'d code on line 1

PHP Warning: Use of undefined constant onlinestatusphrase - assumed 'onlinestatusphrase' (this will throw an Error in a future version of PHP) in ..../includes/class_core.php(4684) : eval()'d code on line 6

PHP Warning: Use of undefined constant onlinestatusphrase - assumed 'onlinestatusphrase' (this will throw an Error in a future version of PHP) in ..../includes/class_core.php(4684) : eval()'d code on line 85

PHP Warning: Use of undefined constant onlinestatusphrase - assumed 'onlinestatusphrase' (this will throw an Error in a future version of PHP) in ..../includes/class_core.php(4684) : eval()'d code on line 6

PHP Warning: Use of undefined constant onlinestatusphrase - assumed 'onlinestatusphrase' (this will throw an Error in a future version of PHP) in ..../includes/class_core.php(4684) : eval()'d code on line 6

PHP Warning: Use of undefined constant onlinestatusphrase - assumed 'onlinestatusphrase' (this will throw an Error in a future version of PHP) in ..../includes/class_core.php(4684) : eval()'d code on line 6
Settings Class
Results 1 to 4 of 4

Thread: Settings Class

  1. #1

    Settings Class

    First, heres an ini file class.

    Found this online, no credits to me except for some minor edits.

    [code=vb]Public Class iniFile
    Dim strFilename As String

    ' Constructor, accepting a filename
    Public Sub New(ByVal Filename As String)
    strFilename = Filename
    End Sub

    ' Read-only filename property
    ReadOnly Property FileName() As String
    Get
    Return strFilename
    End Get
    End Property

    #Region "Get"
    Public Function GetString(ByVal Section As String, _
    ByVal Key As String, ByVal [Default] As String) As String
    ' Returns a string from your INI file
    Dim intCharCount As Integer
    GetString = [Default]
    Dim objResult As New System.Text.StringBuilder(500)
    intCharCount = GetPrivateProfileString(Section, Key, _
    [Default], objResult, objResult.Capacity, strFilename)
    If intCharCount > 0 Then
    GetString = Left(objResult.ToString, intCharCount)
    End If
    End Function

    Public Function GetInteger(ByVal Section As String, _
    ByVal Key As String, ByVal [Default] As UInt32) As UInt32
    ' Returns an integer from your INI file
    Return GetPrivateProfileInt(Section, Key, _
    [Default], strFilename)
    End Function

    Public Function GetShort(ByVal Section As String, _
    ByVal Key As String, ByVal [Default] As UShort) As UShort
    ' Returns an integer from your INI file
    Return GetPrivateProfileInt(Section, Key, _
    [Default], strFilename)
    End Function

    Public Function GetBoolean(ByVal Section As String, _
    ByVal Key As String, ByVal [Default] As Boolean) As Boolean
    ' Returns a boolean from your INI file
    Return (GetPrivateProfileInt(Section, Key, _
    CInt([Default]), strFilename) = 1)
    End Function
    #End Region

    #Region "Write"
    Public Sub WriteString(ByVal Section As String, _
    ByVal Key As String, ByVal Value As String)
    ' Writes a string to your INI file
    WritePrivateProfileString(Section, Key, Value, strFilename)
    Flush()
    End Sub

    Public Sub WriteInteger(ByVal Section As String, _
    ByVal Key As String, ByVal Value As Integer)
    ' Writes an integer to your INI file
    WriteString(Section, Key, CStr(Value))
    Flush()
    End Sub

    Public Sub WriteInteger(ByVal Section As String, _
    ByVal Key As String, ByVal Value As UInt32)
    ' Writes an integer to your INI file
    WriteString(Section, Key, CStr(Value))
    Flush()
    End Sub

    Public Sub WriteShort(ByVal Section As String, _
    ByVal Key As String, ByVal Value As Short)
    ' Writes an integer to your INI file
    WriteString(Section, Key, CStr(Value))
    Flush()
    End Sub

    Public Sub WriteBoolean(ByVal Section As String, _
    ByVal Key As String, ByVal Value As Boolean)
    ' Writes a boolean to your INI file
    WriteString(Section, Key, CStr(CInt(Value)))
    Flush()
    End Sub
    #End Region

    Private Sub Flush()
    ' Stores all the cached changes to your INI file
    FlushPrivateProfileString(0, 0, 0, strFilename)
    End Sub

    End Class[/code]


    Now for my settings code.

    First, we have the Setting class
    [code=vb] Public Class Setting
    Private _Control As Control
    Private _Name As String

    Public ReadOnly Property Control() As Control
    Get
    Return _Control
    End Get
    End Property

    Public ReadOnly Property Name() As String
    Get
    Return _Name
    End Get
    End Property

    Public Property Value() As String
    Get
    Select Case Control.GetType().Name
    Case "ComboBox"
    Return CStr(CType(Control, ComboBox).Text)
    Case "CheckBox"
    Return CStr(CType(Control, CheckBox).Checked)
    Case "RaidoButton"
    Return CStr(CType(Control, RadioButton).Checked)
    Case "TextBox"
    Return CStr(CType(Control, TextBox).Text)
    Case "NumericUpDown"
    Return CStr(CType(Control, NumericUpDown).Value)
    Case "TrackBar"
    Return CStr(CType(Control, TrackBar).Value)
    Case Else
    Return ""
    End Select
    End Get
    Set(ByVal value As String)
    If value = "" Then Exit Property
    Select Case Control.GetType().Name
    Case "ComboBox"
    CType(Control, ComboBox).Text = CStr(value)
    Control.Invalidate()
    Case "CheckBox"
    CType(Control, CheckBox).Checked = CBool(value)
    Case "RaidoButton"
    CType(Control, RadioButton).Checked = CBool(value)
    Case "TextBox"
    CType(Control, TextBox).Text = CStr(value)
    Case "NumericUpDown"
    CType(Control, NumericUpDown).Value = CStr(value)
    Case "TrackBar"
    CType(Control, TrackBar).Value = CStr(value)
    End Select
    End Set
    End Property

    Public Sub New(ByVal C As Control, ByVal name As String)
    _Control = C
    _Name = name
    End Sub
    End Class[/code]

    Now, we will declare a list of Settings

    [code=vb]Private Settings As New List(Of Setting)[/code]

    And we will add settings like so
    [code=vb]Settings.Add(New Setting(Form.Control, "SettingNameInTheIniFile"))[/code]

    And heres saving and loading

    [code=vb]
    Public Sub SaveSettings()
    If Not IO.File.Exists(Application.StartupPath & "\Config.ini") Then MsgBox("Cannot find Config.ini!", MsgBoxStyle.Critical, "Error!") : Exit Sub
    Dim Ini As New iniFile(Application.StartupPath & "\Config.ini")

    For Each [Set] As Setting In Settings
    Ini.WriteString("Settings", [Set].Name, [Set].Value)
    Next
    End Sub

    Public Sub ClearSettings()
    If Not IO.File.Exists(Application.StartupPath & "\Config.ini") Then MsgBox("Cannot find Config.ini!", MsgBoxStyle.Critical, "Error!") : Exit Sub
    Dim Ini As New iniFile(Application.StartupPath & "\Config.ini")

    For Each [Set] As Setting In Settings
    Ini.WriteString("Settings", [Set].Name, "")
    Next
    End Sub

    Public Sub LoadSettings)
    If Not IO.File.Exists(Application.StartupPath & "\Config.ini") Then MsgBox("Cannot find Config.ini!", MsgBoxStyle.Critical, "Error!") : Exit Sub
    Dim Ini As New iniFile(Application.StartupPath & "\Config.ini")

    For Each [Set] As Setting In Settings
    [Set].Value = Ini.GetString("Settings", [Set].Name, "")
    Next
    End Sub
    [/code]

    Its as simple as that! as you can see, the only controls you can use as of now are:
    -ComboBox
    -CheckBox
    -RaidoButton
    -TextBox
    -NumericUpDown
    -TrackBar

    But its not hard to edit the code to do more.
    Goodluck!

  2. #2
    Senior Member
    Join Date
    Mar 2007
    Posts
    324

    RE: Settings Class

    Thanks, always usefull. I personally prefer this more then registry edits. Don't like it if every program writes in my registry!=(

  3. #3

    RE: Settings Class

    Here is my settings: vb 6.0 mooode


    My Class:
    PHP Code:
    Public Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As StringByVal lpKeyName As AnyByVal lpString As AnyByVal lpFileName As String) As Long


    Public Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As StringByVal lpKeyName As AnyByVal lpDefault As StringByVal lpReturnedString As StringByVal nSize As LongByVal lpFileName As String) As Long


    Function GetINI(strMain As StringstrSub As String) As String

        Dim strBuffer 
    As String
        Dim lngLen 
    As Long
        Dim lngRet 
    As Long
        strBuffer 
    Space(100)
        
    lngLen Len(strBuffer)
        
    lngRet GetPrivateProfileString(strMainstrSubvbNullStringstrBufferlngLenApp.Path "\Settings.ini")
        
    GetINI Left(strBufferlngRet)
    End Function



    Sub SetINI(strMain As StringstrSub As StringstrValue As String)

        
    WritePrivateProfileString strMainstrSubstrValueApp.Path "\Settings.ini"
    End Sub 
    How it works:
    Set:
    PHP Code:
    SetINI "global""test"form.control.typeofvalue 
    Get:
    PHP Code:
    form.control.typeofvalue GetINI("global""test"
    Ini look like this:
    Code:
    [global]
    test=0

  4. #4
    Senior Member
    Join Date
    Oct 2007
    Posts
    216

    RE: Settings Class

    Thanks DarkStar, if you have no problems ill use that on my bot
    easy and effective.

    Thanks for share it

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •