VB.Net按钮不使用新信息更新文本

VB.Net按钮不使用新信息更新文本,vb.net,Vb.net,我目前正在创建一个库存管理系统。我输入了多个pc规格,这些规格保存在一个文本文件中。从那里,他们应该显示在屏幕上,在那里我可以向前或向后移动按钮,这些按钮将根据正在查看的计算机更新显示的文本 我的问题在“前进”按钮中,当我第一次添加项目时,我可以单击“前进”,文本将更新,但是如果我向列表中添加任何其他项目,该按钮将不起任何作用。通过调试器工具,我发现这是因为每次单击按钮都会打开流读取器,然后关闭,导致没有显示新信息。这方面可能的解决方法是什么 我还发布了下面的文本文件内容,其中我只看到Dell

我目前正在创建一个库存管理系统。我输入了多个pc规格,这些规格保存在一个文本文件中。从那里,他们应该显示在屏幕上,在那里我可以向前或向后移动按钮,这些按钮将根据正在查看的计算机更新显示的文本

我的问题在“前进”按钮中,当我第一次添加项目时,我可以单击“前进”,文本将更新,但是如果我向列表中添加任何其他项目,该按钮将不起任何作用。通过调试器工具,我发现这是因为每次单击按钮都会打开流读取器,然后关闭,导致没有显示新信息。这方面可能的解决方法是什么

我还发布了下面的文本文件内容,其中我只看到Dell pc规格,无法遍历以查看“测试”规格

下面是一个屏幕截图,可以帮助您提供更好的想法

frmInventory.vb

Private Sub btnForward_Click(sender As Object, e As EventArgs) Handles btnForward.Click
    Dim sr = System.IO.File.OpenText("inventory.txt")

    Dim strInventory = sr.ReadLine()
    txtManufacturer.Text = strInventory
    strInventory = sr.ReadLine()
    txtProcessor.Text = strInventory
    strInventory = sr.ReadLine()
    txtVideo.Text = strInventory
    strInventory = sr.ReadLine()
    txtForm.Text = strInventory
    strInventory = sr.ReadLine()
    txtRam.Text = strInventory
    strInventory = sr.ReadLine()
    txtVram.Text = strInventory
    strInventory = sr.ReadLine()
    txtHd.Text = strInventory
    strInventory = sr.ReadLine()
    chkWireless.CheckState = strInventory

    sr.Close()

End Sub
inventory.txt

Dell
i5
Nvidia
Desktop
8
2
600
1

Test
Test
Test
Test
Test
Test
Test
0

将StreamReader移出到类/表单级别,如下所示(绝对没有错误检查!):


但是…您的整个方法非常有限,因为您只能在文件中前进;你不能倒退。考虑创建一个类来保存该信息并将其实例存储在SomeClass的列表中。另一种选择可能是使用数据表。

这应该可以。我已经对它进行了测试,并做了一些轻微的改进。此外,您需要更改inventory.txt文件,使其开头有一个空行。我在以下代码后发布了修订后的文件:

    'This should be public and set to 1 at initial startup or you can save the value
    '  in a separate file when you close the program and read it when you open it up.
    Dim _lngLineLocation As Long = 1

    Public Sub MoveForward()
        Dim lngCurLine As Long

        'The using statement helps to keep your memory free.
        Using sr As New System.IO.StreamReader("inventory.txt")

            lngCurLine = 1
            Do Until (lngCurLine > _lngLineLocation) Or sr.EndOfStream
                sr.ReadLine()
                lngCurLine += 1
            Loop

            If Not sr.EndOfStream Then
                Dim strInventory = sr.ReadLine()
                txtManufacturer.Text = strInventory
                strInventory = sr.ReadLine()
                txtProcessor.Text = strInventory
                strInventory = sr.ReadLine()
                txtVideo.Text = strInventory
                strInventory = sr.ReadLine()
                txtForm.Text = strInventory
                strInventory = sr.ReadLine()
                txtRam.Text = strInventory
                strInventory = sr.ReadLine()
                txtVram.Text = strInventory
                strInventory = sr.ReadLine()
                txtHd.Text = strInventory
                strInventory = sr.ReadLine()
                chkWireless.CheckState = strInventory

                lngCurLine += 8
            End If

            sr.Close()
        End Using

        _lngLineLocation = lngCurLine

    End Sub

    Public Sub MoveBackward()
        Dim lngCurLine As Long

        'The using statement helps to keep your memory free.
        Using sr As New System.IO.StreamReader("inventory.txt")

            _lngLineLocation -= ((8 + 1) * 2)
            _lngLineLocation = Math.Max(_lngLineLocation, 1)
            lngCurLine = 1
            Do Until (lngCurLine > _lngLineLocation) Or sr.EndOfStream
                sr.ReadLine()
                lngCurLine += 1
            Loop


            Dim strInventory = sr.ReadLine()
            txtManufacturer.Text = strInventory
            strInventory = sr.ReadLine()
            txtProcessor.Text = strInventory
            strInventory = sr.ReadLine()
            txtVideo.Text = strInventory
            strInventory = sr.ReadLine()
            txtForm.Text = strInventory
            strInventory = sr.ReadLine()
            txtRam.Text = strInventory
            strInventory = sr.ReadLine()
            txtVram.Text = strInventory
            strInventory = sr.ReadLine()
            txtHd.Text = strInventory
            strInventory = sr.ReadLine()
            chkWireless.CheckState = strInventory

            lngCurLine += 8
            sr.Close()

        End Using

        _lngLineLocation = lngCurLine

    End Sub
最后,这里是文本文件修改(注意添加的第一个空行)


已经问过了。你总是读同样的8行。UI中不会有任何更改(前提是数据已正确保存)。这不是管理应用程序存储需求的方法。如果您想继续使用文本文件,请使用XML/JSON。是的,我知道它读取的是相同的行,我如何跳过8行到下一台可用的pc?我尝试使用
strInventory=sr.ReadLine()
8次,虽然效率不高,但也不起作用。您必须加载
列表(字符串)
中的所有文本行,然后使用列表按组成记录的行数向前/向后移动。头痛容易,但可行。添加新产品时,您必须将元素添加到列表中,然后在保存到光盘时刷新列表的全部内容。几天后,您会觉得需要一个专门的、可序列化的类来保存元素值,以便更加灵活。然后你会意识到你永远都不够灵活…看看你之前的问题-我在那里添加了一个答案,应该也涵盖了这个问题。是的,向后移动是我至少能够向前移动后的下一个目标。我需要将所有的值存储在文本文件中。@德文,在这种情况下,我强烈考虑使用标准的可串行化文本格式,如XML或JSON……比创建自己的任意格式更容易使用。您将再次解决这些格式已经处理过的所有相同问题。
    'This should be public and set to 1 at initial startup or you can save the value
    '  in a separate file when you close the program and read it when you open it up.
    Dim _lngLineLocation As Long = 1

    Public Sub MoveForward()
        Dim lngCurLine As Long

        'The using statement helps to keep your memory free.
        Using sr As New System.IO.StreamReader("inventory.txt")

            lngCurLine = 1
            Do Until (lngCurLine > _lngLineLocation) Or sr.EndOfStream
                sr.ReadLine()
                lngCurLine += 1
            Loop

            If Not sr.EndOfStream Then
                Dim strInventory = sr.ReadLine()
                txtManufacturer.Text = strInventory
                strInventory = sr.ReadLine()
                txtProcessor.Text = strInventory
                strInventory = sr.ReadLine()
                txtVideo.Text = strInventory
                strInventory = sr.ReadLine()
                txtForm.Text = strInventory
                strInventory = sr.ReadLine()
                txtRam.Text = strInventory
                strInventory = sr.ReadLine()
                txtVram.Text = strInventory
                strInventory = sr.ReadLine()
                txtHd.Text = strInventory
                strInventory = sr.ReadLine()
                chkWireless.CheckState = strInventory

                lngCurLine += 8
            End If

            sr.Close()
        End Using

        _lngLineLocation = lngCurLine

    End Sub

    Public Sub MoveBackward()
        Dim lngCurLine As Long

        'The using statement helps to keep your memory free.
        Using sr As New System.IO.StreamReader("inventory.txt")

            _lngLineLocation -= ((8 + 1) * 2)
            _lngLineLocation = Math.Max(_lngLineLocation, 1)
            lngCurLine = 1
            Do Until (lngCurLine > _lngLineLocation) Or sr.EndOfStream
                sr.ReadLine()
                lngCurLine += 1
            Loop


            Dim strInventory = sr.ReadLine()
            txtManufacturer.Text = strInventory
            strInventory = sr.ReadLine()
            txtProcessor.Text = strInventory
            strInventory = sr.ReadLine()
            txtVideo.Text = strInventory
            strInventory = sr.ReadLine()
            txtForm.Text = strInventory
            strInventory = sr.ReadLine()
            txtRam.Text = strInventory
            strInventory = sr.ReadLine()
            txtVram.Text = strInventory
            strInventory = sr.ReadLine()
            txtHd.Text = strInventory
            strInventory = sr.ReadLine()
            chkWireless.CheckState = strInventory

            lngCurLine += 8
            sr.Close()

        End Using

        _lngLineLocation = lngCurLine

    End Sub

Dell
i5
Nvidia
Desktop
8
2
600
1

TestComp
TestProc
TestGraph
Test1
Test2
Test3
Test4
0

Dell2
i52
Nvidia2
Desktop2
82
22
6002
12

TestComp2
TestProc2
TestGraph2
Test12
Test22
Test32
Test42
02