I'm trying to figure out the best way to solve a problem in my program. Basically, I've got a listbox that holds book codes(1,2,3, etc). 1, 2, 3 would represent 3 different books. I have a text file that holds information such as:
Text file format:
bookCode, bookTitle, bookPrice, quantityofBooks
Text file dummy information:
1, Book One, 22, 5
2, Book Two, 10, 3
3, Book Three, 5, 15
I don't always know how many lines there will be in this text file. What I'm trying to do is to split each individual line and put each line into some sort of array or list, and have the ability to access each individual item in those individual lists (items such as: Book One, Book Two, so that I can put these into a listbox) And, say for example someone clicks on "Book Two" in the list box, my label will change with the price for Book Two.
How can I put the bookTitle (Book One, Book Two, etc) into a listbox, and how can I split each line into separate lists and each individual items for that line into a list?
I'm not quite sure I've explained what I want as well as it could be explained. So I apologise if this has come across mind boggling.
declare this class
public class Book
{
string BookName;
int bookCode;
decimal bookPrice;
int bookQuanity;
}
Then read from the file using this code:
List<Book> Books = new List<Book>();
using (StreamReader sr = new StreamReader(path))
{
while (sr.Peek() >= 0)
{
string str;
string[] strArray;
str = sr.ReadLine();
strArray = str.split(',');
Book currentBook = new Book();
currentBook.bookCode = strArray[0];
currentBook.BookName = strArray[1];
currentBook.bookPrice = double.Parse(strArray[2]);
currentBook.bookCode = int.Parse(strArray[3]);
Books.Add(currentBook);
}
}
//listbox = the name you gave your listbox
listbox.DataSource = Books;
listbox.ValueMember = "bookCode";
listbox.DisplayMember = "BookName";
I would create class for the Book first
public class Book
{
public int BookCode { set;get;}
public string BookTitle { set;get; }
public decimal BookPrice { set;get}
public int Quantity { set;get;"
}
Read from your datasource( in this case the text file) and store it in a List of our Book Class
List<Book> bookList=new List<Book>();
bookList=BookManager.GetBookList(); // gets the list of books read from the text file.
Store this in your global scope so that you can access it from any of your methods in the form.
When you want to access specific data about a book use Linq to get it
var book=(from p in bookList where p.BookCode=2).FirstOrDefault();
if(book !=null)
{
lblPrice.Text=book.BookPrice.ToString();
}
To put the BookTitles to a List box, you can mention the DataTextField as BookTitle when you bind that.
lstBooks.DataSource=bookList;
lstBooks.DataValueField="BookCode";
lstBooks.DataTextField="BookTitle ";
lstBooks.DataBind();
To Read the file from your text file and fill it to your list of Book object, do some thing like this
public List<Book> GetBookList()
{
List<Book> objBooks=new List<Book>();
using (StreamReader file = new StreamReader(@"C:\dataist.txt"))
{
while ((line = file.ReadLine()) != null)
{
char[] delimiters = new char[] { ',' };
string[] parts = line.Split(delimiters);
Book objBook=new Book();
objBook.BookCode=parts[0];
objBook.BookTitle =parts[0];
objBook.BookPrice =Convert.ToDecimal(parts[0]);
objBook.Quantity =Convert.ToInt32(parts[0]);
objBooks.Add(objBook);
}
file.Close();
}
return objBooks;
}
If possible, I will store the book details to a database table. It is more easy to manage and maintain and i dont need to worry about reading and writing to file.
I am assuming you are doing this for a windows form app.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With