执行后
窗体加载时,填充TreeView,点击某一层级时,将文本填充到右侧的textBox文本框中
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; using System.Xml; namespace Street { public partial class FrmMain : Form { public FrmMain() { InitializeComponent(); } private void FrmMain_Load(object sender, EventArgs e) { XmlDocument xml = new XmlDocument(); xml.Load("Address.xml"); XmlElement root = xml.DocumentElement; TreeNode tn = new TreeNode(); tn.Text = root.Attributes["name"].InnerText; tvStreet.Nodes.Add(tn); TreeNode no = null; TreeNode chids = null; foreach (XmlNode item in root.ChildNodes) { no = new TreeNode(item.Attributes["name"].Value); tn.Nodes.Add(no); TreeNode chid = null; foreach (XmlNode node in item.ChildNodes) { chid = new TreeNode(node.Attributes["name"].Value); no.Nodes.Add(chid); foreach (XmlNode items in node.ChildNodes) { chids = new TreeNode(items.InnerXml); chid.Nodes.Add(chids); } } } } private void tvStreet_AfterSelect(object sender, TreeViewEventArgs e) { if (this.tvStreet.SelectedNode.Level == 0) { textBox1.Text = tvStreet.SelectedNode.Text; } else if (this.tvStreet.SelectedNode.Level == 1) { textBox1.Text = tvStreet.SelectedNode.Parent.Text; textBox2.Text = tvStreet.SelectedNode.Text; } else if (this.tvStreet.SelectedNode.Level == 2) { textBox1.Text = tvStreet.SelectedNode.Parent.Parent.Text; textBox2.Text = tvStreet.SelectedNode.Parent.Text; textBox3.Text = tvStreet.SelectedNode.Text; } else if (this.tvStreet.SelectedNode.Level == 3) { textBox1.Text = tvStreet.SelectedNode.Parent.Parent.Parent.Text; textBox2.Text = tvStreet.SelectedNode.Parent.Parent.Text; textBox3.Text = tvStreet.SelectedNode.Parent.Text; textBox4.Text = tvStreet.SelectedNode.Text; } } } }