ชุมชนคนรักภาษาเบสิค - Visual Basic Community

 ลืมรหัสผ่าน
 ลงทะเบียน
ค้นหา
ดู: 4462|ตอบกลับ: 4

[VB.NET] การประยุกต์ใช้งานไฟล์ INI (Initialize File) ภาค 2 ขั้น Advanced

[คัดลอกลิงก์]

310

กระทู้

501

โพสต์

6041

เครดิต

ผู้ดูแลระบบ

ทองก้อน ทับทิมกรอบ

Rank: 9Rank: 9Rank: 9

เครดิต
6041



จากตอนที่แล้วเป็นการจัดเก็บข้อมูลแบบ 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 ข่าวสารต่างๆ

มาดูโค้ดทั้งหมด ...
  1. Imports System.IO

  2. Public Class frmSampleINI
  3.     Dim strFileINI As String

  4.     Private Sub frmSampleINI_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
  5.         strFileINI = MyPath(Application.StartupPath) & "Config.ini"
  6.         '// เช็คว่ามีไฟล์ Config.ini อยู่หรือไม่???
  7.         If My.Computer.FileSystem.FileExists(strFileINI) Then
  8.             RadioButton1.Checked = ReadIni(strFileINI, "Config", "Option1", "")
  9.             RadioButton2.Checked = ReadIni(strFileINI, "Config", "Option2", "")
  10.             RadioButton3.Checked = ReadIni(strFileINI, "Config", "Option3", "")
  11.             '//
  12.             CheckBox1.Checked = ReadIni(strFileINI, "Config", "Check1", "")
  13.             CheckBox2.Checked = ReadIni(strFileINI, "Config", "Check2", "")
  14.             CheckBox3.Checked = ReadIni(strFileINI, "Config", "Check3", "")
  15.             '// Section [data]
  16.             Dim sr As New System.IO.StreamReader(strFileINI, System.Text.Encoding.Default)
  17.             Dim sRow As String
  18.             '/ ระบุ Section ที่ต้องการ
  19.             Dim Section As String = "data"
  20.             Dim blnMatch As Boolean = False
  21.             '// ลูปจนกว่าไม่เจอข้อมูลใดๆเลย ลอง Debug ดูก็จะเห็นครับ
  22.             Do While sr.Peek() <> -1
  23.                 '/ อ่านค่าแบบ Line by Line
  24.                 sRow = sr.ReadLine()
  25.                 '/ ต้องเช็คก่อนว่าเป็น Section ที่ต้องการหรือไม่? (ใส่ Lower Case หรือให้เป็นอักษรตัวเล็กทั้งหมด)
  26.                 If LCase(sRow) = "[" & LCase(Section) & "]" Then
  27.                     blnMatch = True

  28.                     '/ เมื่อได้ Section ตามที่ต้องการ (blnMatch = True) ก็ตรวจสอบอีกครั้งว่ามีข้อมูลหรือไม่ และต้องไม่มีเครื่องหมาย Bracket
  29.                 ElseIf blnMatch AndAlso Trim(sRow) <> "" AndAlso Mid(sRow, 1, 1) <> "[" Then
  30.                     '// แยก (Split) Key กับ Value ออกจากกัน
  31.                     Dim data() As String = Split(sRow, "=")
  32.                     Dim key As String = data(0)
  33.                     Dim value As String = data(1)
  34.                     '// Test to find Key and Value
  35.                     'Debug.Print(key & "=" & value)
  36.                     'ListBox1.Items.Add(key & "=" & value)
  37.                     ListBox1.Items.Add(value)
  38.                 Else
  39.                     blnMatch = False
  40.                 End If
  41.             Loop
  42.             sr.Close()

  43.             '// กรณีไม่เจอ ให้เริ่มต้นค่าใหม่
  44.         Else
  45.             RadioButton1.Checked = True
  46.             RadioButton2.Checked = False
  47.             RadioButton3.Checked = False
  48.             '// Clear ค่าทั้งหมดให้ Uncheck
  49.             For Each ctrl As CheckBox In GroupBox2.Controls
  50.                 ctrl.Checked = False
  51.             Next
  52.         End If
  53.     End Sub

  54.     ' / --------------------------------------------------------------------
  55.     '// บันทึกไฟล์ INI
  56.     Private Sub btnSave_Click(sender As System.Object, e As System.EventArgs) Handles btnSave.Click
  57.         WriteIni(strFileINI, "Config", "Option1", GetRadioButton(RadioButton1))
  58.         WriteIni(strFileINI, "Config", "Option2", GetRadioButton(RadioButton2))
  59.         WriteIni(strFileINI, "Config", "Option3", GetRadioButton(RadioButton3))
  60.         '//
  61.         WriteIni(strFileINI, "Config", "Check1", GetCheckBox(CheckBox1))
  62.         WriteIni(strFileINI, "Config", "Check2", GetCheckBox(CheckBox2))
  63.         WriteIni(strFileINI, "Config", "Check3", GetCheckBox(CheckBox3))
  64.         '//
  65.         For iRow As Byte = 0 To ListBox1.Items.Count - 1
  66.             WriteIni(strFileINI, "Data", "RowData" & iRow, ListBox1.Items(iRow).ToString)
  67.         Next
  68.         'WriteIni(strFileINI, "Data", "", strData)
  69.         '//
  70.         MessageBox.Show("บันทึกการตั้งค่าระบบเรียบร้อย.", "รายงานสถานะ", MessageBoxButtons.OK, MessageBoxIcon.Information)
  71.         Me.Close()
  72.     End Sub

  73.     ' / --------------------------------------------------------------------
  74.     ' / เพิ่มค่า (Value) เข้าไปใน ListBox
  75.     Private Sub btnAdd_Click(sender As System.Object, e As System.EventArgs) Handles btnAdd.Click
  76.         If TextBox1.Text = "" Or Len(TextBox1.Text) = 0 Then Return
  77.         ListBox1.Items.Add(TextBox1.Text)
  78.         TextBox1.Clear()
  79.         TextBox1.Focus()
  80.     End Sub

  81.     ' / --------------------------------------------------------------------
  82.     ' / ลบค่า (Value) ออกจาก ListBox
  83.     Private Sub btnRemove_Click(sender As System.Object, e As System.EventArgs) Handles btnRemove.Click
  84.         If ListBox1.SelectedIndex < 0 Then Return
  85.         ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)
  86.         TextBox1.Focus()
  87.     End Sub

  88.     ' / --------------------------------------------------------------------
  89.     ' / ตรวจสอบสถานะของ RadioButton
  90.     Private Function GetRadioButton(rdb As RadioButton) As Boolean
  91.         If rdb.Checked Then
  92.             Return True
  93.         Else
  94.             Return False
  95.         End If
  96.     End Function

  97.     ' / --------------------------------------------------------------------
  98.     ' / ตรวจสอบสถานะของ CheckBox
  99.     Private Function GetCheckBox(chk As CheckBox) As Boolean
  100.         If chk.Checked Then
  101.             Return True
  102.         Else
  103.             Return False
  104.         End If
  105.     End Function

  106.     Private Sub btnExit_Click(sender As System.Object, e As System.EventArgs) Handles btnExit.Click
  107.         Me.Close()
  108.     End Sub

  109.     Private Sub frmSampleINI_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
  110.         Me.Dispose()
  111.         Application.Exit()
  112.     End Sub

  113. End Class
คัดลอกไปที่คลิปบอร์ด

โมดูลฟังค์ชั่นในส่วนของการอ่านและเขียนข้อมูลลง INI
  1. Module modFunction

  2.     ' / --------------------------------------------------------------------
  3.     ' / Initialized Management
  4.     Private Declare Unicode Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringW" ( _
  5.         ByVal lpApplicationName As String, _
  6.         ByVal lpKeyName As String, _
  7.         ByVal lpString As String, _
  8.         ByVal lpFileName As String _
  9.         ) As Int32

  10.     Private Declare Unicode Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringW" ( _
  11.         ByVal lpApplicationName As String, _
  12.         ByVal lpKeyName As String, _
  13.         ByVal lpDefault As String, _
  14.         ByVal lpReturnedString As String, _
  15.         ByVal nSize As Int32, _
  16.         ByVal lpFileName As String _
  17.         ) As Int32
  18.     ' / --------------------------------------------------------------------

  19.     ' / --------------------------------------------------------------------
  20.     Public Function WriteIni(ByVal iniFileName As String, ByVal Section As String, ByVal ParamName As String, ByVal ParamVal As String) As Integer
  21.         WriteIni = WritePrivateProfileString(Section, ParamName, ParamVal, iniFileName)
  22.     End Function

  23.     ' / --------------------------------------------------------------------
  24.     Public Function ReadIni(ByVal IniFileName As String, ByVal Section As String, ByVal ParamName As String, ByVal ParamDefault As String) As String
  25.         Dim ParamVal As String = Space$(1024)
  26.         Dim LenParamVal As Long = GetPrivateProfileString(Section, ParamName, ParamDefault, ParamVal, Len(ParamVal), IniFileName)
  27.         ReadIni = Left$(ParamVal, LenParamVal)
  28.     End Function

  29.     ' / --------------------------------------------------------------------
  30.     ' / Get my project path
  31.     ' / AppPath = C:\My Project\bin\debug
  32.     ' / Replace "\bin\debug" with ""
  33.     ' / Return : C:\My Project\
  34.     Function MyPath(ByVal AppPath As String) As String
  35.         '/ MessageBox.Show(AppPath);
  36.         AppPath = AppPath.ToLower()
  37.         '/ Return Value
  38.         MyPath = AppPath.Replace("\bin\debug", "").Replace("\bin\release", "").Replace("\bin\x86\debug", "")
  39.         '// If not found folder then put the \ (BackSlash) at the end.
  40.         If Right(MyPath, 1) <> "" Then MyPath = MyPath & ""
  41.     End Function
คัดลอกไปที่คลิปบอร์ด

ดาวน์โหลดโค้ดต้นฉบับแบบเต็ม VB.NET (2010) ได้ที่นี่

ขออภัย! โพสต์นี้มีไฟล์แนบหรือรูปภาพที่ไม่ได้รับอนุญาตให้คุณเข้าถึง

คุณจำเป็นต้อง ลงชื่อเข้าใช้ เพื่อดาวน์โหลดหรือดูไฟล์แนบนี้ คุณยังไม่มีบัญชีใช่ไหม? ลงทะเบียน

x
สิ่งที่ดีกว่าการให้ คือการให้แบบไม่มีที่สิ้นสุด

0

กระทู้

2

โพสต์

18

เครดิต

Newbie

Rank: 1

เครดิต
18
โพสต์ 2017-12-15 11:32:26 | ดูโพสต์ทั้งหมด

ขอบคุณครับ นี่เลยที่ลอยคอ เอ๋ย รอคอย

0

กระทู้

51

โพสต์

233

เครดิต

Full Member

Rank: 3Rank: 3

เครดิต
233
โพสต์ 2017-12-15 14:21:06 | ดูโพสต์ทั้งหมด

ขอบคุณ And Thank you ครับ

0

กระทู้

5

โพสต์

132

เครดิต

Member

Rank: 2

เครดิต
132
โพสต์ 2018-10-27 16:48:13 | ดูโพสต์ทั้งหมด

ขอบคุณครับ

0

กระทู้

5

โพสต์

83

เครดิต

Member

Rank: 2

เครดิต
83
โพสต์ 2019-2-2 07:57:56 | ดูโพสต์ทั้งหมด

ขอบคุณมากครับ

0

กระทู้

6

โพสต์

16

เครดิต

Newbie

Rank: 1

เครดิต
16
โพสต์ 2022-3-4 19:26:29 | ดูโพสต์ทั้งหมด

ขอบคุณครับ
ขออภัย! คุณไม่ได้รับสิทธิ์ในการดำเนินการในส่วนนี้ กรุณาเลือกอย่างใดอย่างหนึ่ง ลงชื่อเข้าใช้ | ลงทะเบียน

รายละเอียดเครดิต

ข้อความล้วน|อุปกรณ์พกพา|ประวัติการแบน|G2GNet.com  

GMT+7, 2024-4-23 18:56 , Processed in 0.182588 second(s), 4 queries , File On.

Powered by Discuz! X3.4, Rev.62

Copyright © 2001-2020 Tencent Cloud.

ตอบกระทู้ ขึ้นไปด้านบน ไปที่หน้ารายการกระทู้