使用文本文件记住 CheckedListBox 项目状态
Remember CheckedListBox items state using a text file
我正在编写代码来标记 CheckedListBox
的复选框,当我的文本文件中的字符串与我的 CheckedListBox
.
中的项目匹配时
Dim fl As String = s.ToString() + "\Script\" + "DB_NAME.txt"
If File.Exists(fl) Then
If File.Exists(fl) Then
Dim line As String
Dim i As Integer
Using reader As StreamReader = New StreamReader(s.ToString() + "\Script\" + "DB_NAME.txt")
Do Until reader.Peek = -1
line = reader.ReadLine
'For Each Item As DataRowView In grd_tabledata.Items
' Dim text As String = Item(0).ToString()
' If (text = line) Then
' grd_tabledata.SetItemChecked(text, True)
' End If
' MsgBox(text)
'Next
Do While (i <= grd_tabledata.Items.Count)
If (CType(grd_tabledata.Items(i), String) = line) Then
grd_tabledata.SetItemChecked(i, True)
End If
'i = (i + 1)
Loop
Loop
End Using
End
End
但我收到如下错误:
'Conversion from type 'DataRowView' to type 'String' is not valid.'
当该文件中存在相应条目时,我想标记 CheckedListBox
项。
有人可以帮我解决这个问题吗?
如果我没理解错的话,您想使用文本文件记住 CheckedListBox 的状态。
我创建了一个虚拟解决方案,其中包含一个 windows 表单 (Form1
),其中包含一个 CheckedListBox (CheckedListBox1
) 和一个用于更新项目的按钮 (Button1
)根据 "DB_NAME.txt"
文本文件中的内容在 CheckedListBox 中。
Form1
Form1.vb
Imports System.IO
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles UpdateButton.Click
Dim filename As String = "DB_NAME.txt"
'Dim filename As String = s.ToString() + "\Script\" + "DB_NAME.txt"
If Not File.Exists(filename) Then Return
Using reader As StreamReader = New StreamReader(filename)
Do Until reader.Peek = -1
Dim line As String = reader.ReadLine
For i As Integer = 0 To CheckedListBox1.Items.Count - 1
If (CheckedListBox1.Items(i).ToString = line) Then CheckedListBox1.SetItemChecked(i, True)
Next
Loop
End Using
End Sub
End Class
文本文件"DB_NAME.txt"
的内容是:
item C
item D
最后,在 运行 时,点击 'Update' 按钮后,您将获得:
如果您仍然遇到麻烦,我建议您在虚拟解决方案中隔离您的问题并使用断点进行调试以了解您正在使用的变量类型到底是什么。
我相信您仍然遇到异常,因为您正在调用对象中不存在的方法和属性。这可能是因为您假设此对象属于给定类型,而实际上它不是。
在您的代码中,只需将 Do until 循环替换为以下代码
For i=0 To grd_tabledata.Items.count -1
If (CType(grd_tabledata.Items(i), String) = line) Then
grd_tabledata.SetItemChecked(i, True)
Exit For
End If
Next
我正在编写代码来标记 CheckedListBox
的复选框,当我的文本文件中的字符串与我的 CheckedListBox
.
Dim fl As String = s.ToString() + "\Script\" + "DB_NAME.txt"
If File.Exists(fl) Then
If File.Exists(fl) Then
Dim line As String
Dim i As Integer
Using reader As StreamReader = New StreamReader(s.ToString() + "\Script\" + "DB_NAME.txt")
Do Until reader.Peek = -1
line = reader.ReadLine
'For Each Item As DataRowView In grd_tabledata.Items
' Dim text As String = Item(0).ToString()
' If (text = line) Then
' grd_tabledata.SetItemChecked(text, True)
' End If
' MsgBox(text)
'Next
Do While (i <= grd_tabledata.Items.Count)
If (CType(grd_tabledata.Items(i), String) = line) Then
grd_tabledata.SetItemChecked(i, True)
End If
'i = (i + 1)
Loop
Loop
End Using
End
End
但我收到如下错误:
'Conversion from type 'DataRowView' to type 'String' is not valid.'
当该文件中存在相应条目时,我想标记 CheckedListBox
项。
有人可以帮我解决这个问题吗?
如果我没理解错的话,您想使用文本文件记住 CheckedListBox 的状态。
我创建了一个虚拟解决方案,其中包含一个 windows 表单 (Form1
),其中包含一个 CheckedListBox (CheckedListBox1
) 和一个用于更新项目的按钮 (Button1
)根据 "DB_NAME.txt"
文本文件中的内容在 CheckedListBox 中。
Form1
Form1.vb
Imports System.IO
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles UpdateButton.Click
Dim filename As String = "DB_NAME.txt"
'Dim filename As String = s.ToString() + "\Script\" + "DB_NAME.txt"
If Not File.Exists(filename) Then Return
Using reader As StreamReader = New StreamReader(filename)
Do Until reader.Peek = -1
Dim line As String = reader.ReadLine
For i As Integer = 0 To CheckedListBox1.Items.Count - 1
If (CheckedListBox1.Items(i).ToString = line) Then CheckedListBox1.SetItemChecked(i, True)
Next
Loop
End Using
End Sub
End Class
文本文件"DB_NAME.txt"
的内容是:
item C
item D
最后,在 运行 时,点击 'Update' 按钮后,您将获得:
如果您仍然遇到麻烦,我建议您在虚拟解决方案中隔离您的问题并使用断点进行调试以了解您正在使用的变量类型到底是什么。
我相信您仍然遇到异常,因为您正在调用对象中不存在的方法和属性。这可能是因为您假设此对象属于给定类型,而实际上它不是。
在您的代码中,只需将 Do until 循环替换为以下代码
For i=0 To grd_tabledata.Items.count -1
If (CType(grd_tabledata.Items(i), String) = line) Then
grd_tabledata.SetItemChecked(i, True)
Exit For
End If
Next