("_")(%_%)求救呀关于树型菜单的问题帮忙解决谢谢各位了
比如界面里有一个树型菜单如下
-1111
-2222
+3333
+4444
-5555
+6666
+7777
-8888
+9999
+0000
一进来就是这样一个界面效果
现在要说的是能不能在界面里加一个导入和导出两个按钮呢
它的功能我个人理解就是
想吧树型菜单的内容保存到处到一个什么地方用文件存起来,如果下次在来后要找上次的信息直接把数据导入进来就可以了,这样的功能可以实现吗各位朋友
如果能实现那有什么好的解决方法呢,在此给各位行礼了帮我解决问题
[解决办法]
从 XML 读数据 吧. 或者直接从 数据库
[解决办法]
用XML是个好主意.
[解决办法]
可以,对TreeView进行设置,TreeView本身有对XML的支持,不过还要一个XSLT文件进行翻译,一时半会儿讲不清楚,建议你先看看有关.Net有关XML的书籍,如果是做网站就看Wrox那本就不错。最好把里面的例子做成类库,以后自己用者就方便了。
[解决办法]
序列化实现对TREEVIEW的存储:
///class2 对TreeView进行序列化
///用于文件的存读;
///文件以二进制写入
public class TreeViewDataAccess
{
/// TreeViewData
[Serializable()]
public struct TreeViewData
{
public TreeNodeData[] Nodes;
/// 递归初始化TreeView数据
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[/size][i][size=2] = new TreeNodeData(treeview.Nodes[/size][i][size=2]);
}
}
/// 通过TreeViewData弹出TreeView
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[/size][i][size=2].ToTreeNode());
}
treeview.EndUpdate();
}
}
/// TreeNodeData
[Serializable()]
public struct TreeNodeData
{
public string Text;
public int ImageIndex;
public int SelectedImageIndex;
public bool Checked;
public bool Expanded;
public object Tag;
public Color BackColor;
public TreeNodeData[] Nodes;
/// TreeNode构造函数
public TreeNodeData(TreeNode node)
{
this.Text = node.Text;
this.ImageIndex = node.ImageIndex;
this.SelectedImageIndex = node.SelectedImageIndex;
this.Checked = node.Checked;
this.BackColor=node.BackColor;
this.Expanded = node.IsExpanded;
this.Nodes = new TreeNodeData[node.Nodes.Count];
this.Tag =node.Tag;
if (node.Nodes.Count == 0)
{
return;
}
for (int i = 0; i <= node.Nodes.Count - 1; i++)
{
Nodes[/size][i][size=2] = new TreeNodeData(node.Nodes[/size][i][size=2]);
}
}
/// TreeNodeData返回TreeNode
public TreeNode ToTreeNode()
{
TreeNode ToTreeNode = new TreeNode(this.Text, this.ImageIndex, this.SelectedImageIndex);
ToTreeNode.Checked = this.Checked;
ToTreeNode.BackColor=this.BackColor;
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[/size][i][size=2].ToTreeNode());
}
return ToTreeNode;
}
}
/// 加载TreeView
public static void LoadTreeViewData(TreeView treeView, string path)
{
try
{
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();
}
catch
{}
}
/// 保存TreeView到文件
public static void SaveTreeViewData(TreeView treeView, string path)
{
try
{
BinaryFormatter ser = new BinaryFormatter();
Stream file = new FileStream(path,FileMode.Create);
ser.Serialize(file,new TreeViewData(treeView));
file.Close();
}
catch
{
}
}
}
}
[解决办法]
你自己使用tree型结构,应该知道他们中的关联,如同数据结构中的树,谁是谁的子,用一个栏位来记录,就可以放到数据库中