Click here to Skip to main content
15,893,508 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
private void frmBookstore_Load(object sender, EventArgs e)
        {
            setUpBookStore();
        }

        private void btnBkStock_Click(object sender, EventArgs e)
        {
            showListOfBooks();

private void setUpBookStore()
        {
            bookstore.Add(new Book1("Spills","Kendrick Williermson", "2015", "5", "4532145321"));

            bookstore.Add(new Book1("Ken", "Lierm Pattasun", "1903", "20", "1235412354"));

            bookstore.Add(new Book1("A Secret Chamber", "Harry Potter", "0000", "10000000", "1111111111"));

            bookstore.Add(new Book1("Eddie", "Rik Mayall", "2000", "31", "7483758376"));

            bookstore.Add(new Book1("Stella the Tortoise", "BL. Ager", "1519", "223", "0003200067"));
        }

        private void showListOfBooks()
        {
            //clear listbox first
            lstBooks.Items.Clear();

            //output data 
           foreach (Book1 b in bookstore)
                lstBooks.Items.Add(b.getTitle() + "by" + b.getAuthor() + 
                    ". Isbn: " + b.getIsbn() + "Year: " + b.getYear() + ".");
        }

I can't understand what else i need to do to gt it to display in the listbox when i click the "BkStock" button. The book class has been created with 5 variables. What am I missing?
Posted
Comments
Sergey Alexandrovich Kryukov 22-Feb-15 17:01pm    
Start with not using ArrayList. This type is obsolete. Use System.Collections.Generic.List<>.
"Display" — what would you mean by that? Display where?...
—SA
Bawwla 22-Feb-15 17:27pm    
So in the form when I click the button, hence "btnBkStock_Click", it shows the list of books that were added to the arraylist in the "showListOfBooks" method called when the form is loaded. If it makes it clearer the form contains simply a button to show the books, and a listbox to print them
Kuthuparakkal 22-Feb-15 17:15pm    
Unclear
Andreas Gieriet 22-Feb-15 17:28pm    
What is the symptom? Did you try debugging?
Andi
Bawwla 22-Feb-15 17:29pm    
Form loads up, but simply nothing happens when I click the button.

1 solution

Try this:

C#
using System;
using System.Collections;
using System.Windows.Forms;

namespace BookstoreTest
{
    public partial class Form1 : Form
    {
        ArrayList bookstore;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            bookstore = new ArrayList();
            setUpBookStore();
        }

        private void btnBkStock_Click(object sender, EventArgs e)
        {
            showListOfBooks();
        }

        private void setUpBookStore()
        {
            bookstore.Add(new Book1("Spills", "Kendrick Williermson", "2015", "5", "4532145321"));

            bookstore.Add(new Book1("Ken", "Lierm Pattasun", "1903", "20", "1235412354"));

            bookstore.Add(new Book1("A Secret Chamber", "Harry Potter", "0000", "10000000", "1111111111"));

            bookstore.Add(new Book1("Eddie", "Rik Mayall", "2000", "31", "7483758376"));

            bookstore.Add(new Book1("Stella the Tortoise", "BL. Ager", "1519", "223", "0003200067"));
        }

        private void showListOfBooks()
        {
            //clear listbox first
            lstBooks.Items.Clear();

            //output data
            foreach (Book1 b in bookstore)
            {
                lstBooks.Items.Add(b.Title + "by" + b.Author + ". Isbn: " + b.ISBN + "Year: " + b.Year + "." + Environment.NewLine);
            }
        }
    }

    public class Book1
    {
        public string Title
        {
            get;
            set;
        }

        public string Author
        {
            get;
            set;
        }

        public string Year
        {
            get;
            set;
        }
        public string Month
        {
            get;
            set;
        }

        public string ISBN
        {
            get;
            set;
        }

        public Book1(string _Title, string _Author, string _Year, string _Month, string _ISBN)
        {
            this.Title = _Title;
            this.Author = _Author;
            this.Year = _Year;
            this.Month = _Month;
            this.ISBN = _ISBN;

        }
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900