Bagikan melalui


TreeNode Kelas

Definisi

Mewakili simpul dari TreeView.

public ref class TreeNode : MarshalByRefObject, ICloneable, System::Runtime::Serialization::ISerializable
[System.ComponentModel.TypeConverter(typeof(System.Windows.Forms.TreeNodeConverter))]
[System.Serializable]
public class TreeNode : MarshalByRefObject, ICloneable, System.Runtime.Serialization.ISerializable
[System.ComponentModel.TypeConverter(typeof(System.Windows.Forms.TreeNodeConverter))]
[System.Serializable]
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]
public class TreeNode : MarshalByRefObject, ICloneable, System.Runtime.Serialization.ISerializable
[<System.ComponentModel.TypeConverter(typeof(System.Windows.Forms.TreeNodeConverter))>]
[<System.Serializable>]
type TreeNode = class
    inherit MarshalByRefObject
    interface ICloneable
    interface ISerializable
[<System.ComponentModel.TypeConverter(typeof(System.Windows.Forms.TreeNodeConverter))>]
[<System.Serializable>]
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)>]
type TreeNode = class
    inherit MarshalByRefObject
    interface ICloneable
    interface ISerializable
Public Class TreeNode
Inherits MarshalByRefObject
Implements ICloneable, ISerializable
Warisan
Turunan
Atribut
Penerapan

Contoh

Contoh kode berikut menampilkan informasi pelanggan dalam TreeView kontrol. Simpul pohon akar menampilkan nama pelanggan, dan simpul pohon anak menampilkan nomor pesanan yang ditetapkan untuk setiap pelanggan. Dalam contoh ini, 1.000 pelanggan ditampilkan dengan masing-masing 15 pesanan. Pengecatan TreeView ulang ditekan dengan menggunakan BeginUpdate metode dan EndUpdate , dan tunggu Cursor ditampilkan saat TreeView membuat dan melukis TreeNode objek. Contoh ini mengharuskan Anda memiliki Customer objek yang dapat menyimpan kumpulan Order objek. Ini juga mengharuskan Anda telah membuat instans TreeView kontrol pada Form.

// The basic Customer class.
ref class Customer: public System::Object
{
private:
   String^ custName;

protected:
   ArrayList^ custOrders;

public:
   Customer( String^ customername )
   {
      custName = "";
      custOrders = gcnew ArrayList;
      this->custName = customername;
   }


   property String^ CustomerName 
   {
      String^ get()
      {
         return this->custName;
      }

      void set( String^ value )
      {
         this->custName = value;
      }

   }

   property ArrayList^ CustomerOrders 
   {
      ArrayList^ get()
      {
         return this->custOrders;
      }

   }

};


// End Customer class
// The basic customer Order class.
ref class Order: public System::Object
{
private:
   String^ ordID;

public:
   Order( String^ orderid )
   {
      ordID = "";
      this->ordID = orderid;
   }


   property String^ OrderID 
   {
      String^ get()
      {
         return this->ordID;
      }

      void set( String^ value )
      {
         this->ordID = value;
      }

   }

};
// End Order class



void FillMyTreeView()
{
   // Add customers to the ArrayList of Customer objects.
   for ( int x = 0; x < 1000; x++ )
   {
      customerArray->Add( gcnew Customer( "Customer " + x ) );
   }
   
   // Add orders to each Customer object in the ArrayList.
   IEnumerator^ myEnum = customerArray->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      Customer^ customer1 = safe_cast<Customer^>(myEnum->Current);
      for ( int y = 0; y < 15; y++ )
      {
         customer1->CustomerOrders->Add( gcnew Order( "Order " + y ) );
      }
   }

   // Display a wait cursor while the TreeNodes are being created.
   ::Cursor::Current = gcnew System::Windows::Forms::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.
   myEnum = customerArray->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      Customer^ customer2 = safe_cast<Customer^>(myEnum->Current);
      treeView1->Nodes->Add( gcnew 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 = safe_cast<Order^>(myEnum->Current);
         treeView1->Nodes[ customerArray->IndexOf( customer2 ) ]->Nodes->Add( gcnew TreeNode( customer2->CustomerName + "." + order1->OrderID ) );
      }
   }
   
   // Reset the cursor to the default for all controls.
   ::Cursor::Current = Cursors::Default;
   
   // Begin repainting the TreeView.
   treeView1->EndUpdate();
}

// The basic Customer class.
public class Customer : System.Object
{
   private string custName = "";
   protected ArrayList custOrders = new ArrayList();

   public Customer(string customername)
   {
      this.custName = customername;
   }

   public string CustomerName
   {      
      get{return this.custName;}
      set{this.custName = value;}
   }

   public ArrayList CustomerOrders 
   {
      get{return this.custOrders;}
   }
} // End Customer class 

// The basic customer Order class.
public class Order : System.Object
{
   private string ordID = "";

   public Order(string orderid)
   {
      this.ordID = orderid;
   }

   public string OrderID
   {      
      get{return this.ordID;}
      set{this.ordID = value;}
   }
} // End Order class

// 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();
}
Public Class Customer
   Inherits [Object]
   Private custName As String = ""
   Friend custOrders As New ArrayList()

   Public Sub New(ByVal customername As String)
      Me.custName = customername
   End Sub

   Public Property CustomerName() As String
      Get
         Return Me.custName
      End Get
      Set(ByVal Value As String)
         Me.custName = Value
      End Set
   End Property

   Public ReadOnly Property CustomerOrders() As ArrayList
      Get
         Return Me.custOrders
      End Get
   End Property
End Class


Public Class Order
   Inherits [Object]
   Private ordID As String

   Public Sub New(ByVal orderid As String)
      Me.ordID = orderid
   End Sub

   Public Property OrderID() As String
      Get
         Return Me.ordID
      End Get
      Set(ByVal Value As String)
         Me.ordID = Value
      End Set
   End Property
End Class

' 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

Keterangan

Koleksi Nodes menyimpan semua objek turunan TreeNode yang ditetapkan ke saat ini TreeNode. Anda dapat menambahkan, menghapus, atau mengkloning TreeNode; ketika Anda melakukan ini, semua simpul pohon anak ditambahkan, dihapus, atau dikloning. Masing-masing TreeNode dapat berisi kumpulan objek lain TreeNode . Ini dapat menyulitkan untuk menentukan di mana Anda berada saat TreeView melakukan iterasi melalui koleksi. Untuk menentukan lokasi Anda dalam struktur pohon, gunakan FullPath properti . String FullPath dapat diuraikan menggunakan PathSeparator nilai string untuk menentukan di mana TreeNode label dimulai dan berakhir.

Label TreeNode diatur dengan mengatur Text properti secara eksplisit. Alternatifnya adalah membuat simpul pohon menggunakan salah TreeNode satu konstruktor yang memiliki parameter string yang mewakili Text properti . Label ditampilkan di samping TreeNode gambar, jika label ditampilkan.

Untuk menampilkan gambar di samping simpul pohon, tetapkan ImageList ke ImageList properti kontrol induk TreeView dan tetapkan Image dengan merujuk nilai indeksnya di ImageList properti . Atur ImageIndex properti ke nilai indeks yang Image ingin Anda tampilkan saat TreeNode berada dalam status tidak dipilih. Demikian juga, atur SelectedImageIndex properti ke nilai indeks yang Image ingin Anda tampilkan saat TreeNode dipilih.

Memilih simpul pohon tertentu dan melakukan iterasi Nodes melalui koleksi dapat dicapai dengan menggunakan nilai properti berikut: FirstNode, , LastNode, NextNodePrevNode, NextVisibleNode, . PrevVisibleNode Tetapkan yang TreeNode dikembalikan oleh salah satu properti yang disebutkan di atas ke TreeView.SelectedNode properti untuk memilih simpul pohon tersebut TreeView dalam kontrol.

Simpul pohon dapat diperluas untuk menampilkan tingkat simpul pohon anak berikutnya. Pengguna dapat memperluas TreeNode dengan menekan tombol plus (+) di TreeNodesamping , jika ditampilkan, atau Anda dapat memperluas TreeNode dengan memanggil Expand metode . Untuk memperluas semua tingkat simpul pohon anak dalam Nodes koleksi, panggil ExpandAll metode . Anda dapat menciutkan tingkat anak TreeNode dengan memanggil Collapse metode , atau pengguna dapat menekan tombol minus (-) di TreeNodesamping , jika ditampilkan. Anda juga dapat memanggil Toggle metode untuk mengganti TreeNode antara status yang diperluas dan diciutkan.

Simpul pohon dapat secara opsional menampilkan kotak centang. Untuk menampilkan kotak centang, atur CheckBoxes properti ke TreeViewtrue. Properti Checked diatur ke true untuk simpul pohon yang dalam keadaan diperiksa.

Konstruktor

Nama Deskripsi
TreeNode()

Menginisialisasi instans baru dari kelas TreeNode.

TreeNode(SerializationInfo, StreamingContext)

Menginisialisasi instans TreeNode baru kelas menggunakan informasi dan konteks serialisasi yang ditentukan.

TreeNode(String, Int32, Int32, TreeNode[])

Menginisialisasi instans TreeNode baru kelas dengan teks label, simpul pohon anak, dan gambar yang ditentukan untuk ditampilkan saat simpul pohon dalam keadaan dipilih dan tidak dipilih.

TreeNode(String, Int32, Int32)

Menginisialisasi instans TreeNode baru kelas dengan teks dan gambar label yang ditentukan untuk ditampilkan saat simpul pohon dalam keadaan dipilih dan tidak dipilih.

TreeNode(String, TreeNode[])

Menginisialisasi instans TreeNode baru kelas dengan teks label dan simpul pohon anak yang ditentukan.

TreeNode(String)

Menginisialisasi instans TreeNode baru kelas dengan teks label yang ditentukan.

Properti

Nama Deskripsi
BackColor

Mendapatkan atau mengatur warna latar belakang simpul pohon.

Bounds

Mendapatkan batas simpul pohon.

Checked

Mendapatkan atau menetapkan nilai yang menunjukkan apakah simpul pohon dalam keadaan diperiksa.

ContextMenu

Mendapatkan menu pintasan yang terkait dengan simpul pohon ini.

ContextMenuStrip

Mendapatkan atau menyetel menu pintasan yang terkait dengan simpul pohon ini.

FirstNode

Mendapatkan simpul pohon anak pertama dalam koleksi simpul pohon.

ForeColor

Mendapatkan atau mengatur warna latar depan simpul pohon.

FullPath

Mendapatkan jalur dari simpul pohon akar ke simpul pohon saat ini.

Handle

Mendapatkan handel simpul pohon.

ImageIndex

Mendapatkan atau mengatur nilai indeks daftar gambar gambar yang ditampilkan saat simpul pohon dalam status tidak dipilih.

ImageKey

Mendapatkan atau mengatur kunci untuk gambar yang terkait dengan simpul pohon ini ketika simpul dalam keadaan tidak dipilih.

Index

Mendapatkan posisi simpul pohon dalam koleksi simpul pohon.

IsEditing

Mendapatkan nilai yang menunjukkan apakah simpul pohon dalam status dapat diedit.

IsExpanded

Mendapatkan nilai yang menunjukkan apakah simpul pohon dalam status diperluas.

IsSelected

Mendapatkan nilai yang menunjukkan apakah simpul pohon berada dalam status yang dipilih.

IsVisible

Mendapatkan nilai yang menunjukkan apakah simpul pohon terlihat atau sebagian terlihat.

LastNode

Mendapatkan simpul pohon anak terakhir.

Level

Mendapatkan kedalaman simpul pohon berbasis nol dalam TreeView kontrol.

Name

Mendapatkan atau mengatur nama simpul pohon.

NextNode

Mendapatkan simpul pohon saudara berikutnya.

NextVisibleNode

Mendapatkan simpul pohon yang terlihat berikutnya.

NodeFont

Mendapatkan atau mengatur font yang digunakan untuk menampilkan teks pada label simpul pohon.

Nodes

Mendapatkan kumpulan TreeNode objek yang ditetapkan ke simpul pohon saat ini.

Parent

Mendapatkan simpul pohon induk dari simpul pohon saat ini.

PrevNode

Mendapatkan simpul pohon saudara sebelumnya.

PrevVisibleNode

Mendapatkan simpul pohon yang terlihat sebelumnya.

SelectedImageIndex

Mendapatkan atau mengatur nilai indeks daftar gambar gambar yang ditampilkan saat simpul pohon berada dalam status yang dipilih.

SelectedImageKey

Mendapatkan atau mengatur kunci gambar yang ditampilkan di simpul pohon saat berada dalam status yang dipilih.

StateImageIndex

Mendapatkan atau mengatur indeks gambar yang digunakan untuk menunjukkan status TreeNode kapan induk TreeView mengatur propertinya CheckBoxes ke false.

StateImageKey

Mendapatkan atau mengatur kunci gambar yang digunakan untuk menunjukkan status TreeNode kapan induk TreeView mengatur propertinya CheckBoxes ke false.

Tag

Mendapatkan atau mengatur objek yang berisi data tentang simpul pohon.

Text

Mendapatkan atau mengatur teks yang ditampilkan di label simpul pohon.

ToolTipText

Mendapatkan atau mengatur teks yang muncul saat penunjuk mouse mengarah ke atas TreeNode.

TreeView

Mendapatkan tampilan pohon induk tempat simpul pohon ditetapkan.

Metode

Nama Deskripsi
BeginEdit()

Memulai pengeditan label simpul pohon.

Clone()

Menyalin simpul pohon dan seluruh subtree yang berakar pada simpul pohon ini.

Collapse()

Menciutkan simpul pohon.

Collapse(Boolean)

Menciutkan TreeNode dan secara opsional menciutkan anak-anaknya.

CreateObjRef(Type)

Membuat objek yang berisi semua informasi relevan yang diperlukan untuk menghasilkan proksi yang digunakan untuk berkomunikasi dengan objek jarak jauh.

(Diperoleh dari MarshalByRefObject)
Deserialize(SerializationInfo, StreamingContext)

Memuat status TreeNode dari .SerializationInfo

EndEdit(Boolean)

Mengakhiri pengeditan label simpul pohon.

EnsureVisible()

Memastikan bahwa simpul pohon terlihat, memperluas simpul pohon dan menggulir kontrol tampilan pohon seperlunya.

Equals(Object)

Menentukan apakah objek yang ditentukan sama dengan objek saat ini.

(Diperoleh dari Object)
Expand()

Memperluas simpul pohon.

ExpandAll()

Memperluas semua simpul pohon anak.

FromHandle(TreeView, IntPtr)

Mengembalikan simpul pohon dengan handel yang ditentukan dan ditetapkan ke kontrol tampilan pohon yang ditentukan.

GetHashCode()

Berfungsi sebagai fungsi hash default.

(Diperoleh dari Object)
GetLifetimeService()
Kedaluwarsa.

Mengambil objek layanan seumur hidup saat ini yang mengontrol kebijakan seumur hidup untuk instans ini.

(Diperoleh dari MarshalByRefObject)
GetNodeCount(Boolean)

Mengembalikan jumlah simpul pohon anak.

GetType()

Mendapatkan Type instans saat ini.

(Diperoleh dari Object)
InitializeLifetimeService()
Kedaluwarsa.

Mendapatkan objek layanan seumur hidup untuk mengontrol kebijakan seumur hidup untuk instans ini.

(Diperoleh dari MarshalByRefObject)
MemberwiseClone()

Membuat salinan dangkal dari Objectsaat ini.

(Diperoleh dari Object)
MemberwiseClone(Boolean)

Membuat salinan dangkal objek saat ini MarshalByRefObject .

(Diperoleh dari MarshalByRefObject)
Remove()

Menghapus simpul pohon saat ini dari kontrol tampilan pohon.

Serialize(SerializationInfo, StreamingContext)

Menyimpan status TreeNode ke yang ditentukan SerializationInfo.

Toggle()

Mengalihkan simpul pohon ke status diperluas atau diciutkan.

ToString()

Mengembalikan string yang mewakili objek saat ini.

Implementasi Antarmuka Eksplisit

Nama Deskripsi
ISerializable.GetObjectData(SerializationInfo, StreamingContext)

Mengisi objek informasi serialisasi dengan data yang diperlukan untuk menserialisasikan TreeNode.

Berlaku untuk

Lihat juga