winform窗体 Treeview控件子节点动态添加图片
for (int x = 0; x < tb.Rows.Count; x++)
{
TreeNode childNode = new TreeNode();
childNode.Text = tb.Rows[x]["us_name"].ToString();
图片绑定代码
node.Nodes.Add(childNode);
} 展开
树节点的图片要通过imageList 里面的图片才能获得,可以是键值,也可以是索引。我给你个例子。
稍微修改下,你就能用。你可以把Button里面代码包装成一个函数,就可以去添加了
控件就是界面的
后台代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.treeView1.ImageList = imagelist;
}
ImageList imagelist = new ImageList();
private void button1_Click(object sender, EventArgs e)
{
try
{
string filename = this.textBox1.Text.Trim();
string key = this.textBox2.Text.Trim();
Image im = Image.FromFile(filename);
imagelist.Images.Add(key,im);
string name = this.textBox3.Text.Trim();
TreeNode tn = new TreeNode(name);
tn.ImageKey = key;
this.treeView1.Nodes.Add(tn);
}
catch
{
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}