473,394 Members | 1,956 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

TreeView and Add an Array of Images

Hi All,

I have an array of 16x16 bitmaps (60 total). I've tried adding the
array to the ImageList with the Add method, but the TreeView paints as
if no BMP is present.

The problem is the method does not 'break out' the bitmaps. If I
specify Index 0, I get an unexpected result (horizontal compressing of
the file).

How do I direct the TreeView/ImageList team to treat this as an array,
rather than a single image? I found an old post, but the suggestion
was incorrect (http://groups.google.com/group/
microsoft.public.dotnet.languages.csharp/browse_thread/thread/
151b30d4814eda55/).

What I don't want to do is add 60 individual files to the project. It
is bad enough that the program cannot find one file in the Resource
directory. I don't want to pollute the space any further with 60
additional files.

Jeff

ImageList images = new ImageList();
TreeView tree = new TreeView();

// Resource file previously copied into .\bin\Debug
images.Images.Add(new Bitmap(<file>)); // 60 images

// ImageSize does not produce expected result... The
// control does not deduce there are 60 elements
images.ImageSize = new System.Drawing.Size(16,16);

// Throws an error - width < 256
images.ImageSize = new System.Drawing.Size(960,16);

tree.Images = images;
Nov 17 '07 #1
1 2864
In case anyone is interested, here's a solution which I hacked
together. This paradigm reeks of Java. I disliked it in college years
ago, and I still have not acquired a taste for it...

The image files were embedded in the executable (select the Image ->
Properties, Build Action = "Embedded Resource"). assemblyname and
assembly are static so they only have to be calculated once, without
the extra argument passing. If you're into software engineering, hack
it further the Java way.

////////////////////////////////////////////////
TreeView Tree = new TreeView();
TreeViewImageHelper helper = new TreeViewImageHelper();
....
Tree.ImageList = helper.CreateImageList();

////////////////////////////////////////////////
public class TreeViewImageHelper
{
private static string assemblyname = null;
private static Assembly assembly = null;

public TreeViewImageHelper() { }

public ImageList CreateImageList()
{
ImageList list = new ImageList();

string[] filenames = {
"Certificate.bmp",
"CertificateSelected.bmp",
"Message.bmp",
"MessageSelected.bmp",
null
};

for (int i = 0; filenames[i] != null; i++)
{
Stream stream = GetEmbeddedFile(filenames[i]);
if (null != stream)
{
list.Images.Add(Bitmap.FromStream(stream));
}
}
return list;
}

public Stream GetEmbeddedFile( string filename )
{
if( null == assemblyname )
{ assemblyname = Assembly.GetExecutingAssembly().GetName().Name; }

if (null == assembly)
{ assembly = System.Reflection.Assembly.Load(assemblyname); }

Stream bitmapstream = null;
try
{
bitmapstream = assembly.GetManifestResourceStream(
assemblyname + ".Resources" + "." + filename
);
}
catch
{ return null; }

return bitmapstream;
}

public int GetImageIndex(int number)
{
// return the appropriate index...
}
}
Nov 18 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Anthony Boudouvas | last post by:
Hi to all, i have a treeview that i put some nodes in it with their repsective images. If i try to change the image and set it to some other ImageList index, nothing happens. The code i use...
3
by: frazer | last post by:
hi i have a set of bitmaps in an array. and i want to add it to a imagelist so that i can show those images in my treeview. imageList2.Images.Add (Image.FromHbitmap (picture.Bitmap)); //cant...
0
by: deko | last post by:
I'm trying to implement a custom TreeView that shows a ghost image while dragging. But the form I'm using is different from the sample code found here:...
6
by: cyriel1920 | last post by:
Hi, I use the selectednodestyle with a white background so the user can see which item was chosen in the treeview navigation. The nodes have a navigateURL with a target to another frame....
0
by: apenly | last post by:
Hi all- I'm trying to Render a TreeView in a custom control, but I'm receiving a NullReferenceException at runtime. If I put the TreeView on the page it works fine, but as soon as I try to...
2
by: vincent90152900 | last post by:
Following is my code and I want the Panel1 display beside the TreeView1. I found out the ModalPopupExtender has two property, “X”, and “Y”. So, I wish I could find out the position of a...
0
by: bsturg21 | last post by:
Hello, I have an app that has a custom treeview which inherits the base treeview class and I am having a problem with the way the treeview is being redrawn. Each node in the treeview represents a...
0
by: =?Utf-8?B?V2FubmFiZQ==?= | last post by:
I have a treeview setup on my page. In case it is relevant, it is set in an accordion, which is inside an update panel. When I click the treeview node, a page loads on the right side of my browser...
0
by: =?Utf-8?B?TWlrZSBDb2xsaW5z?= | last post by:
I have a treeview, which is in an accordion, which is in an updatepanel. When I click on a node in the treeview, it loads up a control as it should. When I click on the same node again, the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.