简体   繁体   English

CheckListBox中的词典需要帮助

[英]Dictionary in CheckListBox help required

The code is ac# 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. 该代码是在Visual Studio 2012上完成的ac#Windows Forms Application,该任务的目的是在GUI中使用字典来添加,删除和搜索书籍。

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. 我已经布置了gui应用程序,它包含4个按钮,2个文本字段,2个复选框列表,然后有几个标签来说明它们的作用。

button3 is supposed to activate a search using ISBN. 应将button3激活为使用ISBN的搜索。 (user enters ISBN in textbox1 , then all books that contain a part of that will be matched) (用户在textbox1输入ISBN,那么包含其中一部分的所有书籍都将被匹配)

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. 还有Book类。

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 . 我在为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 . 我在textbox1输入ISBN的textbox1 ,然后单击按钮,然后应浏览字典,如果找到匹配的书,则将其显示在另一个checklistbox2

I have tried quite a few methods in displaying them in to checklistbox2 but when I click the button nothing appears in checklistbox2 . 我尝试了很多方法将它们显示在checklistbox2但是当我单击按钮时, 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 我发现我出了问题的地方,我的逻辑没有错,可惜我的form.design.cs没有包含

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

I have now fixed this and everything works as it should. 现在,我已修复此问题,一切正常。

You can use a lambda expression 您可以使用lambda表达式

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

in my Form1.Designer.cs I had not included 在我的Form1.Designer.cs中,我没有包含

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. 谢谢所有发表评论以帮助我的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM