|
-
Apr 21st, 2013, 09:45 AM
#1
Thread Starter
Junior Member
Displaying book titles
So I have a form with a menu bar and a listbox. On the menu bar is the Display option with 3 choices; all, fiction, or nonfiction. The textfile has book titles, author, category (fic"F" or nonfic"N"), stock, price, in that order. When the user clicks on "Display" "all", I want all the titles to display in the listbox. If they click on "Nonfic", just the nonfic titles, and same with "fiction". This is my code so far:
Private Sub menuDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menuDisplay.Click
Dim books() As String = IO.File.ReadAllLines("Books.txt")
Dim data() As String
Dim allBooksTitle, allNonFicTitle, allFicTitle As String
For i As Integer = 0 To books.Count - 1
data = books(i).Split(","c)
allBooksTitle = data(2)
If data(2).Trim = "N" Then
allNonFicTitle = data(0)
ElseIf data(2).Trim = "F" Then
allFicTitle = data(0)
End If
Next
' show result
If menuDisplayAll.Checked Then
menuDisplayFic.Checked = False
menuDisplayNonFic.Checked = False
lstBox.Items.Clear()
lstBox.Items.Add(allBooksTitle)
ElseIf menuDisplayFic.Checked Then
menuDisplayNonFic.Checked = False
menuDisplayAll.Checked = False
lstBox.Items.Clear()
lstBox.Items.Add(allFicTitle)
ElseIf menuDisplayNonFic.Checked Then
menuDisplayFic.Checked = False
menuDisplayAll.Checked = False
lstBox.Items.Clear()
lstBox.Items.Add(allNonFicTitle)
End If
End Sub
Yet, Every time I debug it, to actually get the titles to show up in the listbox after i check one option, I have to click the values button again. Also, it only shows one book title or both the nonfiction and the fiction, and then for all book it only shows "N". I don't know what I am doing wrong. Any help would be appreciated!
-
Apr 21st, 2013, 10:28 AM
#2
Re: Displaying book titles
Here is a conceptual example of working with your data (my source I am sure is slightly different than yours yet you can adapt). No UI controls are used, should be easy for you to work out.
Code:
Dim dt As New DataTable
dt.Columns.Add(New DataColumn With {.ColumnName = "Title", .DataType = GetType(String)})
dt.Columns.Add(New DataColumn With {.ColumnName = "Author", .DataType = GetType(String)})
dt.Columns.Add(New DataColumn With {.ColumnName = "Category", .DataType = GetType(String)})
dt.Columns.Add(New DataColumn With {.ColumnName = "Stock", .DataType = GetType(Int32)})
dt.Columns.Add(New DataColumn With {.ColumnName = "Price", .DataType = GetType(Decimal)})
Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser("Books.txt")
MyReader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited
MyReader.Delimiters = New String() {","}
Dim Row As String()
While Not MyReader.EndOfData
Try
Row = MyReader.ReadFields()
dt.Rows.Add(New Object() _
{
Row(0),
Row(1),
IIf(Row(2) = "F", "Fiction", "Non-Fiction").ToString,
CInt(Row(3)),
CDec(Row(4))
}
)
Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
Console.WriteLine(ex.Message)
End Try
End While
End Using
Dim dvFiction As DataView = dt.DefaultView
dvFiction.RowFilter = "Category = 'Fiction'"
For Each row As DataRowView In dvFiction
Console.WriteLine(String.Join(",", row.Row.ItemArray))
Next
Console.WriteLine()
Dim dvNonFiction As DataView = dt.DefaultView
dvFiction.RowFilter = "Category = 'Non-Fiction'"
For Each row As DataRowView In dvNonFiction
Console.WriteLine(String.Join(",", row.Row.ItemArray))
Next
File Books.txt
Code:
Title1,Bob Smith,F,10,12.99
Title2,Jane Anderson,N,2,3.99
Title3,Serge Abiteboul,F,5,10.78
Title4,Dan Suciu,N,3,23.87
Results
Code:
Title1,Bob Smith,Fiction,10,12.99
Title3,Serge Abiteboul,Fiction,5,10.78
Title2,Jane Anderson,Non-Fiction,2,3.99
Title4,Dan Suciu,Non-Fiction,3,23.87
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|