c#,将txt文档导入treeview
现有一TXT文本,文本内容如下:学生列表学生1小明7岁学生2小华8岁......我想将此文本导入treeview中,效果如下:学生列表下包含两个List:学生1和学生2学...
现有一TXT文本,文本内容如下:
学生列表
学生1
小明
7岁
学生2
小华
8岁
......
我想将此文本导入treeview中,效果如下:
学生列表下包含两个List:学生1和学生2
学生 List下包含姓名和年龄.
请问怎么实现?
请着重说明怎么区分小明是学生1,小华是学生2、、、 展开
学生列表
学生1
小明
7岁
学生2
小华
8岁
......
我想将此文本导入treeview中,效果如下:
学生列表下包含两个List:学生1和学生2
学生 List下包含姓名和年龄.
请问怎么实现?
请着重说明怎么区分小明是学生1,小华是学生2、、、 展开
3个回答
展开全部
using System;
using System.IO;
using System.Windows.Forms;
using System.Runtime.Serialization.Formatters.Binary;
namespace Makings
{
/// <summary>
/// TreeView串行化类
/// </summary>
public class TreeViewDataAccess
{
public TreeViewDataAccess() { }
/// <summary>
/// TreeViewData
/// </summary>
[Serializable()]
public struct TreeViewData
{
public TreeNodeData[] Nodes;
/// <summary>
/// 递归初始化TreeView数据
/// </summary>
/// <param name="treeview"></param>
public TreeViewData(TreeView treeview)
{
Nodes = new TreeNodeData[treeview.Nodes.Count];
if (treeview.Nodes.Count == 0)
{
return;
}
for (int i = 0; i <= treeview.Nodes.Count - 1; i++)
{
Nodes[i] = new TreeNodeData(treeview.Nodes[i]);
}
}
/// <summary>
/// 通过TreeViewData弹出TreeView
/// </summary>
/// <param name="treeview"></param>
public void PopulateTree(TreeView treeview)
{
if (this.Nodes == null || this.Nodes.Length == 0)
{
return;
}
treeview.BeginUpdate();
for (int i = 0; i <= this.Nodes.Length - 1; i++)
{
treeview.Nodes.Add(this.Nodes[i].ToTreeNode());
}
treeview.EndUpdate();
}
}
/// <summary>
/// TreeNodeData
/// </summary>
[Serializable()]
public struct TreeNodeData
{
public string Text;
public int ImageIndex;
public int SelectedImageIndex;
public bool Checked;
public bool Expanded;
public object Tag;
public TreeNodeData[] Nodes;
/// <summary>
/// TreeNode构造函数
/// </summary>
/// <param name="node"></param>
public TreeNodeData(TreeNode node)
{
this.Text = node.Text;
this.ImageIndex = node.ImageIndex;
this.SelectedImageIndex = node.SelectedImageIndex;
this.Checked = node.Checked;
this.Expanded = node.IsExpanded;
this.Nodes = new TreeNodeData[node.Nodes.Count];
if ((!(node.Tag == null)) && node.Tag.GetType().IsSerializable)
{
this.Tag = node.Tag;
}
else
{
this.Tag = null;
}
if (node.Nodes.Count == 0)
{
return;
}
for (int i = 0; i <= node.Nodes.Count - 1; i++)
{
Nodes[i] = new TreeNodeData(node.Nodes[i]);
}
}
/// <summary>
/// TreeNodeData返回TreeNode
/// </summary>
/// <returns></returns>
public TreeNode ToTreeNode()
{
TreeNode ToTreeNode = new TreeNode(this.Text, this.ImageIndex, this.SelectedImageIndex);
ToTreeNode.Checked = this.Checked;
ToTreeNode.Tag = this.Tag;
if (this.Expanded)
{
ToTreeNode.Expand();
}
if (this.Nodes == null && this.Nodes.Length == 0)
{
return null;
}
if (ToTreeNode != null && this.Nodes.Length == 0)
{
return ToTreeNode;
}
for (int i = 0; i <= this.Nodes.Length - 1; i++)
{
ToTreeNode.Nodes.Add(this.Nodes[i].ToTreeNode());
}
return ToTreeNode;
}
}
/// <summary>
/// 加载TreeView
/// </summary>
/// <param name="treeView"></param>
/// <param name="path"></param>
public static void LoadTreeViewData(TreeView treeView, string path)
{
BinaryFormatter ser = new BinaryFormatter();
Stream file = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
TreeViewData treeData = ((TreeViewData)(ser.Deserialize(file)));
treeData.PopulateTree(treeView);
file.Close();
}
/// <summary>
/// 保存TreeView到文件
/// </summary>
/// <param name="treeView"></param>
/// <param name="path"></param>
public static void SaveTreeViewData(TreeView treeView, string path)
{
BinaryFormatter ser = new BinaryFormatter();
Stream file = new FileStream(path, FileMode.Create);
ser.Serialize(file, new TreeViewData(treeView));
file.Close();
}
}
}
得吧你的文档改成后缀为.dat格式
最后要添加一个按钮,单击按钮产生事件
最后在事件调用方法
using System.IO;
using System.Windows.Forms;
using System.Runtime.Serialization.Formatters.Binary;
namespace Makings
{
/// <summary>
/// TreeView串行化类
/// </summary>
public class TreeViewDataAccess
{
public TreeViewDataAccess() { }
/// <summary>
/// TreeViewData
/// </summary>
[Serializable()]
public struct TreeViewData
{
public TreeNodeData[] Nodes;
/// <summary>
/// 递归初始化TreeView数据
/// </summary>
/// <param name="treeview"></param>
public TreeViewData(TreeView treeview)
{
Nodes = new TreeNodeData[treeview.Nodes.Count];
if (treeview.Nodes.Count == 0)
{
return;
}
for (int i = 0; i <= treeview.Nodes.Count - 1; i++)
{
Nodes[i] = new TreeNodeData(treeview.Nodes[i]);
}
}
/// <summary>
/// 通过TreeViewData弹出TreeView
/// </summary>
/// <param name="treeview"></param>
public void PopulateTree(TreeView treeview)
{
if (this.Nodes == null || this.Nodes.Length == 0)
{
return;
}
treeview.BeginUpdate();
for (int i = 0; i <= this.Nodes.Length - 1; i++)
{
treeview.Nodes.Add(this.Nodes[i].ToTreeNode());
}
treeview.EndUpdate();
}
}
/// <summary>
/// TreeNodeData
/// </summary>
[Serializable()]
public struct TreeNodeData
{
public string Text;
public int ImageIndex;
public int SelectedImageIndex;
public bool Checked;
public bool Expanded;
public object Tag;
public TreeNodeData[] Nodes;
/// <summary>
/// TreeNode构造函数
/// </summary>
/// <param name="node"></param>
public TreeNodeData(TreeNode node)
{
this.Text = node.Text;
this.ImageIndex = node.ImageIndex;
this.SelectedImageIndex = node.SelectedImageIndex;
this.Checked = node.Checked;
this.Expanded = node.IsExpanded;
this.Nodes = new TreeNodeData[node.Nodes.Count];
if ((!(node.Tag == null)) && node.Tag.GetType().IsSerializable)
{
this.Tag = node.Tag;
}
else
{
this.Tag = null;
}
if (node.Nodes.Count == 0)
{
return;
}
for (int i = 0; i <= node.Nodes.Count - 1; i++)
{
Nodes[i] = new TreeNodeData(node.Nodes[i]);
}
}
/// <summary>
/// TreeNodeData返回TreeNode
/// </summary>
/// <returns></returns>
public TreeNode ToTreeNode()
{
TreeNode ToTreeNode = new TreeNode(this.Text, this.ImageIndex, this.SelectedImageIndex);
ToTreeNode.Checked = this.Checked;
ToTreeNode.Tag = this.Tag;
if (this.Expanded)
{
ToTreeNode.Expand();
}
if (this.Nodes == null && this.Nodes.Length == 0)
{
return null;
}
if (ToTreeNode != null && this.Nodes.Length == 0)
{
return ToTreeNode;
}
for (int i = 0; i <= this.Nodes.Length - 1; i++)
{
ToTreeNode.Nodes.Add(this.Nodes[i].ToTreeNode());
}
return ToTreeNode;
}
}
/// <summary>
/// 加载TreeView
/// </summary>
/// <param name="treeView"></param>
/// <param name="path"></param>
public static void LoadTreeViewData(TreeView treeView, string path)
{
BinaryFormatter ser = new BinaryFormatter();
Stream file = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
TreeViewData treeData = ((TreeViewData)(ser.Deserialize(file)));
treeData.PopulateTree(treeView);
file.Close();
}
/// <summary>
/// 保存TreeView到文件
/// </summary>
/// <param name="treeView"></param>
/// <param name="path"></param>
public static void SaveTreeViewData(TreeView treeView, string path)
{
BinaryFormatter ser = new BinaryFormatter();
Stream file = new FileStream(path, FileMode.Create);
ser.Serialize(file, new TreeViewData(treeView));
file.Close();
}
}
}
得吧你的文档改成后缀为.dat格式
最后要添加一个按钮,单击按钮产生事件
最后在事件调用方法
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
TXT读入的时候通过程序处理成有层次结构的数据,再绑定
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询