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

时间:2019-01-23 13:31:26

标签: vb.net

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

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

我还在下面发布了文本文件的内容,我只在其中看到Dell PC规格,而不能遍历以查看“测试”规格

以下是帮助您更好地了解主意的屏幕截图。 https://prnt.sc/mb2bsd

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

2 个答案:

答案 0 :(得分:0)

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

Private sr As System.IO.StreamReader = Nothing

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

    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
    End If
End Sub

但是 ...您的整个方法非常局限,因为您只能在文件中移动FORWARD;你不能倒退。考虑创建一个CLASS来保存该信息,并将其实例存储在List(Of SomeClass)中。另一种选择是使用数据表。

答案 1 :(得分:0)

这应该有效。我已经对其进行了测试,并做了一些细微的改进。另外,您需要将venture.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

最后,这是文本文件的修改(请注意,添加了第一个空白行)


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