在多行文本框中显示 ArrayList 条目

发布于 2024-12-12 03:38:27 字数 986 浏览 0 评论 0原文

我在显示存储为 ArrayList() 的内容时遇到问题:

private void btnEnter_Click(object sender, EventArgs e)
{
    public ArrayList books = new ArrayList();
    books = new ArrayList();

    // first I am just using lebels for easier input.
    books.Add(new Book(label1.Text, label2.Text, label3.Text, label4.Text, float.Parse(label5.Text)));
}

正如我们所见,我正在使用构造函数来存储包含 5 个元素的数据。问题是我不确定如何使用 foreach 循环显示数据:

public ArrayList books;
public BookList()
{
    InitializeComponent();

    foreach (string data in books)
        txtBookList.Text = data.ToString();
}

我试图在多行文本框中显示内容,但我不确定我到底做错了什么。有什么建议吗?

这是 Book() 构造函数的代码:

public Book(string title, string firstName, string lastName, string publisherName, float price)
: base(title, publisherName, price)
{
    this.authorFirstName = firstName;
    this.authorLastName = lastName;
} 

-- 编辑 -- 有人指出我确实想将 ArrayList 显示为对象。我该怎么做呢?

问候。

求助者

I am having a problem with displaying a content from a ArrayList() stored as such:

private void btnEnter_Click(object sender, EventArgs e)
{
    public ArrayList books = new ArrayList();
    books = new ArrayList();

    // first I am just using lebels for easier input.
    books.Add(new Book(label1.Text, label2.Text, label3.Text, label4.Text, float.Parse(label5.Text)));
}

As we see I am using a constructor to store data with 5 elements. The problem is that I am not sure how to display the data using foreach loop:

public ArrayList books;
public BookList()
{
    InitializeComponent();

    foreach (string data in books)
        txtBookList.Text = data.ToString();
}

I am trying to display the content in multiline textbox and I am not sure what I'm exacly doing wrong. Any tips?

Here is the code for Book() constructor:

public Book(string title, string firstName, string lastName, string publisherName, float price)
: base(title, publisherName, price)
{
    this.authorFirstName = firstName;
    this.authorLastName = lastName;
} 

-- EDIT --
One person pointed out that I do want to display the ArrayList as a objects. How would I do it?

Regards.

HelpNeeder

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

十级心震 2024-12-19 03:38:27

这里有几个问题 - 主要的一个是 ArrayList books 不是字符串的集合,而是 book 对象的集合。因此,您的 foreach 循环需要迭代书籍对象而不是字符串:

    foreach (Book b in books)
    {
        txtBookList.Text += b.authorFirstName;
        txtBookLIst.Text += b.authorLastName;
    }

第二个问题是您试图将字符串值分配给 txtBookList 变量,我假设它是一个文本框。尝试改用 Text 属性,并使用 += 运算符将新字符串附加到该值,如上所示。

You have a couple of problems here - the main one being that your ArrayList books isn't a collection of strings, it's a collection of book objects. Therefore your foreach loop needs to iterate through book objects instead of strings:

    foreach (Book b in books)
    {
        txtBookList.Text += b.authorFirstName;
        txtBookLIst.Text += b.authorLastName;
    }

The second problem is that you are trying to assign a string value to the txtBookList variable, which I'm assuming is a textbox. Try using the Text property instead and use the += operator to append new strings to the value, like above.

回心转意 2024-12-19 03:38:27

如果这是针对 Windows 窗体,请查看 TextBox.Lines 属性。这应该可以帮助您在文本框中显示多行。

If this is for Windows Forms, take a look at the TextBox.Lines property. That should get you on the way to displaying multiple lines in the text box.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文