Reputation: 23
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"));
}
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?
Upvotes: 0
Views: 110
Reputation: 18843
If you want to test this by Initializing
the List<T>(){}
you can see that the same would work.. however I would suggest that you do not hard code if you could do a List<Class>
meaning your Books
Class then you would just create a new object instance and add that object to a List based on your hard code this would be a simpler way of Initializing a List<T>
object but creating a List<ClassObject>
would be a better approach
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);
}
}
Upvotes: 0
Reputation: 1666
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)
}
Now mybookslist have all books where matches with search terms .
Upvotes: 0
Reputation: 451
I hope your Book Class has three properties 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();
}
}
}
And for passing the the Searched Details you can create a Book property inside Form2 .
Upvotes: 0
Reputation: 24031
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;
...
Upvotes: 4