winform树形菜单无限级分类实例
程序员文章站
2023-12-01 13:36:40
以下实现的是一个树形菜单,每一级对应一类窗口,点击叶子节点时弹出新的窗口;
用递归和委托实现的。
第一部分功能-创建树形菜单复制代码 代码如下:///
/// <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();
}
以下实现的是一个树形菜单,每一级对应一类窗口,点击叶子节点时弹出新的窗口;
用递归和委托实现的。
第一部分功能-创建树形菜单
复制代码 代码如下:
/// <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();
}
上一篇: 柿饼的功效与作用有哪些!
下一篇: .NET实现:将EXE设置开机自动启动