在多行文本框中显示 ArrayList 条目
我在显示存储为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里有几个问题 - 主要的一个是 ArrayList books 不是字符串的集合,而是 book 对象的集合。因此,您的 foreach 循环需要迭代书籍对象而不是字符串:
第二个问题是您试图将字符串值分配给 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:
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.
如果这是针对 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.