在ArrayList中搜索项目
本文关键字:项目 搜索 ArrayList | 更新日期: 2023-09-27 18:20:22
我已经初始化了以下可以迭代的书籍的ArrayList:
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"));
}
我想制作一个搜索按钮,这样我就可以键入书名,当我按下搜索时,它会将我发送到一个新表单,其中包含该书的所有详细信息,如数组中所示(书名、作者、流派)。
这是我迄今为止的尝试:
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;
}
}
}
基本上,我想知道如何将其发送到包含所有这些详细信息的新表单?
其他表单需要能够接受您的输入。你可以添加一个Form2的属性来接受它,比如:
class Form2{ public Book book { get; set; } ...}
// then in form1:
Form2 form = new Form2();
form.book = book;
...
我希望你的Book class有三个属性Author、Title、Genre
private void button3_Click(object sender, EventArgs e)
{
String match = textbox.Text;
foreach (Object b in list)
{
Books book = (Books)b;
if (book.Author.Contains(match) || book.Title.Contains(match) || book.Genre.Contains(match))
{
Form2 form = new Form2();
form.Book = book;
form.Show();
}
}
}
为了传递Searched Details,您可以在Form2中创建Book属性。
private void button3_Click(object sender, EventArgs e)
{
String match = textbox.Text;
List<Books> mybookslist = new List<Books>();
foreach (Books b in list)
{
if (book.Author.Contains(match) || book.Title.Contains(match) || book.Genre.Contains(match))
{
mybookslist.Add(b);
}
}
//Navigation code here with your data(mybookslist)
}
现在,mybookslist拥有与搜索词匹配的所有书籍。
如果你想通过Initializing
和List<T>(){}
来测试这一点,你可以看到同样的效果。。然而,我建议你不要硬编码,如果你可以做一个List<Class>
,也就是你的Books
类,那么你只需要创建一个新的对象实例,并根据你的硬编码将该对象添加到列表中。这将是初始化List<T>
对象的一种更简单的方法,但创建List<ClassObject>
将是更好的方法
var bookList = new List<object>()
{
"Pride and Prejudice", "Jane Austen", "Romance", "1813",
"The Notebook", "Nicholas Sparks", "Romance", "1996",
"Carrie", "Stephen King", "Horror", "1974",
"The Shining", "Stephen King", "Horror", "1977",
"A Game of Thrones", "George R.R. Martin", "Fantasy", "1996",
"A Clash of Kings", "George R.R. Martin", "Fantasy", "1998",
"A Storm of Swords", "George R.R. Martin", "Fantasy", "2000",
"A Feast for Crows", "George R.R. Martin", "Fantasy", "2005",
"A Dance with Dragons", "George R.R. Martin", "Fantasy", "2011"//,
//etc......,
};
String match = "A Feast for Crows";
var bookslist = new List<object>();
foreach (Object b in bookList)
{
if (b.Equals(match))
{
bookslist.Add(b);
}
}