【问题标题】:Displaying information from a textbox into a listbox in a form C#以 C# 的形式将文本框中的信息显示到列表框中
【发布时间】:2013-03-20 18:53:22
【问题描述】:

我制作了以下表格,但在成员数量列表框中显示成员数量时遇到问题,这是按下“添加艺术家”按钮之前的屏幕截图

这是当我按添加艺术家时发生的情况,正如您所见,它使用艺术家姓名的内容而不是艺术家人数

这是我的 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);
        }
    }

这是我的 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; }
    }

我意识到它是从“Key”命令中添加艺术家姓名的值:listBox3.Items.Add(pair.Key);

但是我不知道将其更改为..任何帮助都非常感谢:)

【问题讨论】:

  • 是的,我试过了,但还是不行,懒惰的列表框应该显示组中的人数

标签: c# string forms listbox


【解决方案1】:

很难确定,但您似乎希望在 foreach 循环中这样做:

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

此外,由于构造函数中的 mem 参数是第一个参数,因此您可能希望交换传入的内容以使其对齐:

new Book(txtIsbn.Text, txtArtist.Text)

这是假设txtArtist.Text 包含您想要的Artist 的值,而txtIsbn.Text 包含您想要的MEM 的值。

顺便说一句,清理名称是值得的,这样以后发现问题会容易得多。

【讨论】:

  • 感谢您的回复,我刚刚尝试过,但没有成功。虽然感谢您的建议,但肯定会更改我的代码名称 - 编辑 - 刚刚看到编辑正在尝试跨度>
【解决方案2】:

在你的

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

您不会将 MEM 属性的内容添加到列表框。 但我也没有看到你设置它的任何地方。

【讨论】:

    【解决方案3】:

    代替:

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

    我想你想要:

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

    【讨论】:

      【解决方案4】:

      无需捕获异常,只需检查书籍是否已添加到字典中。还将书籍设置为列表框的数据源,而不是手动将它们添加到项目中:

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

      【讨论】:

      【解决方案5】:

      您的音乐词典包含按艺术家姓名排列的书籍。
      所以key是书名,value是书名。
      正确的做法是:

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

      【讨论】:

        猜你喜欢
        • 2018-01-18
        • 2012-04-29
        • 1970-01-01
        • 2018-10-14
        • 1970-01-01
        • 2017-04-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多