选择项目时如何避免winforms树视图图标更改 [英] How to avoid winforms treeview icon changes when item selected

查看:42
本文介绍了选择项目时如何避免winforms树视图图标更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个小的 C#/Winforms 应用程序中试验树视图.我已经以编程方式为树视图分配了一个 ImageList,并且所有节点都可以很好地显示它们的图标,但是当我单击一个节点时,它的图标会发生变化(到 ImageList 中的第一个图像).如何让图标保持不变?

I'm experimenting with a treeview in a little C#/Winforms application. I have programatically assigned an ImageList to the treeview, and all nodes show their icons just fine, but when I click a node, its icon changes (to the very first image in the ImageList). How can I get the icon to remain unchanged?

顺便说一句:SelectedImageIndex"设置为(none)",因为我真的不知道将其设置为什么,因为节点的图像索引不同(我猜?).

BTW: The "SelectedImageIndex" is set to "(none)", since I don't really know what to set it to, since the image-index is different for the nodes (i guess?).

更新:这是应用程序的代码(我使用的是 Visual Studio Express 2008):

UPDATE: Here is the code of the application (I'm using Visual Studio Express 2008):

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            treeView1.BeginUpdate();
            treeView1.Nodes.Clear();
            treeView1.Nodes.Add("root","Project", 0);  

            treeView1.Nodes[0].Nodes.Add("Foo", "Foo", 2);
            treeView1.Nodes[0].Nodes[0].Nodes.Add("Fizz", "Fizz", 3);
            treeView1.Nodes[0].Nodes[0].Nodes.Add("Buzz", "Buzz", 3);

            treeView1.Nodes[0].Nodes.Add("Bar", "Bar", 1);
            treeView1.Nodes[0].Nodes[1].Nodes.Add("Fizz", "Fizz", 2);
            treeView1.Nodes[0].Nodes[1].Nodes[0].Nodes.Add("Buzz", "Buzz", 3);

            treeView1.EndUpdate();
            treeView1.ImageList = imageList1;
        }
    }
}

推荐答案

只需将每个节点的 SelectedImageIndex 设置为与 ImageIndex 相同的值.因此,如果您以编程方式创建节点:

Simply set the SelectedImageIndex for each node to the same value as ImageIndex. So, if you're creating your node programatically:

        TreeNode node = new TreeNode("My Node");
        node.ImageIndex = 1;
        node.SelectedImageIndex = 1;

或者您可以在构造函数中指定整个批次:

Or you can specify the whole lot in the constructor:

        TreeNode node = new TreeNode("My Node" ,1, 1);

如果您在设计时添加节点,您可以使用设计时编辑器执行相同的操作.您只需要在节点级别而不是在 TreeView 级别设置 SelectedImageIndex.

You can do the same thing using the design time editor if you're adding nodes at design time. You just need to set the SelectedImageIndex at the node level and not at the TreeView level.

这篇关于选择项目时如何避免winforms树视图图标更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆