如何查看从我的列表

时间:2018-06-10 19:57:17

标签: .net vb.net

大家晚上蠢事!所以今晚我遇到了一些麻烦...我应该做以下事情:1)定义一个名为Product的结构。该结构应包含两个成员变量:一个用于存储项目编号的String变量和一个用于存储价格的Double变量。 2)声明一个包含五个Product结构变量的类级数组。 3)表单的Load事件过程应该读取ItemInfo.txt文件中的项目编号和价格,并将它们存储在类级别数组中。它还应该将项目编号添加到列表框中。编写程序。 4)当用户选择列表框中的项目时,项目的价格应出现在lblPrice控件中。编写适当的程序。 5)保存解决方案,然后启动并测试应用程序。关闭“代码编辑器”窗口,然后关闭解决方案。 到目前为止,我有以下代码:

Public Class frmMain
Dim newLineIndex As Integer
Dim index As Integer
Dim item As String
Dim counter As Integer
Structure product
    Public ItemPrice As Double
    Public ItemNumber As String

End Structure

Dim dollars() As Integer
Dim price As Integer

Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
    Me.Close()
End Sub
Dim products(4) As product

Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles Me.Load
    Dim file As String = "ItemInfo.txt"
    Dim product As String

    If My.Computer.FileSystem.FileExists(file) Then
        product = My.Computer.FileSystem.ReadAllText(file)
        newLineIndex = product.IndexOf(ControlChars.NewLine)

        Do Until newLineIndex = -1
            item = product.Substring(index, newLineIndex - index)
            Me.lstNumbers.Items.Add(item)
            index = newLineIndex + 2
            newLineIndex = product.IndexOf(ControlChars.NewLine, index)

        Loop

    End If
End Sub

Private Sub lstNumbers_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstNumbers.SelectedIndexChanged
End Sub
End Class

所以现在,我的问题是,当我选择一个项目时,我不知道如何让价格出现在lblPrice上。 另一个问题是,有没有办法只列出项目而不是lstNumbers上的价格? 这是我的界面:

enter image description here

这是文件“ItemInfo.txt”:

enter image description here

非常感谢任何帮助!

2 个答案:

答案 0 :(得分:0)

解释符合要求 List(Of T)比数组更方便。不用担心ReDim和keepint跟踪的元素数量。现在,如果这是家庭作业,你必须使用数组,看看你是否可以自己改变它。

    Private lstProductList As New List(Of product)
    Structure product
            'changed the elements to Properties instead of fields becaus
            'the list box cannot bind DisplayMember and ValueMember to fields
            Public Property ItemNumber As String
            Public Property ItemPrice As Double
    End Structure

    Private Sub Project1Data_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim textfile As String
            OpenFileDialog1.ShowDialog() 'Drag a dialog to your form in the designer
            textfile = OpenFileDialog1.FileName
            'instead of reading the entire file into a variable (which you named the same as
            'your structure - bad idea) read a line at a time
            Using sr As New StreamReader(textfile)
                Do While sr.Peek() >= 0
                    Dim p As New product 'an object of type - your structure
                    p.ItemNumber = sr.ReadLine() 'assign a line to the properties of your structure
                    p.ItemPrice = CDbl(sr.ReadLine())
                    lstProductList.Add(p) 'after the properties are set, add the object to a list
                Loop
            End Using
        lstProducts.DataSource = lstProductList 'A list of product
        lstProducts.DisplayMember = "ItemNumber" 'The property of product you want to display
        lstProducts.ValueMember = "ItemPrice" 'The property you want to retrieve when an item is selected.
        lstProducts.SelectedIndex = -1 'Nothing is selected at first
        lblPrice.Text = "0.00"
    End Sub
    Private Sub lstProducts_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstProducts.SelectedIndexChanged
            'The $ indicates and interpolated string where you can insert variables directly in a string
            'The :N2 is the formating for the double converted to a string; N for number 
            'and 2 for the number of decimal places
            If lstProducts.SelectedIndex <> -1 Then
                lblPrice.Text = $"{lstProducts.SelectedValue:N2}"
            End If
    End Sub

答案 1 :(得分:0)

非常感谢您的帮助,并建议您为此练习给我。 我使用的解决方案在下面,并且按照预期的方式运行。 这是我的代码的一部分:

Dim strItemNumber As String
  Dim dblPrice As Double
    Public Overrides Function ToString() As String
        ' return string variable
        Return Me.strItemNumber
    End Function
End Structure
' declare class-level array
Dim products(4) As Product
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    ' read all lines from the file
    Dim lines() As String = IO.File.ReadAllLines("ItemInfo.txt")
    Dim counter As Integer = 0
    ' store price item numbers and price in the class-level array
    For x As Integer = 0 To lines.GetUpperBound(0) Step 2
        products(counter).strItemNumber = lines(x)
        products(counter).dblPrice = CDbl(lines(x + 1))
        ' add item numbers to ListBox
        lstNumbers.Items.Add(products(counter))
        counter += 1
     Next

再次,谢谢大家!