2015-04-07 40 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) 
    } 
}