【问题标题】:Remember CheckedListBox items state using a text file使用文本文件记住 CheckedListBox 项的状态
【发布时间】:2018-04-27 08:16:20
【问题描述】:

我正在编写代码来标记 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 项。

有人可以帮我解决这个问题吗?

【问题讨论】:

  • 您到底想达到什么目标?当文件中存在该条目时,标记相应的复选框列表项?
  • 我正在文本文件中保存复选框列表的某些字段,并希望读取此文本文件并根据文本文件字符串检查复选框列表项。我的复选框列表已从数据库绑定。
  • 当文件中存在条目时标记相应的复选框列表项是的,我想实现这一点。
  • 您确定 grd_tabledata 是您的 CheckedListBox 控件的名称,而不是该控件的 DataSource?

标签: .net vb.net datagridview vb.net-2010 checkedlistbox


【解决方案1】:

如果我理解正确,您想使用文本文件记住 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

最后,在运行时,点击“更新”按钮后,您会得到:

如果您仍然遇到麻烦,我建议您在虚拟解决方案中隔离您的问题并使用断点进行调试,以了解您正在使用的变量类型到底是什么。

我相信您仍然会遇到异常,因为您正在调用对象中不存在的方法和属性。这可能是因为您假设该对象属于给定类型,但实际上并非如此。

【讨论】:

  • 我收到错误,例如“类 'System.Windows.Forms.CheckedListBox' 无法被索引,因为它没有默认属性。” grd_tabledata 是我的复选框列表名称。
  • 能否请您详细提供代码?因为我不明白该怎么做。
  • @NimishaPrajapati:我更新了答案以反映您的意见。
  • 不工作。在 grd_tabledata.Items(i).ToString 中得到了 'system.data.datarowview' 所以checkedlistbox 项目没有被检查
  • @NimishaPrajapati:您能否在您的问题中添加声明 grd_tabledata 变量的代码?我相信你误以为它是 CheckedListBox 类型的,但事实并非如此。可能您的 CheckedListBox 有另一个名称,例如CheckedListBox1.
【解决方案2】:

在您的代码中,只需将您的 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

【讨论】:

    猜你喜欢
    • 2014-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多