c# treeView 节点问题
我的节点是
A
-B
-C
-D
B
-E
-F
E
-a
-b
我想把它弄成
A
-B
-E
-a
-b
-F
-C
-D
求大家帮帮忙。。。 C# TreeView 兄弟节点? 子节点
[解决办法]
如果你能确认第一个结点为主结点,那么很简单,遍历第一个结点下的所有结点,发现和其他根结点同名的,就把其他结点的子节点添加进来,再把那个删除即可。
[解决办法]
用这个子集
B
-E
-F
替换
A
-B
-C
-D
中的-B
然后用这个子集
E
-a
-b
替换
B
-E
-F
的孩子E
[解决办法]
/*
* 由SharpDevelop创建。
* 用户: Administrator
* 日期: 2013-7-20
* 时间: 9:57
*
* 要改变这种模板请点击 工具
[解决办法]
选项
[解决办法]
代码编写
[解决办法]
编辑标准头文件
*/
using System;
using System.Text;
using System.Collections.Generic;
using System.Collections;
using System.IO;
using System.Diagnostics;
using System.Linq;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Windows.Forms;
namespace 资源管理器的应用
{
/// <summary>
/// Description of MainForm.
/// </summary>
public partial class MainForm : Form
{
public MainForm()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
//
// TODO: Add constructor code after the InitializeComponent() call.
//
}
void MainFormLoad(object sender, EventArgs e)
{
CreateParent();
}
/// <summary>
/// 创建根节点
/// </summary>
void CreateParent()
{
tvNav.Nodes.Clear();
string path=@"D:\myweb\www";
TreeNode parent=new TreeNode();
parent.Text="网站存放目录";
parent.Tag=path;
tvNav.Nodes.Add(parent);
CreateChild(path,parent);
CreateFile(path,parent);
}
/// <summary>
/// 创建子节点
/// </summary>
/// <param name="path"></param>
/// <param name="parent"></param>
void CreateChild(string path,TreeNode parent)
{
DirectoryInfo di=new DirectoryInfo(path);
DirectoryInfo[] dirs=di.GetDirectories(); //获取该路径下的所有目录
foreach(DirectoryInfo dif in dirs) //把这些目录添加到根节点下面
{
TreeNode tn=new TreeNode();
tn.Text=dif.Name;
tn.Tag=dif.FullName;
parent.Nodes.Add(tn);
CreateChild(dif.FullName.ToString(),tn);
CreateFile(dif.FullName.ToString(),tn);
}
}
/// <summary>
/// 获取目录中的所有文件
/// </summary>
/// <param name="path"></param>
/// <param name="parent"></param>
void CreateFile(string path,TreeNode parent)
{
DirectoryInfo di=new DirectoryInfo(path);
FileInfo[] fis=di.GetFiles();
foreach(FileInfo fi in fis)
{
TreeNode tn=new TreeNode();
tn.Text=fi.Name;
tn.Tag=fi.FullName;
parent.Nodes.Add(tn);
}
}
void TvNavAfterSelect(object sender, TreeViewEventArgs e)
{
//int level=e.Node.Level;
//MessageBox.Show(level.ToString());
string tzm=Path.GetExtension(e.Node.Tag.ToString());
if(Path.HasExtension(e.Node.Tag.ToString()))
{
if(tzm==".txt"
[解决办法]
tzm==".php"
[解决办法]
tzm==".html"
[解决办法]
tzm==".css"
[解决办法]
tzm==".js"
[解决办法]
tzm==".htm"
------解决方案--------------------
tzm==".doc")
{
CreateContent(e.Node.Tag.ToString());
}
else if(tzm==".jpg"
[解决办法]
tzm==".png"
[解决办法]
tzm==".gif")
{
txtContent.Visible=false;
picShow.Visible=true;
CreateContent(e.Node.Tag.ToString());
picShow.Load(e.Node.Tag.ToString());
btnEdit.Visible=false;
btnSave.Visible=false;
}
}
}
/// <summary>
/// 获取文本文件的内容
/// </summary>
/// <param name="path"></param>
void CreateContent(string path)
{
txtTitle.Text=Path.GetDirectoryName(path);
txtFileName.Text=Path.GetFileName(path);
txtContent.Text=File.ReadAllText(path,Encoding.Default).ToString();
}
/// <summary>
/// 编辑文本文件的内容
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void BtnEditClick(object sender, EventArgs e)
{
if(txtTitle!=null)
{
btnSave.Visible=true;
}
else
{
MessageBox.Show("您没有选中要编辑的软件");
}
txtContent.ReadOnly=false;
txtContent.Focus();
}
/// <summary>
/// 修改后重新写入文本
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void BtnSaveClick(object sender, EventArgs e)
{
string content=txtContent.Text.ToString();
var pathName=txtTitle.Text+"\"+txtFileName.Text;
if(txtFileName.Text.Contains("."))
{
File.WriteAllText(pathName,content,Encoding.Default);
}
else
{
File.WriteAllText(pathName+".txt",content,Encoding.Default);
}
txtContent.ReadOnly=true;
MessageBox.Show("保存成功");
}
void FolderBrowserDialog1HelpRequest(object sender, EventArgs e)
{
}
void BtnFolderClick(object sender, EventArgs e)
{
FolderBrowserDialog fd = new FolderBrowserDialog();
if (fd.ShowDialog() == DialogResult.OK)
txtTitle.Text = fd.SelectedPath;
}
void TxtTitleTextChanged(object sender, EventArgs e)
{
}
}
}