добавление узлов с помощью встроенного редактора
Добавьте элемент управления TreeView на форму. Нажмите на бермудский треугольник.
Нажмите на EditNodes. Откроется редактор.
Добавьте корневой элемент.
Кликните по корневому элементу и добавьте дочерний.
Form1.cs
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
WindowsFormsApplication2 {
-
public
partial
class
Form1 : Form {
-
public
Form1() {
-
InitializeComponent();
-
}
-
//кнопка OK
-
private
void
button1_Click(
object
sender, EventArgs e) {
-
textBox1.Text += treeView1.SelectedNode.Text +
"\r\n"
;
-
}
-
//кнопка Clear
-
private
void
button2_Click(
object
sender, EventArgs e) {
-
textBox1.Clear();
-
}
-
}
}
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
WindowsFormsApplication2 {
-
public
partial
class
Form1 : Form {
-
public
Form1() {
-
InitializeComponent();
-
}
-
//кнопка OK
-
private
void
button1_Click(
object
sender, EventArgs e) {
-
textBox1.Text += treeView1.SelectedNode.Text +
"\r\n"
;
-
}
-
//кнопка Clear
-
private
void
button2_Click(
object
sender, EventArgs e) {
-
textBox1.Clear();
-
}
-
}
}
добавление узлов программно
Form1.cs
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
WindowsFormsApplication1 {
-
public
partial
class
Form1 : Form {
-
public
Form1() {
-
InitializeComponent();
-
/*2 уровень*/
-
TreeNode node2Red =
new
TreeNode(
"Red"
);
-
TreeNode node2Green =
new
TreeNode(
"Green"
);
-
TreeNode node2Blue =
new
TreeNode(
"Blue"
);
-
TreeNode[] arra2Name =
new
TreeNode[] {node2Red, node2Green, node2Blue};
-
TreeNode node2FF0000 =
new
TreeNode(
"#FF0000"
);
-
TreeNode node200FF00 =
new
TreeNode(
"#00FF00"
);
-
TreeNode node20000FF =
new
TreeNode(
"#0000FF"
);
-
TreeNode[] arra2HEX =
new
TreeNode[] {node2FF0000, node200FF00, node20000FF};
-
TreeNode node2_255_0_0 =
new
TreeNode(
"255, 0, 0"
);
-
TreeNode node2_0_255_0 =
new
TreeNode(
"0, 255, 0"
);
-
TreeNode node2_0_0_255 =
new
TreeNode(
"0, 0, 255"
);
-
TreeNode[] arra2RGB =
new
TreeNode[] {node2_255_0_0, node2_0_255_0, node2_0_0_255};
-
/*1 уровень*/
-
TreeNode node1Name =
new
TreeNode(
"Name"
, arra2Name);
-
TreeNode node1HEX =
new
TreeNode(
"HEX"
, arra2HEX);
-
TreeNode node1RGB =
new
TreeNode(
"RGB"
, arra2RGB);
-
TreeNode[] arra1Colors =
new
TreeNode[] { node1Name, node1HEX, node1RGB };
-
/*0 уровень*/
-
TreeNode node0Colors =
new
TreeNode(
"Colors"
, arra1Colors);
-
treeView1.Nodes.Add(node0Colors);
-
//раскрываем все узлы
-
treeView1.ExpandAll();
-
}
-
//кнопка OK
-
private
void
button1_Click(
object
sender, EventArgs e) {
-
textBox1.Text += treeView1.SelectedNode.Text +
"\r\n"
;
-
}
-
//кнопка Clear
-
private
void
button2_Click(
object
sender, EventArgs e) {
-
textBox1.Clear();
-
}
-
}
}
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
WindowsFormsApplication1 {
-
public
partial
class
Form1 : Form {
-
public
Form1() {
-
InitializeComponent();
-
/*2 уровень*/
-
TreeNode node2Red =
new
TreeNode(
"Red"
);
-
TreeNode node2Green =
new
TreeNode(
"Green"
);
-
TreeNode node2Blue =
new
TreeNode(
"Blue"
);
-
TreeNode[] arra2Name =
new
TreeNode[] {node2Red, node2Green, node2Blue};
-
TreeNode node2FF0000 =
new
TreeNode(
"#FF0000"
);
-
TreeNode node200FF00 =
new
TreeNode(
"#00FF00"
);
-
TreeNode node20000FF =
new
TreeNode(
"#0000FF"
);
-
TreeNode[] arra2HEX =
new
TreeNode[] {node2FF0000, node200FF00, node20000FF};
-
TreeNode node2_255_0_0 =
new
TreeNode(
"255, 0, 0"
);
-
TreeNode node2_0_255_0 =
new
TreeNode(
"0, 255, 0"
);
-
TreeNode node2_0_0_255 =
new
TreeNode(
"0, 0, 255"
);
-
TreeNode[] arra2RGB =
new
TreeNode[] {node2_255_0_0, node2_0_255_0, node2_0_0_255};
-
/*1 уровень*/
-
TreeNode node1Name =
new
TreeNode(
"Name"
, arra2Name);
-
TreeNode node1HEX =
new
TreeNode(
"HEX"
, arra2HEX);
-
TreeNode node1RGB =
new
TreeNode(
"RGB"
, arra2RGB);
-
TreeNode[] arra1Colors =
new
TreeNode[] { node1Name, node1HEX, node1RGB };
-
/*0 уровень*/
-
TreeNode node0Colors =
new
TreeNode(
"Colors"
, arra1Colors);
-
treeView1.Nodes.Add(node0Colors);
-
//раскрываем все узлы
-
treeView1.ExpandAll();
-
}
-
//кнопка OK
-
private
void
button1_Click(
object
sender, EventArgs e) {
-
textBox1.Text += treeView1.SelectedNode.Text +
"\r\n"
;
-
}
-
//кнопка Clear
-
private
void
button2_Click(
object
sender, EventArgs e) {
-
textBox1.Clear();
-
}
-
}
}