将文本从arrayList添加到多行文本框 [英] Adding text from an arrayList to a multiline textbox

查看:165
本文介绍了将文本从arrayList添加到多行文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C#的新手.我需要一个文本框来显示arrayList的内容.目前我有此代码

I am new to c#. I need a textbox to display the contents of an arrayList. Presently i have this code

foreach (CSubTopic references in currSubject.Topics)
           {
               strReferences = references.References.ToString();
               txtReference.Multiline = true;
               txtReference.Text = strReferences;

           }



当我运行此代码时,在文本框中获得System.Collections.ArrayList.谁能帮我这个忙.谢谢



When i run this code, i get System.Collections.ArrayList, in the textbox. Can anyone please help me with this. Thank you

推荐答案

txtReference.Multiline = true;
StringBuilder str = new StringBuilder();
foreach (CSubTopic references in currSubject.Topics)
{
    str.AppendFormat("{0}[1}", references.References.ToString(), Environment.NewLine);
}
txtReference.Text = str.ToString();


要获取第一项,只需使用适当的数组索引即可.

To get just the first item, just use the appropriate array index.

string value = currSubject.Topics[0].References.ToString();


您应该使用StringBuilder逐个编写字符串(如果这样做,效率更高stringsconcatenations):
You should write the strings one by one and with StringBuilder (more efficient if you do stringsconcatenations):
StringBuilder strings = new StringBuilder();
foreach (CSubTopic references in currSubject.Topics)
{
    foreach (string s in references.References)
        strings.AppenLine(s);
}
txtReference.Multiline = true;
txtReference.Text = strings.ToString();


这篇关于将文本从arrayList添加到多行文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆