2015-11-03 66 views
0

我已经初始化的书籍以下ArrayList的,我可以遍历搜索项目:在一个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; 
     } 
    } 
} 

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

+1

什么是你的问题的一个简单的方法? – Blorgbeard

回答

4

其他表单需要能够接受您的输入。您可以添加接受,像窗体2的属性:

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

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

我希望自己的图书类有三个属性作者,标题,流派

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

而对于经过搜索到的详细信息,可以在Form2中创建一个Book属性。

+0

这无关紧要,无论如何,它将与OP初始化列表相同,例如这个 'var listXX = new List (){ “傲慢与偏见”,“简·奥斯汀”,“浪漫” ,“1813”};'什么都进入了textbox.Text它会找到匹配...所以我害怕没有看到'Book'类..你只是猜测..也.Contains不会是什么他正在寻找能够与许多人相匹敌的人 – MethodMan

0
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包含与搜索字词匹配的所有图书。

0

如果您想通过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); 
    } 
}