简体   繁体   English

将信息从文本框显示为C#形式的列表框

[英]Displaying information from a textbox into a listbox in a form C#

I have made the following form and I'm having problems displaying the number of members in the number of members listbox, here is a screenshot before pressing the button 'Add Artist' 我填写了以下表格,但在显示“会员人数”列表框中的会员人数时遇到了问题,这是按“添加艺术家”按钮之前的屏幕截图

在此处输入图片说明

and here is what happens when I press Add Artist, as you can see it uses the contents of Artist Name instead of Number of Artists 这是当我按“添加艺术家”时发生的情况,因为您可以看到它使用“艺术家名称”的内容而不是“艺术家数目”

在此处输入图片说明

Here is my code for form1: 这是我的form1代码:

 public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    Dictionary<String, Book> music = new Dictionary<String, Book>();

    private void btnAdd_Click(object sender, EventArgs e)
    {
        if ((txtIsbn.Text != "") & (txtArtist.Text != "")) // if text artist name and number of artists is present
        {
            try { music.Add(txtArtist.Text, new Book(txtArtist.Text, txtIsbn.Text)); } //txtArtist
            catch { MessageBox.Show("Already Exist!!"); }
        }
        else MessageBox.Show("Please fill in both Artist Name and Number of Members");

        listBox1.Items.Clear();
        listBox2.Items.Clear();
        listBox3.Items.Clear();
        foreach (var pair in music)
        {
            listBox1.Items.Add(pair.Key);
            //listBox2.Items.Add(pair.Value.Onloan);
            listBox3.Items.Add(pair.Key);
        }
    }

and here is my code for the Artist class (called book because I edited a program I created before): 这是我的Artist类的代码(之所以称为book,是因为我编辑了之前创建的程序):

class Book
{
    private String mem; //mem = number of members
    private string artist;


    public Book(string mem, string artist)
    {
        this.mem = mem;
        this.artist = artist;
    }

    public string MEM
    {
        get { return mem; }
        set { mem = value; }
    }

    public string Artist
    {
        get { return artist; }
        set { artist = value; }
    }

I've realised that it's adding the value of artist name from the 'Key' command : listBox3.Items.Add(pair.Key); 我已经意识到,它是通过'Key'命令添加歌手姓名的值的:listBox3.Items.Add(pair.Key);

however I don't know what to change it to.. any help is appreciated thanks alot :) 但是我不知道将其更改为什么..非常感谢任何帮助:)

In your 在你的

    foreach (var pair in music)
    {
        listBox1.Items.Add(pair.Key);
        //listBox2.Items.Add(pair.Value.Onloan);
        Book b = pair.Value;
        listBox3.Items.Add(b.MEM);
    }

You don't add the contents of the MEM-property to the listbox. 您不将MEM属性的内容添加到列表框中。 But I don't see any place you are setting it, either. 但我也看不到您要设置它的任何地方。

It's hard to tell for sure, but it seems like you want this inside the foreach loop: 很难确定,但是好像您希望在foreach循环中这样:

listBox3.Items.Add(pair.Value.MEM);

Also, since the mem argument in your constructor is first, you probably want to swap what you are passing in so that it lines up: 另外,由于构造函数中的mem参数是第一个,因此您可能希望交换传入的内容,以便将其对齐:

new Book(txtIsbn.Text, txtArtist.Text)

This is assuming that txtArtist.Text contains the value you want for Artist , and txtIsbn.Text contains the value you want for MEM . 假设txtArtist.Text包含想要的Artist值,而txtIsbn.Text包含想要的MEM值。

As a side note, it's worth your time to clean up the names, it will make it a lot easier to find problems later. 附带说明一下,值得您花时间清理名称,这将使以后查找问题变得容易得多。

Instead of: 代替:

foreach (var pair in music)
        {
            listBox1.Items.Add(pair.Key);
            //listBox2.Items.Add(pair.Value.Onloan);
            listBox3.Items.Add(pair.Key);
        }

I think you want: 我想你要:

foreach (var pair in music)
        {
            listBox1.Items.Add(pair.Key);
            //listBox2.Items.Add(pair.Value.Onloan);
            listBox3.Items.Add(pair.Value.MEM );
        }

You have your music dictionary containing book by artist name. 您的音乐词典包含按艺术家名称列出的书籍。
So the key is the name of the book, and the value is the book. 因此,关键是书的名称,而值是书。
The right way to do it is : 正确的方法是:

foreach (var pair in music)
{
    var book = pair.Value;
    listBox1.Items.Add(book.Artist);
    listBox3.Items.Add(book.MEM);
}

Instead of catching exceptions, simply check if book already added into dictionary. 无需捕获异常,只需检查书是否已添加到字典中即可。 Also set books as data source for listbox, instead of adding them manually to items: 还将书籍设置为列表框的数据源,而不是将其手动添加到项目中:

private void btnAdd_Click(object sender, EventArgs e)
{
    if (txtIsbn.Text == "" || txtArtist.Text == "")
    {
       MessageBox.Show("Please fill in both Artist Name and Number of Members");
       return;
    }

    Book book = new Book(txtArtist.Text, txtIsbn.Text);

    if (music.ContainsKey(book.Artist))
    {
        MessageBox.Show("Already Exist!!");
        return;
    }

    music.Add(book.Artist, book);
    var books = music.Values.ToList();   

    listBox1.DisplayMember = "Artist"; // set it in designer
    listBox1.DataSource = books;
    listBox3.DisplayMember = "MEM"; // set it in designer
    listBox3.DataSource = books;
}

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

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