System Information

Impossible Is Nothing...

导航

微软IE控件TreeView相关

TreeView下载

CSDN TreeView专题讨论:http://search.csdn.net/Expert/topic/1525/1525202.xml?temp=.9699365

TreeView 下载:(有源码,可编译)

http://msdn.microsoft.com/archive/en-us/samples/internet/asp_dot_net_servercontrols/webcontrols/default.asp

MSI文件:

http://www.microsoft.com/china/msdn/archives/library/dnaspp/html/aspnet-usingtreeviewiewebcontrol.asp

网友Blog:http://blog.csdn.net/xuandhu/archive/2005/04/18/352182.aspx

关于 TreeView WebControl

http://www.microsoft.com/china/msdn/archives/library/workshop/webcontrols/overview/treeview.asp

Using the TreeView Control

See Also

The TreeView control is designed to display data that is hierarchical in nature, such as organization trees, the entries in an index, the files and directories on a disk.

Figure 2.40   Typical TreeView

Possible Uses

  • To create an organization tree that can be manipulated by the user.

  • To create a tree that shows at least two or more levels of a database.

Setting Node Object Properties

A "tree" is comprised of cascading branches of "nodes," and each node typically consists of an image (set with the Image property) and a label (set with the Text property). Images for the nodes are supplied by an ImageList control associated with the TreeView control. For more information on using the ImageList control with other controls, see "Using the ImageList control."

A node can be expanded or collapsed, depending on whether or not the node has child nodes — nodes which descend from it. At the topmost level are "root" nodes, and each root node can have any number of child nodes. The total number of nodes is not limited (except by machine constraints). Figure 2.41 shows a tree with two root nodes; "Root 1" has three child nodes, and "Child 3" has a child node itself. "Root 2" has child nodes, as indicated by the "+" sign, but is unexpanded.

Figure 2.41   Root and child nodes

Each node in a tree is actually a programmable Node object, which belongs to the Nodes collection. As in other collections, each member of the collection has a unique Index and Key property which allows you to access the properties of the node. For example, the code below uses the Index of a particular node ("7") to set the Image and Text properties:

tvwMyTree.Nodes(7).Image = "closed" 
tvwMyTree.Nodes(7).Text = "IEEE"

However, if a unique key, for example "7 ID" had been assigned to the node, the same code could be written as follows:

tvwMyTree.Nodes("7 ID").Image = "closed"
tvwMyTree.Nodes("7 ID").Text = "IEEE"

Node Relationships and References to Relative Nodes

Each node can be either a child or a parent, depending on its relationship to other nodes. The Node object features several properties which return various kinds of information about children or parent nodes. For example, the following code uses the Children property to return the number of children — if any — a node has:

MsgBox tvwMyTree.Nodes(10).Children

However, some of the properties do not return information, as the Children property does, but instead return a reference to another node object. For example, the Parent property returns a reference to the parent of any particular node (as long as the node is not a root node). With this reference, you can manipulate the parent node by invoking any methods, or setting properties, that apply to Node objects. For example, the code below returns the Text and Index properties of a parent node:

MsgBox tvwMyTree.Nodes(10).Parent.Text
MsgBox tvwMyTree.Nodes(10).Parent.Index

Tip   Use the Set statement with an object variable of type Node to manipulate references to other Node objects. For example, the code below sets a Node object variable to the reference returned by the Parent property. The code then uses the object variable to return properties of the relative node:

Dim tempNode As Node ' Declare object variable.
' Set object variable to returned reference.
Set tempNode = tvwMyTree.Nodes(10).Parent 
MsgBox tempNode.Text ' Returns parent's Text.
MsgBox tempNode.Index ' Returns parent's Index.

Adding Node Objects to the Nodes Collection

To add a Node to the tree, use the Add method (Nodes collection). This method includes two arguments, relative and relationship, which can determine where the node will be added. The first argument relative names a node; the second argument relationship specifies the relationship between the new node and the node named in relative.

For example, the following code adds a node named "11 node" as a child of another node named "7 node." The intrinsic constant tvwChild specifies that the new node is a child of the node named in the previous argument. The third argument assigns the Key property to the new node.

tvwMyTree.Nodes.Add "7 node", tvwChild, "11 node"

Other possible relationships include:

Constant Value Description
tvwLast 1 The Node is placed after all other nodes at the same level of the node named in relative.
tvwNext 2 The Node is placed after the node named in relative.
tvwPrevious 3 The Node is placed before the node named in relative.
tvwChild 4 The Node becomes a child node of the node named in relative.

For example, suppose there were three existing nodes, and you wished to place a fourth node between the second and the third nodes, the code would be:

' Assuming the second node's Key value is "2 node".
tvwMyTree.Nodes.Add "2 node", tvwNext

Other arguments of the Add method are key, text, and image. Using these arguments, you can assign the Key, Text, and Image properties as the Node object is created.

For More Information   For more information about the Nodes collection's Add method See "Add Method" by typing "Add Method" in the Index search and clicking "Add Method (Nodes Collection)."

A second way of adding nodes is to declare an object variable of type Node, and then use the Set statement with the Add method. The Set statement sets the object variable to the new node. You can then use the object variable to set the node's properties, as shown below:

Dim nodX As Node
Set nodX = tvwMyTree.Nodes.Add("10 node", tvwChild)
nodX.Key = "11 node"
nodX.Text = "IEEE"
nodX.Image = "closed"

Tip   Using the Set statement with the Add method makes reading and debugging your code easier. However, using the Add method and its arguments to add nodes creates faster code.

TreeView Class

Displays a hierarchical collection of labeled items, each represented by a TreeNode.

For a list of all members of this type, see TreeView Members.

System.Object
   System.MarshalByRefObject
      System.ComponentModel.Component
         System.Windows.Forms.Control
            System.Windows.Forms.TreeView

[Visual Basic]
Public Class TreeView
   Inherits Control
[C#]
public class TreeView : Control
[C++]
public __gc class TreeView : public Control
[JScript]
public class TreeView extends Control

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Remarks

The Nodes collection holds all the TreeNode objects that are assigned to the TreeView control. The tree nodes in this collection are referred to as the root tree nodes. Any tree node that is subsequently added to a root tree node is referred to as a child node. Because each TreeNode can contain a collection of other TreeNode objects, you might find it difficult to determine your location in the tree structure when you iterate through the collection. You can parse the TreeNode.FullPath string by using the PathSeparator string value to determine where a TreeNode label begins and ends.

You can display images next to the tree nodes by assigning an ImageList object to the ImageList property, and referencing the index value of an Image in the ImageList to assign that Image. Set the ImageIndex property to the index value of the Image that you want to display when a tree node is not selected. Likewise, set the SelectedImageIndex property to the index value of the Image that you want to display when a tree node is selected. The images referenced by the ImageIndex and SelectedImageIndex property values are the default images displayed by all the tree nodes that are assigned to the Nodes collection. Each tree node can override the default images by setting the TreeNode.ImageIndex and TreeNode.SelectedImageIndex properties.

Tree nodes can be expanded to display the next level of child tree nodes. The user can expand the TreeNode by clicking the plus-sign (+) button, if one is displayed next to the TreeNode, or you can expand the TreeNode by calling the TreeNode.Expand method. To expand all the child tree node levels in the Nodes collection, call the ExpandAll method. You can collapse the child TreeNode level by calling the TreeNode.Collapse method, or the user can press the minus-sign (-) button, if one is displayed next to the TreeNode. You can also call the TreeNode.Toggle method to alternate between the expanded and collapsed states.

Tree nodes can optionally display check boxes. To display the check boxes, set the CheckBoxes property of the TreeView to true. The Checked property is set to true for tree nodes that are in a checked state.

Note   Setting the TreeNode.Checked property from within the BeforeCheck or AfterCheck event causes the event to be raised multiple times and can result in unexpected behavior. For example, you might set the Checked property in the event handler when you are recursively updating the child nodes, so the user does not have to expand and check each one individually. To prevent the event from being raised multiple times, add logic to your event handler that only executes your recursive code if the Action property of the TreeViewEventArgs is not set to TreeViewAction.Unknown. For an example of how to do this, see the Example section of the AfterCheck or BeforeCheck events.

You can change the appearance of the TreeView control by setting some of its display and style properties. Setting ShowPlusMinus to true displays a plus-sign or minus-sign button next to each TreeNode object that can be expanded or collapsed, respectively. Setting the ShowRootLines property to true causes the TreeView to display lines that join all the root tree nodes together. You can display lines that connect child tree nodes to their root node by setting the ShowLines property to true. Setting the HotTracking property to true changes the appearance of the tree node labels as the mouse pointer passes over them. When hot-tracked, the tree node labels take on the appearance of a hyperlink.

Note   When setting the CheckBoxes, Scrollable, ImageIndex, and SelectedImageIndex properties at run time, the TreeView handle is recreated (see Control.RecreateHandle) to update the control's appearance. This causes all tree nodes to be collapsed, with the exception of the selected TreeNode.

Example

[Visual Basic, C#, C++] The following example displays customer information in a TreeView control. The root tree nodes display customer names, and the child tree nodes display the order numbers assigned to each customer. In this example, 1,000 customers are displayed with 15 orders each. The repainting of the TreeView is suppressed by using the BeginUpdate and EndUpdate methods, and a wait Cursor is displayed while the TreeView creates and paints the TreeNode objects. This example assumes you have a Customer object that can hold a collection of Order objects. It also assumes that you have a cursor file named MyWait.cur in the application directory and that you have created an instance of a TreeView control on a Form.

[Visual Basic] 
' Create a new ArrayList to hold the Customer objects.
Private customerArray As New ArrayList()

Private Sub FillMyTreeView()
   ' Add customers to the ArrayList of Customer objects.
   Dim x As Integer
   For x = 0 To 999
      customerArray.Add(New Customer("Customer" + x.ToString()))
   Next x

   ' Add orders to each Customer object in the ArrayList.
   Dim customer1 As Customer
   For Each customer1 In customerArray
      Dim y As Integer
      For y = 0 To 14
         customer1.CustomerOrders.Add(New Order("Order" + y.ToString()))
      Next y
   Next customer1

   ' 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.
   Dim customer2 As Customer
   For Each customer2 In customerArray
      treeView1.Nodes.Add(New TreeNode(customer2.CustomerName))

      ' Add a child TreeNode for each Order object in the current Customer object.
      Dim order1 As Order
      For Each order1 In customer2.CustomerOrders
         treeView1.Nodes(customerArray.IndexOf(customer2)).Nodes.Add( _
    New TreeNode(customer2.CustomerName + "." + order1.OrderID))
      Next order1
   Next customer2

   ' Reset the cursor to the default for all controls.
   Cursor.Current = System.Windows.Forms.Cursors.Default

   ' Begin repainting the TreeView.
   treeView1.EndUpdate()
End Sub 'FillMyTreeView

[C#] 
// 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();
}

[C++] 
void FillMyTreeView() {
    // Add customers to the ArrayList of Customer objects.
    for (int x=0; x<1000; x++) {
        customerArray->Add(new Customer(String::Concat(S"Customer ", __box(x))));
    }

    // Add orders to each Customer object in the ArrayList.
    IEnumerator* myEnum = customerArray->GetEnumerator();
    while (myEnum->MoveNext()) {
        Customer* customer1 = __try_cast(myEnum->Current);

        for (int y=0; y<15; y++) {
            customer1->CustomerOrders->Add(new Order(String::Concat(S"Order ", __box(y))));
        }
    }

    // Display a wait cursor while the TreeNodes are being created.
    Cursor::Current = new System::Windows::Forms::Cursor(S"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.
    while (myEnum->MoveNext()) {
        Customer* customer2 = __try_cast(myEnum->Current);

        treeView1->Nodes->Add(new TreeNode(customer2->CustomerName));

        // Add a child treenode for each Order object in the current Customer object.
        IEnumerator* myEnum = customer2->CustomerOrders->GetEnumerator();
        while (myEnum->MoveNext()) {
            Order* order1 = __try_cast(myEnum->Current);

            treeView1->Nodes->Item[customerArray->IndexOf(customer2)]->Nodes->Add(
                new TreeNode(String::Concat(customer2->CustomerName, S".", order1->OrderID)));
        }
    }

    // Reset the cursor to the default for all controls.
    Cursor::Current = Cursors::Default;

    // Begin repainting the TreeView.
    treeView1->EndUpdate();
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Windows.Forms

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

Assembly: System.Windows.Forms (in System.Windows.Forms.dll)


树的实现(网友boytomato (深爱一人叫颖的女孩!) )
http://community.csdn.net/Expert/topic/4017/4017253.xml?temp=.1536829

posted on 2005-06-12 20:13  SysInfo  阅读(6017)  评论(3编辑  收藏  举报