2015-04-07 39 views
-3

我在窗体中添加了一个RichTextBox控件。我想在RichTextBox中显示ArrayList中的数据。我怎样才能做到这一点?在richtext框中显示阵列数据

try 
{ 
    string filepath = ""; 
    string filename = ""; 
    OpenFileDialog ofd = new OpenFileDialog(); 
    ofd.Filter = "XML files|*.xml"; 
    if (ofd.ShowDialog() == DialogResult.OK) 
    { 
      filepath = ofd.FileName; 
      txtPath.Text = filepath + filename; 
      XMLParser objxmlparser = new XMLParser(); 
      ArrayList al =objxmlparser.readDataLogXml(txtPath.Text); 
     } 
} 
+0

不清楚你在问什么。还有什么'readDataLogXml()'? – MickyD

+0

arraylist是在XMLParser类中声明的,它在程序执行时有一些数据。现在我需要在窗体上显示richtextbox上的数据列表数据。 readDatalogxml是xmlparser类中的方法。 –

+1

ArrayList是对象的集合。 ArrayList中有什么样的对象(string,int,short等等,或者你的XMLParser是否创建了你定义的自定义对象)? – Shar1er80

回答

0

我觉得只要在ArrayList中的对象是简单数据类型,你可以尝试

try 
{ 
    string filepath = ""; 
    string filename = ""; 
    OpenFileDialog ofd = new OpenFileDialog(); 
    ofd.Filter = "XML files|*.xml"; 
    if (ofd.ShowDialog() == DialogResult.OK) 
    { 
      filepath = ofd.FileName; 
      txtPath.Text = filepath + filename; 
      XMLParser objxmlparser = new XMLParser(); 
      ArrayList al =objxmlparser.readDataLogXml(txtPath.Text); 
      for (int i = 0; i < al.Count; i++) 
      { 
       yourRichTextBox.Text += string.Format("{0}\r\n", al[i].ToString()); 
      } 
     } 
} 

否则,如果在ArrayList中的对象是自定义对象创建,那么你需要重写您定义的自定义类中的ToString()方法

public class YourCustomClass 
{ 
    // Your Custom Fields 

    // Your Custom Methods 

    public override string ToString() 
    { 
     // Format how you want this object to display information about itself 
     // The {0}, {1}, etc... are place holders for your custom fields 
     return string.Format("Put what you want to display here: {0} {1} {2}", customField1, customField2, customField3) 
    } 
}