繁体   English   中英

CheckListBox中的词典需要帮助

[英]Dictionary in CheckListBox help required

该代码是在Visual Studio 2012上完成的ac#Windows Forms Application,该任务的目的是在GUI中使用字典来添加,删除和搜索书籍。

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

应将button3激活为使用ISBN的搜索。 (用户在textbox1输入ISBN,那么包含其中一部分的所有书籍都将被匹配)

这是我的表格代码

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
{
}

}

还有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;
    }
} 

我在为button3苦苦挣扎。 我在textbox1输入ISBN的textbox1 ,然后单击按钮,然后应浏览字典,如果找到匹配的书,则将其显示在另一个checklistbox2

我尝试了很多方法将它们显示在checklistbox2但是当我单击按钮时, checklistbox2什么也没有出现。

我真的很困惑如何做到这一点。

我努力了。

编辑:

我发现我出了问题的地方,我的逻辑没有错,可惜我的form.design.cs没有包含

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

现在,我已修复此问题,一切正常。

您可以使用lambda表达式

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

在我的Form1.Designer.cs中,我没有包含

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

我添加了这个,我的代码正常工作,我曾经

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

谢谢所有发表评论以帮助我的人。

暂无
暂无

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

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