从组合框填充列表视图

发布于 2024-10-02 05:46:35 字数 3669 浏览 4 评论 0原文

我有一个程序,我试图选择一个组合框条目并填充到列表视图中。但是,我只能显示其中一个组合框条目(这是最后一个)。我在存储文本文件中的每个单独记录时遇到问题。有人可以告诉我一些如何做到这一点的想法吗?

Imports System.IO
Public Class Form1
    Private BooksStreamReader As StreamReader
    Dim str(51) As String
    Structure Book
        Dim ISBN As String
        Dim Title1 As String
        Dim Author As String
        Dim Publisher As String
        Dim Subject_Code As String
        Dim Shelf_Location As String
        Dim Fiction As Boolean
        Dim Price As Decimal
        Dim QDH As Integer

    End Structure
    Dim ISBN As String
    Dim Title1 As String
    Dim Author As String
    Dim Publisher As String
    Dim Subject_Code As String
    Dim Shelf_Location As String
    Dim Fiction As Boolean
    Dim Price As Decimal
    Dim QDH As Integer
    Dim BooksArray(50) As Book
    Dim BookUBound As Integer
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim I As Integer = 0
        Try
            BooksStreamReader = New StreamReader("C:\Users\Public\Pictures\Books2.Txt")

        Catch ex As Exception
            MessageBox.Show("File Not Found")

        End Try


        'Display combobox ISBNS
        Do While BooksStreamReader.Peek <> -1

            ISBN = BooksStreamReader.ReadLine()
            Title1 = BooksStreamReader.ReadLine()
            Author = BooksStreamReader.ReadLine()
            Publisher = BooksStreamReader.ReadLine()
            Subject_Code = BooksStreamReader.ReadLine()
            Shelf_Location = BooksStreamReader.ReadLine()
            Fiction = BooksStreamReader.ReadLine()
            Price = BooksStreamReader.ReadLine()
            QDH = BooksStreamReader.ReadLine()
            ComboBox1.Items.Add(ISBN)

        Loop
        BookUBound = BooksArray.Length - 1


    End Sub
    Private Sub DisplayRecord()


    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        Form1_Load(sender, e)
        Dim str(200) As String
        Dim selectedindex As Integer = 0

            str(0) = ISBN
            str(1) = Title1
            str(2) = Author
            str(3) = Fiction
            str(4) = Price
            str(5) = QDH
            str(6) = QDH * Price



        If CheckBox1.Checked Then

            Dim itm As New ListViewItem(str)
            ListView1.Items.Add(itm)
        End If


    End Sub
End Class

这是文本文件:

0-671-66398-4
Seven Habits of Highly Effective People
Covey, Stepehn R.
Fireside
BSN
RC-1111
False
 6.99 
 14 
0-934136-27-0
Wine Makers Guide
Nury/Fugelsang
Western Tanager
MST
RC-1112
False
 6.99 
 0 
0-451-16095-9
The Stand
King, Stephen
Signet
MST
RC-1113
True
 7.99 
 0 
0-446-51652-X
Bridges of Madison County
Waller, Robert James
Warner Books
BSS
RC-1114
True
 10.99 
 15 
0-89997-2063
Afoot and Afield in Orange County
Schad, Jerry
Wilderness Press
SLH
RC-1112
False
 11.99 
 7 
0-8041-0753-X
The Kitchen God's Wife
Tan, Amy
Ivy Books
BSS
RC-1114
True
 11.99 
 7 
0-8487-0504-1
Erica Wilson's Quilts of America
Wilson, Erica
Oxmoor House
ART
RC-1112
False
 12.99 
 7 
8423-2218-3
The Way
Null
Tyndale
RLG
RC-1111
False
 12.99 
 0 
0-446-60274-4
The Rules
Fein/Schneider
Warner Books
SLH
RC-1111
False
 14.99 
 1 
0-87826-5299
Know Your Lhaso Apso
Schneider, Earl
The Pet Library LTD
SLH
RC-1112
False
 15.99 
 15 
0-697-12897-0
QuickBasic and QBasic Using Modular Structure
Bradley, Julia Case
B & E Tech
BSN
RC-1111
False
 16.99 
 13 
1-55615-484-4
Code Complete
Mc Connell, Steve
Microsoft Press
BSN
RC-1111
False
 16.99 
 2 

I have a program where I am trying to select a combobox entry and populate into the listview. However I can only get one of the combo box entries to show(which is the last). I am having a problem storing each individual record from a text file. Can someone show me someideas on how to do this?

Imports System.IO
Public Class Form1
    Private BooksStreamReader As StreamReader
    Dim str(51) As String
    Structure Book
        Dim ISBN As String
        Dim Title1 As String
        Dim Author As String
        Dim Publisher As String
        Dim Subject_Code As String
        Dim Shelf_Location As String
        Dim Fiction As Boolean
        Dim Price As Decimal
        Dim QDH As Integer

    End Structure
    Dim ISBN As String
    Dim Title1 As String
    Dim Author As String
    Dim Publisher As String
    Dim Subject_Code As String
    Dim Shelf_Location As String
    Dim Fiction As Boolean
    Dim Price As Decimal
    Dim QDH As Integer
    Dim BooksArray(50) As Book
    Dim BookUBound As Integer
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim I As Integer = 0
        Try
            BooksStreamReader = New StreamReader("C:\Users\Public\Pictures\Books2.Txt")

        Catch ex As Exception
            MessageBox.Show("File Not Found")

        End Try


        'Display combobox ISBNS
        Do While BooksStreamReader.Peek <> -1

            ISBN = BooksStreamReader.ReadLine()
            Title1 = BooksStreamReader.ReadLine()
            Author = BooksStreamReader.ReadLine()
            Publisher = BooksStreamReader.ReadLine()
            Subject_Code = BooksStreamReader.ReadLine()
            Shelf_Location = BooksStreamReader.ReadLine()
            Fiction = BooksStreamReader.ReadLine()
            Price = BooksStreamReader.ReadLine()
            QDH = BooksStreamReader.ReadLine()
            ComboBox1.Items.Add(ISBN)

        Loop
        BookUBound = BooksArray.Length - 1


    End Sub
    Private Sub DisplayRecord()


    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        Form1_Load(sender, e)
        Dim str(200) As String
        Dim selectedindex As Integer = 0

            str(0) = ISBN
            str(1) = Title1
            str(2) = Author
            str(3) = Fiction
            str(4) = Price
            str(5) = QDH
            str(6) = QDH * Price



        If CheckBox1.Checked Then

            Dim itm As New ListViewItem(str)
            ListView1.Items.Add(itm)
        End If


    End Sub
End Class

Here is the text file:

0-671-66398-4
Seven Habits of Highly Effective People
Covey, Stepehn R.
Fireside
BSN
RC-1111
False
 6.99 
 14 
0-934136-27-0
Wine Makers Guide
Nury/Fugelsang
Western Tanager
MST
RC-1112
False
 6.99 
 0 
0-451-16095-9
The Stand
King, Stephen
Signet
MST
RC-1113
True
 7.99 
 0 
0-446-51652-X
Bridges of Madison County
Waller, Robert James
Warner Books
BSS
RC-1114
True
 10.99 
 15 
0-89997-2063
Afoot and Afield in Orange County
Schad, Jerry
Wilderness Press
SLH
RC-1112
False
 11.99 
 7 
0-8041-0753-X
The Kitchen God's Wife
Tan, Amy
Ivy Books
BSS
RC-1114
True
 11.99 
 7 
0-8487-0504-1
Erica Wilson's Quilts of America
Wilson, Erica
Oxmoor House
ART
RC-1112
False
 12.99 
 7 
8423-2218-3
The Way
Null
Tyndale
RLG
RC-1111
False
 12.99 
 0 
0-446-60274-4
The Rules
Fein/Schneider
Warner Books
SLH
RC-1111
False
 14.99 
 1 
0-87826-5299
Know Your Lhaso Apso
Schneider, Earl
The Pet Library LTD
SLH
RC-1112
False
 15.99 
 15 
0-697-12897-0
QuickBasic and QBasic Using Modular Structure
Bradley, Julia Case
B & E Tech
BSN
RC-1111
False
 16.99 
 13 
1-55615-484-4
Code Complete
Mc Connell, Steve
Microsoft Press
BSN
RC-1111
False
 16.99 
 2 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

假装不在乎 2024-10-09 05:46:35

这就像一场火车失事,你真的离得很近。建议修复:

Structure Book
    Dim ISBN As String
    '' etc
    Public Overrides Function ToString() As String
        Return ISBN
    End Function
End Structure

...
Do While BooksStreamReader.Peek <> -1
    Dim book As New Book
    book.ISBN = BooksStreamReader.ReadLine()
    '' etc
    ComboBox1.Items.Add(book)
Loop
...

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
    If CheckBox1.Checked Then
        Dim book As Book = CType(ComboBox1.SelectedItem, Book)
        Dim item As New ListViewItem(book.ISBN)
        item.SubItems.Add(book.Title)
        '' etc...
        ListView1.Items.Add(item)
    End If
End Sub

This is rather a train wreck, you are really not close. Suggested fixes:

Structure Book
    Dim ISBN As String
    '' etc
    Public Overrides Function ToString() As String
        Return ISBN
    End Function
End Structure

...
Do While BooksStreamReader.Peek <> -1
    Dim book As New Book
    book.ISBN = BooksStreamReader.ReadLine()
    '' etc
    ComboBox1.Items.Add(book)
Loop
...

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
    If CheckBox1.Checked Then
        Dim book As Book = CType(ComboBox1.SelectedItem, Book)
        Dim item As New ListViewItem(book.ISBN)
        item.SubItems.Add(book.Title)
        '' etc...
        ListView1.Items.Add(item)
    End If
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文