【发布时间】:2014-12-05 23:47:17
【问题描述】:
代码是在 Visual Studio 2012 上完成的 c# Windows 窗体应用程序,任务的目的是在 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 的一部分,然后单击按钮,这应该会查看字典,如果找到任何匹配的书,它将在另一个checklistbox2 中显示它们。
我尝试了很多方法将它们显示到checklistbox2,但是当我单击按钮时,checklistbox2 中没有任何内容。
我真的很困惑如何做到这一点。
我试过了。
编辑:
我发现我哪里出错了,我的逻辑没有错,遗憾的是我的 form.design.cs 没有包含
this.button3.Click += new System.EventHandler(this.button3_Click);
我现在已经解决了这个问题,一切正常。
【问题讨论】:
-
您知道您可以(并且应该)重命名控件吗?
-
我在“button3_Click”中看不到任何代码。你现在尝试了什么?
-
这个问题充满了噪音和不必要的评论和解释。您遇到了什么具体问题,以及与该具体问题相关的代码给您带来了困难?你的问题不应该用一整本书的章节来解释。请将其缩减为提出问题所需的最少的文本和代码。
-
@KenWhite 我现在会减少它,然后更具体。谢谢你 kaveman 为我减少了这一点
-
我想知道这个问题是否应该关闭,最初我认为我的字典无法将其添加到检查列表中的某个地方出错了,但结果我错过了来自 form1.designer.cs 的简单行,因此我觉得提出的问题可能是错过领先,尽管如果其他人被字典和 gui 困住,他们可以参考这里并得到答案,所以我不确定,这是怎么回事工作吗?
标签: c# winforms dictionary checklistbox