在列表框中显示ArrayList项时出现问题。 [英] Problem displaying ArrayList items in a listbox.

查看:70
本文介绍了在列表框中显示ArrayList项时出现问题。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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() + ".");
        }



当我点击BkStock按钮时,我无法理解我还需要做什么来将其显示在列表框中。书类已经创建了5个变量。我错过了什么?


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?

推荐答案

试试这个:



Try this:

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;

        }
    }
}


这篇关于在列表框中显示ArrayList项时出现问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆