[英]Load sections of ini file to ComboBox in Visual Basic
Hope someone can help. 希望有人能帮忙。 I want to load the Sections of an external ini file into a ComboBox in Visual Basic 2015 and then load the Keys and Values of a selected Section into TextBoxes. 我想将外部ini文件的Sections加载到Visual Basic 2015中的ComboBox中,然后将所选Section的键和值加载到TextBoxes中。 I've been pulling my hair out with this and can't even get a start on how to accomplish this. 我一直在用这种方法拔头发,甚至还没有开始如何完成该方法的开始。
Any helping start you can give would be greatly appreciated. 您可以提供的任何帮助开始将不胜感激。
Thanks, Dan 谢谢,丹
First create this class(Source - CodeProject Article ): 首先创建此类(Source- CodeProject Article ):
Imports System.Text
Imports System.Runtime.InteropServices
Public Class INI
<DllImport("kernel32")> _
Private Shared Function GetPrivateProfileString(Section As String, Key As String, Value As String, Result As StringBuilder, Size As Integer, FileName As String) As Integer
End Function
<DllImport("kernel32")> _
Private Shared Function GetPrivateProfileString(Section As String, Key As Integer, Value As String, <MarshalAs(UnmanagedType.LPArray)> Result As Byte(), Size As Integer, FileName As String) As Integer
End Function
<DllImport("kernel32")> _
Private Shared Function GetPrivateProfileString(Section As Integer, Key As String, Value As String, <MarshalAs(UnmanagedType.LPArray)> Result As Byte(), Size As Integer, FileName As String) As Integer
End Function
Public path As String
Public Sub New(INIPath As String)
path = INIPath
End Sub
Public Function GetSectionNames() As String()
Dim maxsize As Integer = 500
While True
Dim bytes As Byte() = New Byte(maxsize - 1) {}
Dim size As Integer = GetPrivateProfileString(0, "", "", bytes, maxsize, path)
If size < maxsize - 2 Then
Dim Selected As String = Encoding.ASCII.GetString(bytes, 0, size - (If(size > 0, 1, 0)))
Return Selected.Split(New Char() {ControlChars.NullChar})
End If
maxsize *= 2
End While
End Function
Public Function GetEntryNames(section As String) As String()
Dim maxsize As Integer = 500
While True
Dim bytes As Byte() = New Byte(maxsize - 1) {}
Dim size As Integer = GetPrivateProfileString(section, 0, "", bytes, maxsize, path)
If size < maxsize - 2 Then
Dim entries As String = Encoding.ASCII.GetString(bytes, 0, size - (If(size > 0, 1, 0)))
Return entries.Split(New Char() {ControlChars.NullChar})
End If
maxsize *= 2
End While
End Function
Public Function GetEntryValue(section As String, entry As String) As Object
Dim maxsize As Integer = 250
While True
Dim result As New StringBuilder(maxsize)
Dim size As Integer = GetPrivateProfileString(section, entry, "", result, maxsize, path)
If size < maxsize - 1 Then
Return result.ToString()
End If
maxsize *= 2
End While
End Function
End Class
Add sections to the ComboBox like this: 将部分添加到ComboBox中,如下所示:
Dim _ini As New INI("somefile.ini")
ComboBox1.Items.AddRange(ini.GetSectionNames()) 'For all sections
ComboBox1.Items.Add("section1") 'For specific section(s)
Then, in the selection changed event of the ComboBox: 然后,在ComboBox的选择更改事件中:
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
Dim _ini As New INI("somefile.ini")
Dim section As String = ComboBox1.SelectedItem
TextBox1.Text = _ini.GetEntryValue(section, "someKey") 'for specific entry
For Each item In _ini.GetEntryNames(section) 'this is for all entries
'do whatever you want here with the item variable like this:
'TextBox1.Text = _ini.GetEntryValue(section, item)...
Next
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.