咋整在一个对象里包含另一个对象?~~~~~~~~
下面的代码示例在 TreeView 控件中显示客户信息。根树节点显示客户名称,子树节点显示分配给每个客户的订单编号。在本示例中,显示 1,000 个客户,每个客户有 15 个订单。使用 BeginUpdate 方法和 EndUpdate 方法可取消重新绘制 TreeView,在 TreeView 创建和绘制 TreeNode 对象时,显示等待 Cursor。本示例要求有一个 Customer 对象,它可存放 Order 对象的集合。本示例还要求您在 Form 上创建了 TreeView 控件的一个实例。
// Create a new ArrayList to hold the Customer objects.
private ArrayList customerArray = new ArrayList();
private void FillMyTreeView()
{
// Add customers to the ArrayList of Customer objects.
for(int x=0; x <1000; x++)
{
customerArray.Add(new Customer( "Customer " + x.ToString()));
}
// Add orders to each Customer object in the ArrayList.
foreach(Customer customer1 in customerArray)
{
for(int y=0; y <15; y++)
{
customer1.CustomerOrders.Add(new Order( "Order " + y.ToString()));
}
}
// Display a wait cursor while the TreeNodes are being created.
Cursor.Current = new Cursor( "MyWait.cur ");
// Suppress repainting the TreeView until all the objects have been created.
treeView1.BeginUpdate();
// Clear the TreeView each time the method is called.
treeView1.Nodes.Clear();
// Add a root TreeNode for each Customer object in the ArrayList.
foreach(Customer customer2 in customerArray)
{
treeView1.Nodes.Add(new TreeNode(customer2.CustomerName));
// Add a child treenode for each Order object in the current Customer object.
foreach(Order order1 in customer2.CustomerOrders)
{
treeView1.Nodes[customerArray.IndexOf(customer2)].Nodes.Add(
new TreeNode(customer2.CustomerName + ". " + order1.O