搜索在一个ArrayList项目 [英] Searching for items in an ArrayList

查看:112
本文介绍了搜索在一个ArrayList项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经初始化的书籍以下ArrayList的,我可以遍历:

I have initialised the following ArrayList of books which I can iterate over:

public void InitializeArrayList()
{
    list.Add(new Books("Pride and Prejudice", "Jane Austen", "Romance", "1813"));
    list.Add(new Books("The Notebook", "Nicholas Sparks", "Romance", "1996"));
    list.Add(new Books("Carrie", "Stephen King", "Horror", "1974"));
    list.Add(new Books("The Shining", "Stephen King", "Horror", "1977"));
    list.Add(new Books("A Game of Thrones", "George R.R. Martin", "Fantasy", "1996"));
    list.Add(new Books("A Clash of Kings", "George R.R. Martin", "Fantasy", "1998"));
    list.Add(new Books("A Storm of Swords", "George R.R. Martin", "Fantasy", "2000"));
    list.Add(new Books("A Feast for Crows", "George R.R. Martin", "Fantasy", "2005"));
    list.Add(new Books("A Dance with Dragons", "George R.R. Martin", "Fantasy", "2011"));
    list.Add(new Books("Gone Girl", "Gillian Flynn", "Thriller", "2014"));
    list.Add(new Books("The Girl on the Train", "Paula Hawkins", "Thriller", "2015"));
    list.Add(new Books("The Hunger Games", "Suzanne Collins", "Science Fiction", "2008"));
    list.Add(new Books("Catching Fire", "Suzanne Collins", "Science Fiction", "2009"));
    list.Add(new Books("Mockingjay", "Suzanne Collins", "Science Fiction", "2010"));
    list.Add(new Books("Matilda", "Roald Dahl", "Children's Fiction", "1988"));
    list.Add(new Books("Charlie and the Chocolate Factory", "Roald Dahl", "Children's Fiction", "1964"));
    list.Add(new Books("Room", "Emma Donoghue", "Fiction", "2010"));
    list.Add(new Books("Holes", "Louis Sachar", "Fiction", "1998"));
    list.Add(new Books("About a Boy", "Nick Hornby", "Fiction", "1998"));
}

我想打一个搜索按钮,这样我就可以键入书名,当我preSS搜索会送我去一个新的形式在那本书的所有细节,(载数组中的冠军,作者,流派)。

I want to make a search button so I can type in a book title and when I press search it will send me to a new form with all the details on that book, as contained in the array (title, author, genre).

这是我尝试到目前为止:

This is my attempt so far:

private void button3_Click(object sender, EventArgs e)
{
    String match = textbox.Text;
    foreach (Object b in list)
    {
        Books book = (Books)b;
        if (book.Equals(match))
        {
            Form2 form = new Form2();
            form.Visible = true;
        }
    }
}

基本上,我不知道如何使它发送到新的形式与所有这些细节?

Basically, I'm wondering how to make it send to that new form with all those details?

推荐答案

另一种形式需要能够接受你的意见。您可以添加接受窗体2的属性,是这样的:

The other form needs to be able to accept your input. You can add a property of Form2 that accepts that, something like:

class Form2{ public Book book { get; set; } ...}

// then in form1:
Form2 form = new Form2();
form.book = book;
...

这篇关于搜索在一个ArrayList项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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