在MSDN上看到的一个关于treeview类的操作使用方法示例,在机子上运行了一下有点问题,百思不得其解.
------------------------------------------------
本示例假定有一个 Customer
对象,它可以存放 Order
对象的集合。还假定在应用程序目录中有一个名为 MyWait.cur
的光标文件,并已在 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.OrderID));
}
}
// Reset the cursor to the default for all controls.
Cursor.Current = Cursors.Default;
// Begin repainting the TreeView.
treeView1.EndUpdate();
}
---------------------------------------------------------------------------------------------------------
有谁能帮忙把那个几Customer对象,order对象类的具体定义描述出来吗?
我的是这样的,不过通不过编译:
public class Customer
{
public Customer(string theCustomerName)
{
CustomerName=theCustomerName;
CustomerOrders=new CustomerOrder();
}
public CustomerOrder CustomerOrders;
public string CustomerName;
}
public class CustomerOrder
{
public CustomerOrder()
{
}
public void Add(Order orders)
{
orderkeeper[i]=orders.GetOrder();
}
private string[] orderkeeper;
static int i=0;
}
public class Order
{
public Order(string odr)
{
this.myorder=odr;
}
public string GetOrder()
{
return this.myorder;
}
private string myorder;
}
----------------------------------
谢谢你的关注~!