C# · 12月 21, 2021

winform树形菜单无限级分类实例

以下实现的是一个树形菜单,每一级对应一类窗口,点击叶子节点时弹出新的窗口;

用递归和委托实现的。

第一部分功能-创建树形菜单
复制代码 代码如下:
/// <summary>
/// 创建树形菜单
/// </summary>
public void AddTree(int ParentID,TreeNode pNode)
{
// 数据库名字字段
string strName = “Name”;
// 数据库ID字段
string strID = “ID”;
// 数据库父级ID字段
string strParentID = “ParentID”;
DataTable dt = typeManager.GetAllList();
DataView dvTree = new DataView(dt);
dvTree.RowFilter = strParentID + ” = ” + ParentID;
foreach (DaTarowView Row in dvTree)
{
TreeNode Node = new TreeNode();
if (pNode == null)
{
Node.Text = Row[strName].ToString();
Node.Name = Row[strName].ToString();
Node.Tag = Row[strID].ToString();
Node.ImageIndex = 1;
this.treeView1.Nodes.Add(Node);
AddTree(Int32.Parse(Row[strID].ToString()),Node); //再次递归
}
else
{
Node.Text = Row[strName].ToString();
Node.Name = Row[strName].ToString();
Node.Tag = Row[strID].ToString();
Node.ImageIndex = 1;
pNode.Nodes.Add(Node);
AddTree(Int32.Parse(Row[strID].ToString()),Node); //再次递归
}
}
}

/// <summary>
/// 主窗体加载
/// </summary>
/// <param name=”sender”></param>
/// <param name=”e”></param>
private void frmTree_Load(object sender,EventArgs e)
{
// 根节点ID值
int i = 0;
this.treeView1.Nodes.Clear();
AddTree(i,(TreeNode)null);
treeView1.HideSelection = true;
treeView1.ShowLines = true;
}

第二部分功能-文件夹图标变换:

复制代码 代码如下:
private void treeView1_NodeMouseDoubleClick(object sender,TreeNodeMouseClickEventArgs e)
{
ImageChange(e);
}

private void treeView1_NodeMouseClick(object sender,TreeNodeMouseClickEventArgs e)
{
ImageChange(e);
}
/// <summary>
/// 变换文件夹图标
/// </summary>
/// <param name=”e”></param>
public void ImageChange(TreeNodeMouseClickEventArgs e)
{
if (null == e.Node.FirstNode)
{
e.Node.ImageIndex = 0;
e.Node.SelectedImageIndex = 0;
}
else
{
if (e.Node.IsExpanded)
{
e.Node.ImageIndex = 0;
e.Node.SelectedImageIndex = 0;
}
else
{
e.Node.ImageIndex = 1;
e.Node.SelectedImageIndex = 1;
}
}
}

第三部分功能-委托打开新窗口:

复制代码 代码如下:
delegate void Mydelegate(string name,string id);
Mydelegate md = null;
public frmTree()
{
InitializeComponent();
}
/// <summary>
/// 打开新窗口
/// </summary>
/// <param name=”sender”></param>
/// <param name=”e”></param>
private void treeView1_AfterSelect(object sender,TreeViewEventArgs e)
{
// 设置显示图标的变换
if(null== e.Node.FirstNode)
{
e.Node.ImageIndex = 1;
e.Node.SelectedImageIndex = 0;
}
// 打开新的窗口,每一级对应一类窗口
if (e.Node != null && null == e.Node.FirstNode)
{
string tag =e.Node.Tag.ToString();
string name = e.Node.Text.ToString();
switch(e.Node.Level)
{
case 0:
this.md= new Mydelegate(OpenForm1);
break;
case 1:
this.md = new Mydelegate(OpenForm2);
break;
case 2:
this.md = new Mydelegate(OpenForm3);
break;
}
md(name,tag);
}
}
/// <summary>
/// 打开新窗口
/// </summary>
/// <param name=”name”>传递参数</param>
/// <param name=”id”>传递参数</param>
public static void OpenForm1(string name,string id)
{
Form newForm = new Form1();
newForm.ShowDialog();
}
public static void OpenForm2(string name,string id)
{
Form newForm = new Form2();
newForm.ShowDialog();
}
public static void OpenForm3(string name,string id)
{
Form newForm = new Form3();
newForm.ShowDialog();
}