Top 45 C Treeview Top 105 Best Answers

You are looking for information, articles, knowledge about the topic nail salons open on sunday near me c treeview on Google, you do not find the information you need! Here are the best content compiled and compiled by the https://chewathai27.com/to team, along with other related topics such as: c treeview TreeView C#, Treeview Tkinter, Load dữ liệu lên TreeView trong C#, Treeview bootstrap 4, Treeview là gì, Treeview jQuery, Treeview Python, TreeView checkbox c winforms


[Lập trình C# Winform] – Bài 13: TreeView | HowKteam
[Lập trình C# Winform] – Bài 13: TreeView | HowKteam


Cách dùng TreeView trong C# winforms – Freetuts

  • Article author: freetuts.net
  • Reviews from users: 3977 ⭐ Ratings
  • Top rated: 3.8 ⭐
  • Lowest rated: 1 ⭐
  • Summary of article content: Articles about Cách dùng TreeView trong C# winforms – Freetuts 1. Giới thiệu TreeView trong C# … TreeView là điều khiển dùng để hiển thị danh sách các đối tượng dưới dạng phân cấp. Đối tượng trong TreeView thường được gọi … …
  • Most searched keywords: Whether you are looking for Cách dùng TreeView trong C# winforms – Freetuts 1. Giới thiệu TreeView trong C# … TreeView là điều khiển dùng để hiển thị danh sách các đối tượng dưới dạng phân cấp. Đối tượng trong TreeView thường được gọi … Cách sử dụng TreeView trong C# Winforms, TreeView là một điều khiển khá quen thuộc trong Winforms, bạn có thể xem các thuộc tính của TreeView C# tại đây
  • Table of Contents:

1 Giới thiệu TreeView trong C#

2 Sử dụng TreeView trong C#

Kết luận

Cùng chuyên mục

Cách dùng TreeView trong C# winforms - Freetuts
Cách dùng TreeView trong C# winforms – Freetuts

Read More

TreeView Class (System.Windows.Forms) | Microsoft Docs

  • Article author: docs.microsoft.com
  • Reviews from users: 45133 ⭐ Ratings
  • Top rated: 4.4 ⭐
  • Lowest rated: 1 ⭐
  • Summary of article content: Articles about TreeView Class (System.Windows.Forms) | Microsoft Docs The following code example demonstrates the use of the TreeView control. C# Copy. // Populates a TreeView control with example nodes. private vo … …
  • Most searched keywords: Whether you are looking for TreeView Class (System.Windows.Forms) | Microsoft Docs The following code example demonstrates the use of the TreeView control. C# Copy. // Populates a TreeView control with example nodes. private vo … Displays a hierarchical collection of labeled items, each represented by a TreeNode.
  • Table of Contents:

Definition

Examples

Remarks

Constructors

Properties

Methods

Events

Explicit Interface Implementations

Applies to

See also

TreeView Class (System.Windows.Forms) | Microsoft Docs
TreeView Class (System.Windows.Forms) | Microsoft Docs

Read More

[C#] Hướng dẫn sử dụng Treeview Control – C# Căn Bản

  • Article author: csharpcanban.com
  • Reviews from users: 27544 ⭐ Ratings
  • Top rated: 4.1 ⭐
  • Lowest rated: 1 ⭐
  • Summary of article content: Articles about [C#] Hướng dẫn sử dụng Treeview Control – C# Căn Bản TreeView là một control trong Windows Form dùng để hiển thị hệ thống phân cấp của các node, hay có thể gọi là hiển thị theo dạng “cây”, vì nó có gốc và các … …
  • Most searched keywords: Whether you are looking for [C#] Hướng dẫn sử dụng Treeview Control – C# Căn Bản TreeView là một control trong Windows Form dùng để hiển thị hệ thống phân cấp của các node, hay có thể gọi là hiển thị theo dạng “cây”, vì nó có gốc và các …
  • Table of Contents:
[C#] Hướng dẫn sử dụng Treeview Control - C# Căn Bản
[C#] Hướng dẫn sử dụng Treeview Control – C# Căn Bản

Read More

TreeView trong lập trình C# Winform | How Kteam

  • Article author: howkteam.vn
  • Reviews from users: 9082 ⭐ Ratings
  • Top rated: 3.4 ⭐
  • Lowest rated: 1 ⭐
  • Summary of article content: Articles about TreeView trong lập trình C# Winform | How Kteam TreeView trong lập trình C# Winform. Lập trình Winform cơ bản. 5.0 (2 đánh giá). Tạo bởi HowKteam Cập nhật lần cuối 2 năm trước 30.884 lượt xem 7 bình luận. …
  • Most searched keywords: Whether you are looking for TreeView trong lập trình C# Winform | How Kteam TreeView trong lập trình C# Winform. Lập trình Winform cơ bản. 5.0 (2 đánh giá). Tạo bởi HowKteam Cập nhật lần cuối 2 năm trước 30.884 lượt xem 7 bình luận. TreeView trong lập trình C# WinformTreeView trong lập trình C# Winform
  • Table of Contents:

Lập trình Winform cơ bản

TreeView trong lập trình C# Winform

TreeView trong lập trình C# Winform

Dẫn nhập

Nội dung

Kết luận

Thảo luận

TreeView trong lập trình C# Winform | How Kteam
TreeView trong lập trình C# Winform | How Kteam

Read More

TreeView Control in C#

  • Article author: www.c-sharpcorner.com
  • Reviews from users: 14732 ⭐ Ratings
  • Top rated: 3.6 ⭐
  • Lowest rated: 1 ⭐
  • Summary of article content: Articles about TreeView Control in C# This process relies on two separate methods. The first method creates an instance of a tree node collection containing all of the nodes in the … …
  • Most searched keywords: Whether you are looking for TreeView Control in C# This process relies on two separate methods. The first method creates an instance of a tree node collection containing all of the nodes in the … This is an article addressing some of the basics of working with a TreeView in C#; the article will address dynamically adding TreeNodes to a TreeView control, searching the nodes to find and highlight a single node or a collection of nodes.
  • Table of Contents:

TreeView Introduction

TreeView Application

Summary

TreeView Control in C#
TreeView Control in C#

Read More

Bài tập C# – Bài 8 – Cách dùng TreeView trong C# winforms

  • Article author: www.plpsoft.vn
  • Reviews from users: 1585 ⭐ Ratings
  • Top rated: 3.3 ⭐
  • Lowest rated: 1 ⭐
  • Summary of article content: Articles about Bài tập C# – Bài 8 – Cách dùng TreeView trong C# winforms Đối tượng trong TreeView thường được gọi là Node và cấu trúc phân cấp của TreeView được biểu diễn bởi lớp TreeNode. Mỗi một Node trong TreeView có thể chứa các … …
  • Most searched keywords: Whether you are looking for Bài tập C# – Bài 8 – Cách dùng TreeView trong C# winforms Đối tượng trong TreeView thường được gọi là Node và cấu trúc phân cấp của TreeView được biểu diễn bởi lớp TreeNode. Mỗi một Node trong TreeView có thể chứa các … Công ty phần mềm, thiết kế website, bảo mật mạng máy tính, lắp đặt camera, mua bán laptop, dịch vụ tư vấn, thiết kế, thi công liên quan đến thiết bị tin học và thiết bị an ninh.Phần mềm, phần mềm quản lý, phần mềm bán hàng, phần mềm tính tiền, sản xuất các sản phẩm phần mềm, thiết kế website, thiết kế, thi công lắp đặt camera, điện mặt trời, phần mềm quản lý Karaoke, SPA, quán ăn, cafe, nhà hàng, khách sạn, hotel, vật tư xây dựng, điện gia dụng, vật tư nông nghiệp, phòng khám đông y, phòng khám bệnh, quầy thuốc tây, plpsoft, plpsoft.vn, bảo mật mạng máy tính, bảo mật, network security, computer network security servives, CNSS
  • Table of Contents:

BÀI TẬP THỰC HÀNH C#

Bài 8 Cách dùng TreeView trong C# winforms

Bài tập C# - Bài 8 -  Cách dùng TreeView trong C# winforms
Bài tập C# – Bài 8 – Cách dùng TreeView trong C# winforms

Read More

C# Treeview Control

  • Article author: csharp.net-informations.com
  • Reviews from users: 38514 ⭐ Ratings
  • Top rated: 4.2 ⭐
  • Lowest rated: 1 ⭐
  • Summary of article content: Articles about C# Treeview Control C# Treeview Control … The TreeView control contains a hierarchy of TreeViewItem controls. It proves a way to display information in a hierarchical structure … …
  • Most searched keywords: Whether you are looking for C# Treeview Control C# Treeview Control … The TreeView control contains a hierarchy of TreeViewItem controls. It proves a way to display information in a hierarchical structure … TreeView control provides a way to display information in a hierarchical structure by using collapsible nodes
  • Table of Contents:
C# Treeview Control
C# Treeview Control

Read More

Sử dụng control TreeView trong C# – Using TreeView control in C# | For Better Life!

  • Article author: thanhcuong.wordpress.com
  • Reviews from users: 34419 ⭐ Ratings
  • Top rated: 3.7 ⭐
  • Lowest rated: 1 ⭐
  • Summary of article content: Articles about
    Sử dụng control TreeView trong C# – Using TreeView control in C# | For Better Life! TreeView là một control trong Windows Form dùng để hiển thị hệ thông phân cấp của các node. Giống như các file và folder được hiển thị trong … …
  • Most searched keywords: Whether you are looking for
    Sử dụng control TreeView trong C# – Using TreeView control in C# | For Better Life! TreeView là một control trong Windows Form dùng để hiển thị hệ thông phân cấp của các node. Giống như các file và folder được hiển thị trong … TreeView là một control trong Windows Form dùng để hiển thị hệ thông phân cấp của các node. Giống như các file và folder được hiển thị trong cửa sổ phía bên phải của Windows Explorer. TreeView control hữu ích trong rất nhiều trường hợp, nó giúp việc hiển thị các dữ liệu một cách…
  • Table of Contents:

Sử dụng control TreeView trong C# – Using TreeView control in C#

About thanhcuong1990


Sử dụng control TreeView trong C# –  Using TreeView control in C# | For Better Life!
Sử dụng control TreeView trong C# – Using TreeView control in C# | For Better Life!

Read More

Thêm Node và ChildNode vào TreeView trong C#

  • Article author: code24h.com
  • Reviews from users: 43845 ⭐ Ratings
  • Top rated: 3.3 ⭐
  • Lowest rated: 1 ⭐
  • Summary of article content: Articles about Thêm Node và ChildNode vào TreeView trong C# Cấp 3: chứa thông tin thiết bị (thuộc node loại thiết bị). Giả sử, chương trình có các biến sau: TreeView tên trvDevice; ImageList tên imgList; Các ảnh “Root. …
  • Most searched keywords: Whether you are looking for Thêm Node và ChildNode vào TreeView trong C# Cấp 3: chứa thông tin thiết bị (thuộc node loại thiết bị). Giả sử, chương trình có các biến sau: TreeView tên trvDevice; ImageList tên imgList; Các ảnh “Root. them node ,va childnode ,vao treeview ,trong c ,thêm node ,và childnode ,vào treeview ,trong c ,Thêm Node và ChildNode vào TreeView trong C#Góc Kinh Nghiệm sẽ trình bày cách hiển thị một danh sách các thiết bị trên treeview, tương ứng với mỗi thiết bị thì nó thuộc từng loại thiết bị nhất định
    Treeview sẽ gồm 3 cấp:
    Cấp 1: chứa 1 node gốc duy nhất
    Cấp 2: chứa thông tin loại thiết bị (thuộc node gốc)
    Cấp 3: chứa thông tin thiết
  • Table of Contents:
Thêm Node và ChildNode vào TreeView trong C#
Thêm Node và ChildNode vào TreeView trong C#

Read More


See more articles in the same category here: https://chewathai27.com/to/blog.

Cách dùng TreeView trong C# winforms

Trong bài này chúng ta sẽ tìm hiểu cách sử dụng TreeView trong C# Winforms. Đây là một điều khiển khá quen thuộc, nó được sử dụng rất nhiều trong việc quản lý dữ liệu.

Bài viết này được đăng tại freetuts.net , không được copy dưới mọi hình thức.

Chúng ta sẽ cùng nhau tìm hiểu về công dụng cũng như các thuộc tính và phương thức, sự kiện của TreeView. Sau đó mình sẽ viết một chương trình áp dụng TreeView để xem sự khác nhau giữa TreeView và ListView nhé.

1. Giới thiệu TreeView trong C#

TreeView là điều khiển dùng để hiển thị danh sách các đối tượng dưới dạng phân cấp.

Đối tượng trong TreeView thường được gọi là Node và cấu trúc phân cấp của TreeView được biểu diễn bởi lớp TreeNode. Mỗi một Node trong TreeView có thể chứa các Node khác.

Bài viết này được đăng tại [free tuts .net]

Node chứa một Node khác gọi là Node cha (RootNode) và Node được chứa goi là Node con (ChildNode).

Việc sử dụng điều khiển TreeView để hiển thị rất hữu ích, vì trình bày theo dạng phân cấp giúp việc hiển thị được rõ ràng và có hệ thông hơn.

Thuộc tính, phương thức và sự kiện của TreeView

Một số thuộc tính thường dùng của TreeView:

Thuộc tính Mô tả Node Trả về một đối tượng thuộc lớp TreeNode SelectedNode Trả về Node đang được chọn trong TreeView ShowPlusMinus Hiển thị dấu + và – trước mỗi TreeNode ShowRootLines Hiển thị đường thằng nối giữa các RootNode trong một TreeView ImageList Hiển thị hình trước mỗi Node trong TreeView. *Lưu ý: phải sử dụng thêm điều khiển ImageList và gán tên đối tượng của điều khiển ImageList cho thuộc tính ImageList của TreeView ImageIndex Giá trị của thuộc tính ImageIndex là chỉ số của hình trong điều khiển ImageList. Khi gán chỉ số cho thuộc tính ImageIndex thì hình hiển thị trước mỗi Node sẽ là hình có chỉ số tương ứng. *Lưu ý: Phải sử dụng thuộc tính ImageList trước SelectedImageIndex Giá trị của thuộc tính SelectedImageIndex là chỉ số của hình trong điều khiển ImageList. Khi người dùng chọn Node nào thì Node đó sẽ có hình tương ứng như thuộc tính SelectedImageIndex chỉ định

Một số phương thức thường dùng của TreeView:

Phương thức Mô tả GetNodeCount() Đến số Node trong một TreeView ExpandAll() Hiển thị tất cả các Node trên TreeView CollapseAll() Thu gọn tất cả các Node trên TreeView GetNodeAt(x, y) Lấy một Node tại một vị trí có tọa độ (x, y) trên màn hình. *Lưu ý: Thương sử dụng sự kiện MouseDown hoặc NodeMouseClick

Một số sự kiện thường dùng của TreeView:

Sự kiện Mô tả AfterCollapse Phát sinh khi thu gọn một TreeNode AfterExpand Phát sinh khi hiển thị các Node trong TreeNode AfterSelect Phát sinh khi chọn một TreeNode NodeMouseClick Phát sinh khi chọn một Node

Thuộc tính và phương thức của TreeNode

Một số thuộc tính thương dùng của TreeNode:

Thuộc tính Mô tả Nodes Trả về tập các Node Text Đọc/gán chuỗi ký tự người dùng sẽ nhìn thấy ở mỗi Node FirstNode Trả về Node đầu tiên LastNode Trả về Node cuối cùng NextNode Chuyển đến Node tiếp theo PrevNode Lùi lại Node trước đó Parent Trả về Node cha của Node hiện tại Index Trả về chỉ số của Node

Một số phương thức thường dùng của TreeNode:

Phương thức Mô tả Nodes.Add Thêm một Node Nodes.Remove Xóa một Node Nodes.Insert Chèn vào một Node Nodes.Clear Xóa tất cả các Node con và Node hiện tại

2. Sử dụng TreeView trong C#

Trong ví dụ này mình sẽ thực hiện một chương trình theo giao diện Form như dưới đây, với một số chức năng.

Tạo hệ thống cây TreeView gồm:

Button ” Thêm Node gốc “: Thêm một Node gốc.

“: Thêm một Node gốc. Button ” Thêm Node con “: Thêm một Node con.

“: Thêm một Node con. Button ” Xóa tất cả các Node “: Hủy tất cả các Node trong TreeView.

“: Hủy tất cả các Node trong TreeView. Butotn ” Xóa Node được chọn “: Xóa Node được chọn khỏi TreeView.

“: Xóa Node được chọn khỏi TreeView. Button ” – “: Thu hẹp lại vị trí chọn – thu hẹp lại các Node là con của Node.

“: Thu hẹp lại vị trí chọn – thu hẹp lại các Node là con của Node. Button ” + “: Mở rộng tại vị trí chọn – mở rộng các Node là con của Node.

“: Mở rộng tại vị trí chọn – mở rộng các Node là con của Node. Button “Đếm tổng Node TreeView”: Đếm tổng Node có trên TreeView.

Việc đầu tiền chúng ta sẽ thiết kế giao diện tương tự như Form mẫu, bao gồm:

1 TextBox để nhập tiêu đề cho Node.

7 Button với các chức năng tương ứng.

1 TreeView để hiển thị danh sách các Node.

Sau khi tạo giao diện cho Form, ta bắt đầu thực hiện lần lượt các chức năng trên các nút Button.

Bước 1: xử lý sự kiện trên Button “Thêm Node gốc”, với chức năng khi người dùng Click vào nút thì nội dung trong ô TextBox sẽ được thêm vào Node gốc trong TreeView.

Ta sử dụng string.IsNullOrEmpty() để xét điều kiện nếu như trong ô TextBox rỗng mà người dùng nhấn vào Button thì thông báo cho người dùng biết.

Tiếp đến ta sử dụng string.Equals() để so sánh nội dung trong ô TextBox với các Node trong TreeView, nếu bằng nhau thì thông báo cho người dùng biết đã tồn tại Node trong TreeView.

Sau khi xét xong điều kiện, bây giờ ta chỉ cần sử dụng Node.Add() để thêm nội dung từ TextBox vào Node. Khi thêm xong ta sẽ xóa nội dung trong ô TextBox bằng phương thức Clear() .

private void btnThemnodegoc_Click(object sender, EventArgs e) { bool t = false; if (!string.IsNullOrEmpty(txtNode.Text)) { TreeNode Node = new TreeNode(); Node.Text = txtNode.Text; foreach(TreeNode nodex in treeView1.Nodes) { if(string.Equals(Node.Text,nodex.Text)) { MessageBox.Show(“Node đã tồn tại”); t = true; } } if (t == false) treeView1.Nodes.Add(Node); txtNode.Clear(); txtNode.Focus(); } else MessageBox.Show(“Node không được để trống”); }

Kết quả: Sau khi thêm các Node gốc và các Node con.

Bước 2: Xử lý sự kiện trên Button “Thêm Node con”, với chức năng khi người dùng nhâp chuột vào Button thì nội dung trong ô TextBox sẽ được thêm nào Node con của Node được chọn trong TreeView.

Tương tự như Button “Thêm Node gốc”, ta cũng sử dụng string.IsNullOrEmpty() để thêm điều kiện cho nút. Sau đó sử dụng SelectedNode.Nodes.Add() để thêm Node con vào Node được chọn.

private void btnThemnodecon_Click(object sender, EventArgs e) { if (!string.IsNullOrEmpty(txtNode.Text)) { if (treeView1.SelectedNode != null) { TreeNode Subnode = new TreeNode(); Subnode.Text = txtNode.Text; treeView1.SelectedNode.Nodes.Add(Subnode); txtNode.Clear(); txtNode.Focus(); } else MessageBox.Show(“Bạn chưa chọn vị trí tạo Node con”); } else MessageBox.Show(“Node không được để trống”); }

Bước 3: Xử lý sự kiện trên Button “Xóa tất cả các Node”, với chức năng khi người dùng nhấn vào thì tất cả các Node sẽ được xóa.

Đơn giản ta chỉ cần sử dụng phương thức Clear() có trong Nodes để xóa tất cả các Node.

private void btnXoaallNode_Click(object sender, EventArgs e) { treeView1.Nodes.Clear(); }

Bước 4: Xử lý sự kiện trên Button “Xóa Node được chọn”, với chức năng xóa Node mà người dùng chọn khỏi TreeView.

Ta sẽ sử dụng SelectedNode.Remove() để xóa Node đang được chọn khỏi TreeView.

private void btnXoaNodechon_Click(object sender, EventArgs e) { if(treeView1.SelectedNode !=null) treeView1.SelectedNode.Remove(); }

Bước 5: Xử lý sự kiện trên hai Button “-” và “+”, với chức năng thu hẹp và mở rộng các Node trong TreeView.

Trong TreeView có hai sự kiện hổ trợ cho việc này đó chính là ExpandAll() và CollapseAll() .

private void btnShow_Click(object sender, EventArgs e) { treeView1.ExpandAll(); } private void btnAn_Click(object sender, EventArgs e) { treeView1.CollapseAll(); }

Bước 6: Xử lý sự kiện trên Button “Đếm tổng Node TreeView”, với chức năng đếm tất cả các Node có trong TreeView.

Ta sẽ sử dụng GetNodeCount() để đếm tổng số Node trong TreeView() .

private void btnTong_Click(object sender, EventArgs e) { int n = treeView1.GetNodeCount(true); MessageBox.Show(“Tổng số Node của TreeView : “+n); }

Kết quả: khi nhấn vào Button “Đến tổng Node TreeView”.

Full Code:

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace cau4 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnThemnodegoc_Click(object sender, EventArgs e) { bool t = false; if (!string.IsNullOrEmpty(txtNode.Text)) { TreeNode Node = new TreeNode(); Node.Text = txtNode.Text; foreach(TreeNode nodex in treeView1.Nodes) { if(string.Equals(Node.Text,nodex.Text)) { MessageBox.Show(“Node đã tồn tại”); t = true; } } if (t == false) treeView1.Nodes.Add(Node); txtNode.Clear(); txtNode.Focus(); } else MessageBox.Show(“Node không được để trống”); } private void btnThemnodecon_Click(object sender, EventArgs e) { if (!string.IsNullOrEmpty(txtNode.Text)) { if (treeView1.SelectedNode != null) { TreeNode Subnode = new TreeNode(); Subnode.Text = txtNode.Text; treeView1.SelectedNode.Nodes.Add(Subnode); txtNode.Clear(); txtNode.Focus(); } else MessageBox.Show(“Bạn chưa chọn vị trí tạo Node con”); } else MessageBox.Show(“Node không được để trống”); } private void btnShow_Click(object sender, EventArgs e) { treeView1.ExpandAll(); } private void btnAn_Click(object sender, EventArgs e) { treeView1.CollapseAll(); } private void btnXoaallNode_Click(object sender, EventArgs e) { treeView1.Nodes.Clear(); } private void btnXoaNodechon_Click(object sender, EventArgs e) { if(treeView1.SelectedNode !=null) treeView1.SelectedNode.Remove(); } private void btnTong_Click(object sender, EventArgs e) { int n = treeView1.GetNodeCount(true); MessageBox.Show(“Tổng số Node của TreeView : “+n); } private void Form1_Load(object sender, EventArgs e) { } } }

Kết luận

Như vậy là chúng ta đã cùng nhau tìm hiểu về điều khiển TreeView trong C#, cũng như các thuộc tính, sự kiên và phương thức của nó. Đây là một điều khiển được sử dụng rất nhiều, vì vậy các bạn hãy luyện tập thật nhiều để thành thạo nó nhé. Ở bài tiếp theo mình sẽ hướng dẫn các bạn các điều khiển khác trong winforms, các bạn hãy chú ý theo dõi nhé !!!

TreeView Class (System.Windows.Forms)

Table of contents

Tree View Class

Reference

Definition

Important Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.

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

In this article

public ref class TreeView : System::Windows::Forms::Control

public class TreeView : System.Windows.Forms.Control

[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.AutoDispatch)] [System.Runtime.InteropServices.ComVisible(true)] [System.Windows.Forms.Docking(System.Windows.Forms.DockingBehavior.Ask)] public class TreeView : System.Windows.Forms.Control

[System.Windows.Forms.Docking(System.Windows.Forms.DockingBehavior.Ask)] public class TreeView : System.Windows.Forms.Control

type TreeView = class inherit Control

[] [] [] type TreeView = class inherit Control

[] type TreeView = class inherit Control

Public Class TreeView Inherits Control

Inheritance Object MarshalByRefObject Component Control TreeView

Attributes ClassInterfaceAttribute ComVisibleAttribute DockingAttribute

Examples

The following code example demonstrates the use of the TreeView control.

// Populates a TreeView control with example nodes. private void InitializeTreeView() { treeView1.BeginUpdate(); treeView1.Nodes.Add(“Parent”); treeView1.Nodes[0].Nodes.Add(“Child 1”); treeView1.Nodes[0].Nodes.Add(“Child 2”); treeView1.Nodes[0].Nodes[1].Nodes.Add(“Grandchild”); treeView1.Nodes[0].Nodes[1].Nodes[0].Nodes.Add(“Great Grandchild”); treeView1.EndUpdate(); }

‘ Populates a TreeView control with example nodes. Private Sub InitializeTreeView() treeView1.BeginUpdate() treeView1.Nodes.Add(“Parent”) treeView1.Nodes(0).Nodes.Add(“Child 1”) treeView1.Nodes(0).Nodes.Add(“Child 2”) treeView1.Nodes(0).Nodes(1).Nodes.Add(“Grandchild”) treeView1.Nodes(0).Nodes(1).Nodes(0).Nodes.Add(“Great Grandchild”) treeView1.EndUpdate() End Sub

The following, more complex code 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 requires that you have a Customer object that can hold a collection of Order objects. It also requires that you have a cursor file that is named MyWait.cur in the application directory and that you have created an instance of a TreeView control on a 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(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(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(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 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 to the ImageList property and referencing the index value of an Image in the ImageList to assign that Image. Use the following properties to assign images: Set the ImageIndex property to the index value of the Image that you want to display when a tree node is not selected. 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. Individual tree nodes can override the default images by setting the TreeNode.ImageIndex and TreeNode.SelectedImageIndex properties. The state images displayed in the TreeView are 16 x 16 pixels by default. Setting the ImageSize property of the StateImageList will have no effect on how the images are displayed. However, the state images are resized according to the system DPI setting when the app.config file contains the following entry:

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 that 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 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. You can also completely customize the appearance of the TreeView control. To do this, set the DrawMode property to a value other than TreeViewDrawMode.Normal and handle the DrawNode event.

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, except for the selected TreeNode.

Constructors

TreeView() Initializes a new instance of the TreeView class.

Properties

AccessibilityObject Gets the AccessibleObject assigned to the control. (Inherited from Control) AccessibleDefaultActionDescription Gets or sets the default action description of the control for use by accessibility client applications. (Inherited from Control) AccessibleDescription Gets or sets the description of the control used by accessibility client applications. (Inherited from Control) AccessibleName Gets or sets the name of the control used by accessibility client applications. (Inherited from Control) AccessibleRole Gets or sets the accessible role of the control. (Inherited from Control) AllowDrop Gets or sets a value indicating whether the control can accept data that the user drags onto it. (Inherited from Control) Anchor Gets or sets the edges of the container to which a control is bound and determines how a control is resized with its parent. (Inherited from Control) AutoScrollOffset Gets or sets where this control is scrolled to in ScrollControlIntoView(Control). (Inherited from Control) AutoSize This property is not relevant for this class. (Inherited from Control) BackColor Gets or sets the background color for the control. BackgroundImage Gets or set the background image for the TreeView control. BackgroundImageLayout Gets or sets the layout of the background image for the TreeView control. BackgroundImageLayout Gets or sets the background image layout as defined in the ImageLayout enumeration. (Inherited from Control) BindingContext Gets or sets the BindingContext for the control. (Inherited from Control) BorderStyle Gets or sets the border style of the tree view control. Bottom Gets the distance, in pixels, between the top edge of the control and the top edge of its container’s client area. (Inherited from Control) Bounds Gets or sets the size and location of the control including its nonclient elements, in pixels, relative to the parent control. (Inherited from Control) CanEnableIme Gets a value indicating whether the ImeMode property can be set to an active value, to enable IME support. (Inherited from Control) CanFocus Gets a value indicating whether the control can receive focus. (Inherited from Control) CanRaiseEvents Determines if events can be raised on the control. (Inherited from Control) CanSelect Gets a value indicating whether the control can be selected. (Inherited from Control) Capture Gets or sets a value indicating whether the control has captured the mouse. (Inherited from Control) CausesValidation Gets or sets a value indicating whether the control causes validation to be performed on any controls that require validation when it receives focus. (Inherited from Control) CheckBoxes Gets or sets a value indicating whether check boxes are displayed next to the tree nodes in the tree view control. ClientRectangle Gets the rectangle that represents the client area of the control. (Inherited from Control) ClientSize Gets or sets the height and width of the client area of the control. (Inherited from Control) CompanyName Gets the name of the company or creator of the application containing the control. (Inherited from Control) Container Gets the IContainer that contains the Component. (Inherited from Component) ContainsFocus Gets a value indicating whether the control, or one of its child controls, currently has the input focus. (Inherited from Control) ContextMenu Gets or sets the shortcut menu associated with the control. (Inherited from Control) ContextMenuStrip Gets or sets the ContextMenuStrip associated with this control. (Inherited from Control) Controls Gets the collection of controls contained within the control. (Inherited from Control) Created Gets a value indicating whether the control has been created. (Inherited from Control) CreateParams Gets the required creation parameters when the control handle is created. Cursor Gets or sets the cursor that is displayed when the mouse pointer is over the control. (Inherited from Control) DataBindings Gets the data bindings for the control. (Inherited from Control) DefaultCursor Gets or sets the default cursor for the control. (Inherited from Control) DefaultImeMode Gets the default Input Method Editor (IME) mode supported by the control. (Inherited from Control) DefaultMargin Gets the space, in pixels, that is specified by default between controls. (Inherited from Control) DefaultMaximumSize Gets the length and height, in pixels, that is specified as the default maximum size of a control. (Inherited from Control) DefaultMinimumSize Gets the length and height, in pixels, that is specified as the default minimum size of a control. (Inherited from Control) DefaultPadding Gets the internal spacing, in pixels, of the contents of a control. (Inherited from Control) DefaultSize Gets the default size of the control. DesignMode Gets a value that indicates whether the Component is currently in design mode. (Inherited from Component) DeviceDpi Gets the DPI value for the display device where the control is currently being displayed. (Inherited from Control) DisplayRectangle Gets the rectangle that represents the display area of the control. (Inherited from Control) Disposing Gets a value indicating whether the base Control class is in the process of disposing. (Inherited from Control) Dock Gets or sets which control borders are docked to its parent control and determines how a control is resized with its parent. (Inherited from Control) DoubleBuffered Gets or sets a value indicating whether the control should redraw its surface using a secondary buffer. The DoubleBuffered property does not affect the TreeView control. DoubleBuffered Gets or sets a value indicating whether this control should redraw its surface using a secondary buffer to reduce or prevent flicker. (Inherited from Control) DrawMode Gets or sets the mode in which the control is drawn. Enabled Gets or sets a value indicating whether the control can respond to user interaction. (Inherited from Control) Events Gets the list of event handlers that are attached to this Component. (Inherited from Component) Focused Gets a value indicating whether the control has input focus. (Inherited from Control) Font Gets or sets the font of the text displayed by the control. (Inherited from Control) FontHeight Gets or sets the height of the font of the control. (Inherited from Control) ForeColor Gets or sets the foreground color of the control. FullRowSelect Gets or sets a value indicating whether the selection highlight spans the width of the tree view control. Handle Gets the window handle that the control is bound to. (Inherited from Control) HasChildren Gets a value indicating whether the control contains one or more child controls. (Inherited from Control) Height Gets or sets the height of the control. (Inherited from Control) HideSelection Gets or sets a value indicating whether the selected tree node remains highlighted even when the tree view has lost the focus. HotTracking Gets or sets a value indicating whether a tree node label takes on the appearance of a hyperlink as the mouse pointer passes over it. ImageIndex Gets or sets the image-list index value of the default image that is displayed by the tree nodes. ImageKey Gets or sets the key of the default image for each node in the TreeView control when it is in an unselected state. ImageList Gets or sets the ImageList that contains the Image objects that are used by the tree nodes. ImeMode Gets or sets the Input Method Editor (IME) mode of the control. (Inherited from Control) ImeModeBase Gets or sets the IME mode of a control. (Inherited from Control) Indent Gets or sets the distance to indent each child tree node level. InvokeRequired Gets a value indicating whether the caller must call an invoke method when making method calls to the control because the caller is on a different thread than the one the control was created on. (Inherited from Control) IsAccessible Gets or sets a value indicating whether the control is visible to accessibility applications. (Inherited from Control) IsAncestorSiteInDesignMode Indicates if one of the Ancestors of this control is sited and that site in DesignMode. This property is read-only. (Inherited from Control) IsDisposed Gets a value indicating whether the control has been disposed of. (Inherited from Control) IsHandleCreated Gets a value indicating whether the control has a handle associated with it. (Inherited from Control) IsMirrored Gets a value indicating whether the control is mirrored. (Inherited from Control) ItemHeight Gets or sets the height of each tree node in the tree view control. LabelEdit Gets or sets a value indicating whether the label text of the tree nodes can be edited. LayoutEngine Gets a cached instance of the control’s layout engine. (Inherited from Control) Left Gets or sets the distance, in pixels, between the left edge of the control and the left edge of its container’s client area. (Inherited from Control) LineColor Gets or sets the color of the lines connecting the nodes of the TreeView control. Location Gets or sets the coordinates of the upper-left corner of the control relative to the upper-left corner of its container. (Inherited from Control) Margin Gets or sets the space between controls. (Inherited from Control) MaximumSize Gets or sets the size that is the upper limit that GetPreferredSize(Size) can specify. (Inherited from Control) MinimumSize Gets or sets the size that is the lower limit that GetPreferredSize(Size) can specify. (Inherited from Control) Name Gets or sets the name of the control. (Inherited from Control) Nodes Gets the collection of tree nodes that are assigned to the tree view control. Padding Gets or sets the spacing between the TreeView control’s contents and its edges. Padding Gets or sets padding within the control. (Inherited from Control) Parent Gets or sets the parent container of the control. (Inherited from Control) PathSeparator Gets or sets the delimiter string that the tree node path uses. PreferredSize Gets the size of a rectangular area into which the control can fit. (Inherited from Control) ProductName Gets the product name of the assembly containing the control. (Inherited from Control) ProductVersion Gets the version of the assembly containing the control. (Inherited from Control) RecreatingHandle Gets a value indicating whether the control is currently re-creating its handle. (Inherited from Control) Region Gets or sets the window region associated with the control. (Inherited from Control) RenderRightToLeft Obsolete. Obsolete. This property is now obsolete. (Inherited from Control) ResizeRedraw Gets or sets a value indicating whether the control redraws itself when resized. (Inherited from Control) Right Gets the distance, in pixels, between the right edge of the control and the left edge of its container’s client area. (Inherited from Control) RightToLeft Gets or sets a value indicating whether control’s elements are aligned to support locales using right-to-left fonts. (Inherited from Control) RightToLeftLayout Gets or sets a value that indicates whether the TreeView should be laid out from right-to-left. ScaleChildren Gets a value that determines the scaling of child controls. (Inherited from Control) Scrollable Gets or sets a value indicating whether the tree view control displays scroll bars when they are needed. SelectedImageIndex Gets or sets the image list index value of the image that is displayed when a tree node is selected. SelectedImageKey Gets or sets the key of the default image shown when a TreeNode is in a selected state. SelectedNode Gets or sets the tree node that is currently selected in the tree view control. ShowFocusCues Gets a value indicating whether the control should display focus rectangles. (Inherited from Control) ShowKeyboardCues Gets a value indicating whether the user interface is in the appropriate state to show or hide keyboard accelerators. (Inherited from Control) ShowLines Gets or sets a value indicating whether lines are drawn between tree nodes in the tree view control. ShowNodeToolTips Gets or sets a value indicating ToolTips are shown when the mouse pointer hovers over a TreeNode. ShowPlusMinus Gets or sets a value indicating whether plus-sign (+) and minus-sign (-) buttons are displayed next to tree nodes that contain child tree nodes. ShowRootLines Gets or sets a value indicating whether lines are drawn between the tree nodes that are at the root of the tree view. Site Gets or sets the site of the control. (Inherited from Control) Size Gets or sets the height and width of the control. (Inherited from Control) Sorted Gets or sets a value indicating whether the tree nodes in the tree view are sorted. StateImageList Gets or sets the image list that is used to indicate the state of the TreeView and its nodes. TabIndex Gets or sets the tab order of the control within its container. (Inherited from Control) TabStop Gets or sets a value indicating whether the user can give the focus to this control using the TAB key. (Inherited from Control) Tag Gets or sets the object that contains data about the control. (Inherited from Control) Text Gets or sets the text of the TreeView. Top Gets or sets the distance, in pixels, between the top edge of the control and the top edge of its container’s client area. (Inherited from Control) TopLevelControl Gets the parent control that is not parented by another Windows Forms control. Typically, this is the outermost Form that the control is contained in. (Inherited from Control) TopNode Gets or sets the first fully-visible tree node in the tree view control. TreeViewNodeSorter Gets or sets the implementation of IComparer to perform a custom sort of the TreeView nodes. UseWaitCursor Gets or sets a value indicating whether to use the wait cursor for the current control and all child controls. (Inherited from Control) Visible Gets or sets a value indicating whether the control and all its child controls are displayed. (Inherited from Control) VisibleCount Gets the number of tree nodes that can be fully visible in the tree view control. Width Gets or sets the width of the control. (Inherited from Control) WindowTarget This property is not relevant for this class. (Inherited from Control)

Methods

Events

AfterCheck Occurs after the tree node check box is checked. AfterCollapse Occurs after the tree node is collapsed. AfterExpand Occurs after the tree node is expanded. AfterLabelEdit Occurs after the tree node label text is edited. AfterSelect Occurs after the tree node is selected. AutoSizeChanged This event is not relevant for this class. (Inherited from Control) BackColorChanged Occurs when the value of the BackColor property changes. (Inherited from Control) BackgroundImageChanged Occurs when the BackgroundImage property changes. BackgroundImageLayoutChanged Occurs when the BackgroundImageLayout property changes. BackgroundImageLayoutChanged Occurs when the BackgroundImageLayout property changes. (Inherited from Control) BeforeCheck Occurs before the tree node check box is checked. BeforeCollapse Occurs before the tree node is collapsed. BeforeExpand Occurs before the tree node is expanded. BeforeLabelEdit Occurs before the tree node label text is edited. BeforeSelect Occurs before the tree node is selected. BindingContextChanged Occurs when the value of the BindingContext property changes. (Inherited from Control) CausesValidationChanged Occurs when the value of the CausesValidation property changes. (Inherited from Control) ChangeUICues Occurs when the focus or keyboard user interface (UI) cues change. (Inherited from Control) Click Occurs when the control is clicked. (Inherited from Control) ClientSizeChanged Occurs when the value of the ClientSize property changes. (Inherited from Control) ContextMenuChanged Occurs when the value of the ContextMenu property changes. (Inherited from Control) ContextMenuStripChanged Occurs when the value of the ContextMenuStrip property changes. (Inherited from Control) ControlAdded Occurs when a new control is added to the Control.ControlCollection. (Inherited from Control) ControlRemoved Occurs when a control is removed from the Control.ControlCollection. (Inherited from Control) CursorChanged Occurs when the value of the Cursor property changes. (Inherited from Control) Disposed Occurs when the component is disposed by a call to the Dispose() method. (Inherited from Component) DockChanged Occurs when the value of the Dock property changes. (Inherited from Control) DoubleClick Occurs when the control is double-clicked. (Inherited from Control) DpiChangedAfterParent Occurs when the DPI setting for a control is changed programmatically after the DPI of its parent control or form has changed. (Inherited from Control) DpiChangedBeforeParent Occurs when the DPI setting for a control is changed programmatically before a DPI change event for its parent control or form has occurred. (Inherited from Control) DragDrop Occurs when a drag-and-drop operation is completed. (Inherited from Control) DragEnter Occurs when an object is dragged into the control’s bounds. (Inherited from Control) DragLeave Occurs when an object is dragged out of the control’s bounds. (Inherited from Control) DragOver Occurs when an object is dragged over the control’s bounds. (Inherited from Control) DrawNode Occurs when a TreeView is drawn and the DrawMode property is set to a TreeViewDrawMode value other than Normal. EnabledChanged Occurs when the Enabled property value has changed. (Inherited from Control) Enter Occurs when the control is entered. (Inherited from Control) FontChanged Occurs when the Font property value changes. (Inherited from Control) ForeColorChanged Occurs when the ForeColor property value changes. (Inherited from Control) GiveFeedback Occurs during a drag operation. (Inherited from Control) GotFocus Occurs when the control receives focus. (Inherited from Control) HandleCreated Occurs when a handle is created for the control. (Inherited from Control) HandleDestroyed Occurs when the control’s handle is in the process of being destroyed. (Inherited from Control) HelpRequested Occurs when the user requests help for a control. (Inherited from Control) ImeModeChanged Occurs when the ImeMode property has changed. (Inherited from Control) Invalidated Occurs when a control’s display requires redrawing. (Inherited from Control) ItemDrag Occurs when the user begins dragging a node. KeyDown Occurs when a key is pressed while the control has focus. (Inherited from Control) KeyPress Occurs when a character. space or backspace key is pressed while the control has focus. (Inherited from Control) KeyUp Occurs when a key is released while the control has focus. (Inherited from Control) Layout Occurs when a control should reposition its child controls. (Inherited from Control) Leave Occurs when the input focus leaves the control. (Inherited from Control) LocationChanged Occurs when the Location property value has changed. (Inherited from Control) LostFocus Occurs when the control loses focus. (Inherited from Control) MarginChanged Occurs when the control’s margin changes. (Inherited from Control) MouseCaptureChanged Occurs when the control loses mouse capture. (Inherited from Control) MouseClick Occurs when the control is clicked by the mouse. (Inherited from Control) MouseDoubleClick Occurs when the control is double clicked by the mouse. (Inherited from Control) MouseDown Occurs when the mouse pointer is over the control and a mouse button is pressed. (Inherited from Control) MouseEnter Occurs when the mouse pointer enters the control. (Inherited from Control) MouseHover Occurs when the mouse pointer rests on the control. (Inherited from Control) MouseLeave Occurs when the mouse pointer leaves the control. (Inherited from Control) MouseMove Occurs when the mouse pointer is moved over the control. (Inherited from Control) MouseUp Occurs when the mouse pointer is over the control and a mouse button is released. (Inherited from Control) MouseWheel Occurs when the mouse wheel moves while the control has focus. (Inherited from Control) Move Occurs when the control is moved. (Inherited from Control) NodeMouseClick Occurs when the user clicks a TreeNode with the mouse. NodeMouseDoubleClick Occurs when the user double-clicks a TreeNode with the mouse. NodeMouseHover Occurs when the mouse hovers over a TreeNode. PaddingChanged Occurs when the value of the Padding property changes. PaddingChanged Occurs when the control’s padding changes. (Inherited from Control) Paint Occurs when the TreeView is drawn. ParentChanged Occurs when the Parent property value changes. (Inherited from Control) PreviewKeyDown Occurs before the KeyDown event when a key is pressed while focus is on this control. (Inherited from Control) QueryAccessibilityHelp Occurs when AccessibleObject is providing help to accessibility applications. (Inherited from Control) QueryContinueDrag Occurs during a drag-and-drop operation and enables the drag source to determine whether the drag-and-drop operation should be canceled. (Inherited from Control) RegionChanged Occurs when the value of the Region property changes. (Inherited from Control) Resize Occurs when the control is resized. (Inherited from Control) RightToLeftChanged Occurs when the RightToLeft property value changes. (Inherited from Control) RightToLeftLayoutChanged Occurs when the value of the RightToLeftLayout property changes. SizeChanged Occurs when the Size property value changes. (Inherited from Control) StyleChanged Occurs when the control style changes. (Inherited from Control) SystemColorsChanged Occurs when the system colors change. (Inherited from Control) TabIndexChanged Occurs when the TabIndex property value changes. (Inherited from Control) TabStopChanged Occurs when the TabStop property value changes. (Inherited from Control) TextChanged Occurs when the Text property changes. Validated Occurs when the control is finished validating. (Inherited from Control) Validating Occurs when the control is validating. (Inherited from Control) VisibleChanged Occurs when the Visible property value changes. (Inherited from Control)

Explicit Interface Implementations

IDropTarget.OnDragDrop(DragEventArgs) Raises the DragDrop event. (Inherited from Control) IDropTarget.OnDragEnter(DragEventArgs) Raises the DragEnter event. (Inherited from Control) IDropTarget.OnDragLeave(EventArgs) Raises the DragLeave event. (Inherited from Control) IDropTarget.OnDragOver(DragEventArgs) Raises the DragOver event. (Inherited from Control)

Applies to

See also

[C#] Hướng dẫn sử dụng Treeview Control

Chia sẻ

Giới thiệu

Trong bài trước csharpcanban.com đã hướng dẫn các bạn sử dụng Listview trên C#, các bạn có thể xem lại ở đây: Hướng dẫn sử dụng Listview Control trong C# . Để tiếp tục với loạt bài hướng dẫn sử dụng các Controls trong Winform (C#) trong bài viết này csharpcanban.com sẽ hướng dẫn các bạn sử dụng Treeview Control để lập trình phần mềm trên C# Winform.

TreeView là một control trong Windows Form dùng để hiển thị hệ thống phân cấp của các node, hay có thể gọi là hiển thị theo dạng “cây”, vì nó có gốc và các nhánh, giống như trên hệ điều hành Windows mà các bạn sử dụng, các tệp tin và thư mục được hiển thị trong cửa sổ phía bên phải của Windows Explorer. TreeView có các node có thể đóng hoặc mở các nhánh tương ứng. Ngày nay, các lập trình viên thường sử dụng TreeView control vì nó rất hữu ích trong nhiều trường hợp, nó giúp việc hiển thị dữ liệu một cách có hệ thống hơn, rõ ràng hơn.

Xây dựng phần mềm

Bước 1. Tạo một Project Winform mới tên là HDTreeview.

Bước 2. Kéo 1 Treeview từ Toolbox vào Form1.

Bước 3. Trong sự kiện Form_Load() thêm mã sau

private void Form1_Load(object sender, EventArgs e) { TreeNode tNode; tNode = treeView1.Nodes.Add(“Websites”); treeView1.Nodes[0].Nodes.Add(“csharpcanban.com”); treeView1.Nodes[0].Nodes[0].Nodes.Add(“CLR”); treeView1.Nodes[0].Nodes.Add(“csharpcanban.com”); treeView1.Nodes[0].Nodes[1].Nodes.Add(“String Tutorial”); treeView1.Nodes[0].Nodes[1].Nodes.Add(“Excel Tutorial”); treeView1.Nodes[0].Nodes.Add(“csharpcanban.com”); treeView1.Nodes[0].Nodes[2].Nodes.Add(“ADO.NET”); treeView1.Nodes[0].Nodes[2].Nodes[0].Nodes.Add(“Dataset”); }

Bước 4. Chạy chương trình

Sau khi nhấn F5 để chạy chương trình sẽ hiện ra cửa sổ sau, tiếp tục nhấp vào nốt Button1 sẽ hiện ra Messagebox như dưới đây

Chú ý: Bạn có thể mở Treeview bằng cách nhấp chuột vào các dấu (+) hoặc thu gọn Treeview bằng cách nhấp chuột vào dấu (-).

Để mở hoặc thu gọn Treeview bạn có thể sử dụng phương thức TreeNode.Expand.

Bạn cũng có thể duyệt Treeview từ node tới node thông qua các thuộc tính: FirstNode, LastNode, NextNode, PrevNode, NextVisibleNode, PrevVisibleNode.

Gợi ý 1. Để lấy đường dẫn tới node đã được chọn trên Treeview bạn thực hiện như sau:

treeView1.SelectedNode.FullPath.ToString ();

Gợi ý 2. Trên Treeview cũng có thể sử dụng thuộc tính CheckBoxes. Để hiện Checkboxs bạn hãy đặt cho nó giá trị là True như sau:

treeView1.CheckBoxes = true;

Mã nguồn chương trình

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace HDTreeView { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { TreeNode tNode; tNode = treeView1.Nodes.Add(“Websites”); treeView1.Nodes[0].Nodes.Add(“csharpcanban.com”); treeView1.Nodes[0].Nodes[0].Nodes.Add(“CLR”); treeView1.Nodes[0].Nodes.Add(“csharpcanban.com”); treeView1.Nodes[0].Nodes[1].Nodes.Add(“String Tutorial”); treeView1.Nodes[0].Nodes[1].Nodes.Add(“Excel Tutorial”); treeView1.Nodes[0].Nodes.Add(“csharpcanban.com”); treeView1.Nodes[0].Nodes[2].Nodes.Add(“ADO.NET”); treeView1.Nodes[0].Nodes[2].Nodes[0].Nodes.Add(“Dataset”); } private void button1_Click(object sender, EventArgs e) { MessageBox.Show(treeView1.SelectedNode.FullPath.ToString()); } private void Form1_FormClosed(object sender, FormClosedEventArgs e) { System.Diagnostics.Process.Start(“https://csharpcanban.com”); } } }

Kết luận

Như vậy, trên đây csharpcanban.com đã Hướng dẫn các bạn xây dựng một chương trình sử dụng Treeview Control trong C#. Thực sự là quá đơn giản đúng không các bạn. Hãy thực hành và vận dụng vào phần mềm của các bạn. Nếu gặp khó khăn trong quá trình sử dụng Treeview Control trong C# hãy đặt câu hỏi trong phần bình luận ở dưới đây. Csharpcanban.com sẽ hướng dẫn các bạn chi tiết nhất.

Trong các bài tiếp theo mình sẽ tiếp tục hướng dẫn các bạn sử dụng Microsoft Visual Studio C#, sử dụng các Control trong C#, đó là sử dụng Combobox, Listbox, Radio Button, và nhiều Control khác, hãy thường xuyên theo dõi website csharpcanban.com để cập nhật các bài học mới nhé các bạn.

Bản quyền

Nội dung trên csharpcanban.com được biên soạn, biên dịch từ nhiều nguồn khác nhau, đồng thời đã được viết thành chương trình chạy. Csharpcanban.Com bảo lưu mọi quyền đối với những bài viết này. Mọi hình thức sao chép đều cần phải được Csharpcanban.Com cấp phép.

Tải CODE

Để tải Code, các bạn hãy nhấp vào Nốt bấm dưới đây. Do mình dử dụng dịch vụ lưu trữ của MediaFire cho nên các bạn hoàn toàn yên tâm là không có virus đâu nhé.

Mật mã giải nén: csharpcanban.com

Chia sẻ

So you have finished reading the c treeview topic article, if you find this article useful, please share it. Thank you very much. See more: TreeView C#, Treeview Tkinter, Load dữ liệu lên TreeView trong C#, Treeview bootstrap 4, Treeview là gì, Treeview jQuery, Treeview Python, TreeView checkbox c winforms

Leave a Comment