Dictionary in CheckListBox help required

252 Views Asked by At

The code is a c# Windows Forms Application done on Visual Studio 2012, the aim of the task is to use dictionary in a GUI to add, remove, and search for books.

I have laid out my gui application, it contains 4 buttons, 2 textfields, 2 checkboxlists,and then a few labels to explain what they do.

button3 is supposed to activate a search using ISBN. (user enters ISBN in textbox1, then all books that contain a part of that will be matched)

Here is my form code

Dictionary<string, Book> library = new Dictionary<string, Book>();
public Form1()
{
    InitializeComponent();
    button1.Text = "Add Book";
    button2.Text = "Remove Book";
    button3.Text = "Search Using ISBN";
    button4.Text = "Search Using Title";
    label1.Text = "Enter ISBN below";
    label2.Text = "Enter Title below";
    label3.Text = "Tick boxes on the left display if a book is loaned or not";
    label4.Text = "All books found after search";
}

public void Update()
{
    checkedListBox1.Items.Clear();
    foreach (var pair in library)
    {
        checkedListBox1.Items.Add(pair.Value);
    }
}
private void button1_Click(object sender, EventArgs e) //Add Button
{
    if (textBox1.Text != "" && textBox2.Text != "")
    {
        library[textBox1.Text] = new Book(textBox1.Text, textBox2.Text);
        Update();
    }
}
private void button2_Click(object sender, EventArgs e) //Remove Button
{
        library.Remove(textBox1.Text);
        Update();
}
private void button3_Click(object sender, EventArgs e) //ISBN Search Button
{
}

}

And the Book class.

class Book
{
    private String isbn;
    private string title
    private Boolean onloan = false;
    public Book(string isbn, string title)
    {
        this.isbn = isbn;
        this.title = title;
    }
    public string ISBN
    {
        get { return isbn; }
        set { isbn = value; }
    }
    public string Title
    {
        get { return title; }
        set { title = value; }
    }
    override public String ToString()
    {
        return this.ISBN + "        " + this.Title;
    }
} 

I am struggling with button3. I enter a partial bit of an ISBN in textbox1, then click the button, this should then look through the dictionary and if it finds any book that matches it will display them in the other checklistbox2.

I have tried quite a few methods in displaying them in to checklistbox2 but when I click the button nothing appears in checklistbox2.

I'm really stumped on how to do this.

I have tried.

EDIT:

I have found out where I was going wrong, there was nothing wrong with my logic, sadly my form.design.cs did not contain

this.button3.Click += new System.EventHandler(this.button3_Click);

I have now fixed this and everything works as it should.

2

There are 2 best solutions below

0
On BEST ANSWER

in my Form1.Designer.cs I had not included

this.button3.Click += new System.EventHandler(this.button3_Click);

I added this and my code works as normal, I used

private void button3_Click(object sender, EventArgs e) //ISBN Search Button
{
    checkedListBox2.Items.Clear(); //clears list on click

    foreach (var pair in library)
    {
        if (pair.Key.Contains(textBox1.Text))
        {
            checkedListBox2.Items.Add(pair.Value);
        }
    }  
}

Thank you everyone who commented to help me.

3
On

You can use a lambda expression

private void button3_Click(object sender, EventArgs e) //ISBN Search Button
{
    checkedListBox2.Items.Add(_library.First(i => i.Key == textBox1.Text).Value);
}