0

I've got an application I'm building that inputs data into a list, using input textboxes on one tab (say Tab 1). When you hit the command button it adds the data (Book number, author, title, genre, # of pages and Publisher) to a list (books).

It then displays the title of the book in a listbox on tab 2. When you click the item in the listbox on tab 2, I want it to redisplay all the information you just input on tab 1, into textboxes on tab 2. But I can't get information to show up.

Below is my code, including the class I created for the project.

class Book
{
    //attributes
    private string callNumber;
    private string bookTitle;
    private string authorName;
    private string genre;
    private int numberOfPages;
    private string publisher;

    //constructor
    public Book()
    { 
    }
    //accessor
    public void SetNumber(string aNumber)
    {
        callNumber = aNumber;
    }
    public void SetTitle(string aTitle)
    {
        bookTitle = aTitle;
    }
    public void SetAuthor(String aName)
    {
        authorName = aName;
    }
    public void SetGenre(String aGenre)
    {
        genre = aGenre;
    }
    public void SetPages(int aPageNumber)
    {
        numberOfPages = aPageNumber;
    }
    public void SetPublisher(String aPublisher)
    {
        publisher = aPublisher;
    }
    public string GetNumber()
    {
        return callNumber;
    }
    public string GetTitle()
    {
        return bookTitle;
    }
    public string GetAuthor()
    {
        return authorName;
    }
    public string GetGenre()
    {
        return genre;
    }
    public int GetPages()
    {
        return numberOfPages;
    }
    public string GetPublisher()
    {
        return publisher;
    }
}

public partial class Form1 : Form
{

    List<Book> books;

    public Form1()
    {
        InitializeComponent();

        this.books = new List<Book>();
    }

    private void btnAdd_Click(object sender, EventArgs e)
    {
        Book aBook = new Book();
        aBook.SetNumber(txtCallNumber.Text);
        aBook.SetAuthor(txtAuthorName.Text);
        aBook.SetTitle(txtBookTitle.Text);
        aBook.SetGenre(txtGenre.Text);
        aBook.SetPages(int.Parse(txtNumberOfPages.Text));
        aBook.SetPublisher(txtPublisher.Text);




        foreach (Control ctrl in this.Controls)
        {
            if (ctrl is TextBox)
            {
                ((TextBox)ctrl).Clear();
            }
            txtCallNumber.Focus();

            txtAuthorName.Clear();
            txtBookTitle.Clear();
            txtCallNumber.Clear();
            txtGenre.Clear();
            txtNumberOfPages.Clear();
            txtPublisher.Clear();

            lstLibrary.Items.Add(aBook.GetTitle());


        }
    }

    private void lstLibrary_SelectedIndexChanged(object sender, EventArgs e)
    {

        int index = 0;
        foreach (Book book in books)
        {

            string tempTitle;
            tempTitle = book.GetTitle();
            if (tempTitle == (string)lstLibrary.SelectedItem)
                break;

            else
            {
                index++;
            }


        txtNumberRecall.Text = books[index].GetNumber();
        txtTitleRecall.Text = books[index].GetTitle();
        txtAuthorRecall.Text = books[index].GetAuthor();
        txtGenreRecall.Text = books[index].GetGenre();
        txtPagesRecall.Text = Convert.ToString(books[index].GetPages());
        txtPublisherRecall.Text = books[index].GetPublisher();
        break;
            }
        }
    }
}

Once again, I'm trying to get the information from the listbox (in the click event) to show up in the textboxes.

4

2 回答 2

0

In the btnAdd_Click method, you are never saving the new aBook that you created. You need to add it to you books collection. Adding the title as an entry in lstLibrary.Items does not actually save the newly created object.

Also, you should review your looping structures. In btnAdd_Click() it looks like you will add it to lstLibrary once for each control that exists on your form. In lstLibrary_SelectedIndexChanged(), if you had actually added books to the collection in btnAdd_Click(), you would update the textboxes for the first book in the collection that does not match the selected book.

于 2013-04-04T14:32:54.790 回答
0

这样的事情会起作用吗?

private void button1_Click(object sender, EventArgs e)
    {
        int i = 0;
        foreach (string s in listBox1.Items)
        {
            i++;
            if (i == 1)
            {
                textBox1.Text = s;
            }
            if (i == 2)
            {
                textBox2.Text = s;
            }
            if (i == 3)
            {
                textBox3.Text = s;
            }
        }
    }
于 2013-04-04T12:28:00.677 回答