且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Windows窗体中的树视图

更新时间:2023-12-06 10:49:28

如果你问怎么回事,在运行 - 时间,在代码中,您可以从数据库中获取文件,并将它们放入TreeView已链接到的ImageList中,并在TreeView中设置某些(或所有)节点以使用ImageList图形,然后读取第一节;如果您有一个更简单的场景(您可以在设计时完成所有事情),那么请跳过第I部分,然后转到第II部分。



我的运行时间




a。根据您的需要:从数据库中获取您需要的图形内容文件的路径(当然,我假设您有权阅读这些文件并获取其内容)。



b。创建一个BitMap对象,并使用其Load(FilePath)方法将文件内容放入其中。



c。将BitMap对象添加到ImageList.Images集合。



d。然后:将一些(或所有)TreeView节点的ImageIndex和/或SelectedImage属性设置为您希望它们使用的ImageList.Images [index]。



e。这里有一个简短的代码示例来说明:
  private   void  FormTemplate_Load( object  sender,EventArgs e)
{
// 使用文件加载新的BitMap对象,并将其添加到ImageList的Images集合
imageList1.Images.Add( new 位图( @ C:\ Users \ SomeUser\Desktop\Camera.png)) ;

// 在'treeView1的节点集合中设置第七个节点以使用图像
// 您只需放入ImageList。请注意,这里假设您
// 确切地知道ImageList中图像的位置刚添加的是
treeView1.Nodes [ 6 ]。ImageIndex = 6 ;
}

II设计时间



if, at design-time ,您知道要在ImageList中使用的图形内容的所有文件路径,当然,它们是可公开访问然后只需:



a。在表格上放置一个ImageList组件



b。上下文单击它,打开Images集合编辑器,打开所需的文件。



c。在TreeView的属性浏览器中,您可以为所有节点设置默认的ImageIndex和SelectedIndex。



d。使用TreeView的Nodes集合编辑器设置您想要的ImageIndex或SelectedImageIndex:您希望的节点使用默认的TreeView设置。


在TreeView中使用带有和不带图像的treenodes [ ^ 一>

hai sir/madam

i am trying to add image to treeview node. but that is not from device but from database.

database like

image Path
~\Images\xyz.jpg

may i know how to add that path to treeview node. first of all is it possible?
i got how to populate treeview. but i am not able to get thougt how to add image to tree node from database. please if possible give the way to solve this.
thank u.

If you are asking how, at run-time, in code, you can get files from a database, and put them into an ImageList that a TreeView has been linked to, and set certain (or all) Nodes in the TreeView to use the ImageList graphics, then read section I; if, by some chance, you have a more simple scenario (you can do everything at design-time), then skip section I, and go to section II.

I run-time


a. based on whatever you need: get the paths to the files with graphic content that you need from the database (of course, I assume you have permission to read those files, and get their content).

b. create a BitMap object, and use its Load(FilePath) method to get the file content into it.

c. add the BitMap object to the ImageList.Images collection.

d. then: set some (or all) TreeView Nodes' ImageIndex and/or SelectedImage Properties to the ImageList.Images[index] you want them to use.

e. here's a brief code sample to illustrate:
private void FormTemplate_Load(object sender, EventArgs e)
{
    // load new BitMap object with a file, and add it to the Images collection of ImageList
    imageList1.Images.Add(new Bitmap(@"C:\Users\SomeUser\Desktop\Camera.png"));

    // set the seventh Node in 'treeView1's Nodes collection to use the image
    // you just put in the ImageList. note that it's assumed here that you
    // know exactly where in the ImageList the Image you just added is
    treeView1.Nodes[6].ImageIndex = 6;
}

II design time

if, at design-time, you know all the file-paths to the graphic content you wish to use in an ImageList, and, of course, they are publicly accessible to you then simply:

a. put an ImageList Component on a Form

b. context-click on it, and open the Images collection editor, and open the files you want.

c. in the Property Browser for the TreeView you can set the default ImageIndex, and SelectedIndex for all the Nodes.

d. use the TreeView's Nodes collection editor to set the ImageIndex, or SelectedImageIndex you want for: nodes you wish not to use the default TreeView's settings.


Using treenodes with and without images in a TreeView[^]