我正在编写代码,以标记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
但是我收到一个类似的错误:
'从类型'DataRowView'到类型'String'的转换无效。'
CheckedListBox
当该文件中存在相应条目时,我想标记项目。
有人可以帮我解决这个问题吗?
如果我正确理解,您想使用文本文件来更改CheckedListBox的状态。
我创建了一个虚拟解决方案,Form1
其中包含一个Windows Form()CheckedListBox1
和一个Button(Button1
),其中Windows Form()包含CheckedListBox(),Button()根据"DB_NAME.txt"
文本文件中的内容更新CheckedListBox中的项目。
表格1
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
最后,在运行时,单击“更新”按钮后,您将获得:
如果仍然遇到问题,建议将问题隔离在虚拟解决方案中,并使用断点进行调试,以了解正在使用的变量类型到底是什么。
我相信您仍然会遇到异常,因为您正在调用对象中不存在的方法和属性。这可能是因为您假设此对象属于给定类型,而并非如此。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句