|  | 
 
|  
 จากตอนที่แล้วเป็นการจัดเก็บข้อมูลแบบ Text ขนาดเล็ก คราวนี้ก้าวขึ้นมาอีกขั้นด้วยตัวอย่างของการเก็บข้อมูลจาก RadioButton กับ CheckBox Control ซึ่งก็แน่นอนล่ะว่าเป็นชนิดข้อมูลแบบ Boolean แต่เดี๋ยวง่ายไป แอดมินเลยเพิ่มเติมการจัดเก็บข้อมูลแบบ Text ในลักษณะไดนามิคลง Section ซึ่งเป็น Climax ของบทความนี้ทั้งหมด (โฟกัสไปที่ ListBox นั่นแหละครับ) ... อนึ่งขอให้ทำความเข้าใจว่า การใช้ Initilize File หรือ INI มันเป็นการเก็บข้อมูลที่ไม่แน่นอนตายตัวลงในแต่ละ Section ส่วน XML (eXtensible Markup Language) เป็นการเก็บข้อมูลที่มีรูปแบบค่อนข้างแน่นอน ในแต่ละ Element เช่น Title, Link, Description และ Date ซึ่งคล้ายๆกับ DataBase และเราพบเห็นได้ทั่วไปในลักษณะของ RSS Feed ข่าวสารต่างๆ
 
 มาดูโค้ดทั้งหมด ...
 
 คัดลอกไปที่คลิปบอร์ดImports System.IO
Public Class frmSampleINI
    Dim strFileINI As String
    Private Sub frmSampleINI_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        strFileINI = MyPath(Application.StartupPath) & "Config.ini"
        '// เช็คว่ามีไฟล์ Config.ini อยู่หรือไม่???
        If My.Computer.FileSystem.FileExists(strFileINI) Then
            RadioButton1.Checked = ReadIni(strFileINI, "Config", "Option1", "")
            RadioButton2.Checked = ReadIni(strFileINI, "Config", "Option2", "")
            RadioButton3.Checked = ReadIni(strFileINI, "Config", "Option3", "")
            '//
            CheckBox1.Checked = ReadIni(strFileINI, "Config", "Check1", "")
            CheckBox2.Checked = ReadIni(strFileINI, "Config", "Check2", "")
            CheckBox3.Checked = ReadIni(strFileINI, "Config", "Check3", "")
            '// Section [data]
            Dim sr As New System.IO.StreamReader(strFileINI, System.Text.Encoding.Default)
            Dim sRow As String
            '/ ระบุ Section ที่ต้องการ
            Dim Section As String = "data"
            Dim blnMatch As Boolean = False
            '// ลูปจนกว่าไม่เจอข้อมูลใดๆเลย ลอง Debug ดูก็จะเห็นครับ
            Do While sr.Peek() <> -1
                '/ อ่านค่าแบบ Line by Line
                sRow = sr.ReadLine()
                '/ ต้องเช็คก่อนว่าเป็น Section ที่ต้องการหรือไม่? (ใส่ Lower Case หรือให้เป็นอักษรตัวเล็กทั้งหมด)
                If LCase(sRow) = "[" & LCase(Section) & "]" Then
                    blnMatch = True
                    '/ เมื่อได้ Section ตามที่ต้องการ (blnMatch = True) ก็ตรวจสอบอีกครั้งว่ามีข้อมูลหรือไม่ และต้องไม่มีเครื่องหมาย Bracket
                ElseIf blnMatch AndAlso Trim(sRow) <> "" AndAlso Mid(sRow, 1, 1) <> "[" Then
                    '// แยก (Split) Key กับ Value ออกจากกัน
                    Dim data() As String = Split(sRow, "=")
                    Dim key As String = data(0)
                    Dim value As String = data(1)
                    '// Test to find Key and Value
                    'Debug.Print(key & "=" & value)
                    'ListBox1.Items.Add(key & "=" & value)
                    ListBox1.Items.Add(value)
                Else
                    blnMatch = False
                End If
            Loop
            sr.Close()
            '// กรณีไม่เจอ ให้เริ่มต้นค่าใหม่
        Else
            RadioButton1.Checked = True
            RadioButton2.Checked = False
            RadioButton3.Checked = False
            '// Clear ค่าทั้งหมดให้ Uncheck
            For Each ctrl As CheckBox In GroupBox2.Controls
                ctrl.Checked = False
            Next
        End If
    End Sub
    ' / --------------------------------------------------------------------
    '// บันทึกไฟล์ INI
    Private Sub btnSave_Click(sender As System.Object, e As System.EventArgs) Handles btnSave.Click
        WriteIni(strFileINI, "Config", "Option1", GetRadioButton(RadioButton1))
        WriteIni(strFileINI, "Config", "Option2", GetRadioButton(RadioButton2))
        WriteIni(strFileINI, "Config", "Option3", GetRadioButton(RadioButton3))
        '//
        WriteIni(strFileINI, "Config", "Check1", GetCheckBox(CheckBox1))
        WriteIni(strFileINI, "Config", "Check2", GetCheckBox(CheckBox2))
        WriteIni(strFileINI, "Config", "Check3", GetCheckBox(CheckBox3))
        '//
        For iRow As Byte = 0 To ListBox1.Items.Count - 1
            WriteIni(strFileINI, "Data", "RowData" & iRow, ListBox1.Items(iRow).ToString)
        Next
        'WriteIni(strFileINI, "Data", "", strData)
        '//
        MessageBox.Show("บันทึกการตั้งค่าระบบเรียบร้อย.", "รายงานสถานะ", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Me.Close()
    End Sub
    ' / --------------------------------------------------------------------
    ' / เพิ่มค่า (Value) เข้าไปใน ListBox
    Private Sub btnAdd_Click(sender As System.Object, e As System.EventArgs) Handles btnAdd.Click
        If TextBox1.Text = "" Or Len(TextBox1.Text) = 0 Then Return
        ListBox1.Items.Add(TextBox1.Text)
        TextBox1.Clear()
        TextBox1.Focus()
    End Sub
    ' / --------------------------------------------------------------------
    ' / ลบค่า (Value) ออกจาก ListBox
    Private Sub btnRemove_Click(sender As System.Object, e As System.EventArgs) Handles btnRemove.Click
        If ListBox1.SelectedIndex < 0 Then Return
        ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)
        TextBox1.Focus()
    End Sub
    ' / --------------------------------------------------------------------
    ' / ตรวจสอบสถานะของ RadioButton
    Private Function GetRadioButton(rdb As RadioButton) As Boolean
        If rdb.Checked Then
            Return True
        Else
            Return False
        End If
    End Function
    ' / --------------------------------------------------------------------
    ' / ตรวจสอบสถานะของ CheckBox
    Private Function GetCheckBox(chk As CheckBox) As Boolean
        If chk.Checked Then
            Return True
        Else
            Return False
        End If
    End Function
    Private Sub btnExit_Click(sender As System.Object, e As System.EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub
    Private Sub frmSampleINI_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        Me.Dispose()
        Application.Exit()
    End Sub
End Class
โมดูลฟังค์ชั่นในส่วนของการอ่านและเขียนข้อมูลลง INI
 
 คัดลอกไปที่คลิปบอร์ดModule modFunction
    ' / --------------------------------------------------------------------
    ' / Initialized Management
    Private Declare Unicode Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringW" ( _
        ByVal lpApplicationName As String, _
        ByVal lpKeyName As String, _
        ByVal lpString As String, _
        ByVal lpFileName As String _
        ) As Int32
    Private Declare Unicode Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringW" ( _
        ByVal lpApplicationName As String, _
        ByVal lpKeyName As String, _
        ByVal lpDefault As String, _
        ByVal lpReturnedString As String, _
        ByVal nSize As Int32, _
        ByVal lpFileName As String _
        ) As Int32
    ' / --------------------------------------------------------------------
    ' / --------------------------------------------------------------------
    Public Function WriteIni(ByVal iniFileName As String, ByVal Section As String, ByVal ParamName As String, ByVal ParamVal As String) As Integer
        WriteIni = WritePrivateProfileString(Section, ParamName, ParamVal, iniFileName)
    End Function
    ' / --------------------------------------------------------------------
    Public Function ReadIni(ByVal IniFileName As String, ByVal Section As String, ByVal ParamName As String, ByVal ParamDefault As String) As String
        Dim ParamVal As String = Space$(1024)
        Dim LenParamVal As Long = GetPrivateProfileString(Section, ParamName, ParamDefault, ParamVal, Len(ParamVal), IniFileName)
        ReadIni = Left$(ParamVal, LenParamVal)
    End Function
    ' / --------------------------------------------------------------------
    ' / Get my project path
    ' / AppPath = C:\My Project\bin\debug
    ' / Replace "\bin\debug" with ""
    ' / Return : C:\My Project\
    Function MyPath(ByVal AppPath As String) As String
        '/ MessageBox.Show(AppPath);
        AppPath = AppPath.ToLower()
        '/ Return Value
        MyPath = AppPath.Replace("\bin\debug", "").Replace("\bin\release", "").Replace("\bin\x86\debug", "")
        '// If not found folder then put the \ (BackSlash) at the end.
        If Right(MyPath, 1) <> "" Then MyPath = MyPath & ""
    End Function
ดาวน์โหลดโค้ดต้นฉบับแบบเต็ม VB.NET (2010) ได้ที่นี่
 
 | 
 
xขออภัย! โพสต์นี้มีไฟล์แนบหรือรูปภาพที่ไม่ได้รับอนุญาตให้คุณเข้าถึงคุณจำเป็นต้อง ลงชื่อเข้าใช้ เพื่อดาวน์โหลดหรือดูไฟล์แนบนี้ คุณยังไม่มีบัญชีใช่ไหม? ลงทะเบียน  |