TreeView Not Displaying Images from ImageList - c#

I have a TreeView that displays CheckBoxes:
I want to check if a given directory contains an ".mdf" database and if it does, check whether it is attached on the selected server instance. If the database is attached I display an image against that node, and a different image if it is not attached. Note: The images are .png format, size 32x32...
I populate an ImageList from Properties.Resources
mainImageList = new ImageList();
mainImageList.Images.Add(Properties.Resources.Database);
mainImageList.Images.Add(Properties.Resources.DatabaseGrey);
I then loop through the tree and add the relevant image
public static void RecursiveAddImage(TreeNode treeNode, List<string> attachedList)
{
if (String.Compare(Path.GetExtension(treeNode.Text), ".mdf", true) == 0)
{
string databaseName = treeNode.Text.Replace(".mdf", String.Empty);
if (attachedList.Contains(databaseName))
{
treeNode.ImageIndex = 0;
treeNode.SelectedImageIndex = 0;
}
else
{
treeNode.ImageIndex = 1;
treeNode.SelectedImageIndex = 1;
}
}
foreach (TreeNode node in treeNode.Nodes)
RecursiveAddImage(node, attachedList);
}
The above code goes through the loop with no complaints, finds ".mdf"s and seems to add the relevant ImageIndexes but these do not show up in the TreeView. What am I doing wrong here and can I add the ImageList at design time (something I also can't seem to do)?
I have read several posts and ofcourse the MSDN documantation but I still can't seem to get it working. Any help as always, is much appreciated.

Make sure the TreeView control has the ImageList property set to the correct ImageList reference:
mainImageList = new ImageList();
mainImageList.Images.Add(Properties.Resources.Database);
mainImageList.Images.Add(Properties.Resources.DatabaseGrey);
treeView1.ImageList = mainImageList;

TreeNode.StateImageIndex= 0; would set the imagelist images. Make sure the imagelist is binded to Treeview control as mentioned above.

Related

Populating winform ListView from Calling External Class, not adding image and text

I am trying to add a new image list with text describing each image below each image. I have set the listview to large icons and tried many other attempts to get preview images back into the first form.
I have been able to either
1. get very small images into the list view
2. get just the text into the listview
3. get the very tiny images again with text first then the images, not together.
I have tried all the overloaded methods and I am still at a loss as to why i can't even get a Properties.resources.image in as an image placeholder. except as a tiny image. And yes I have set the listview large icon mode and tried the imageSize properties, nothing works. Also some attempts usually with a technique that appears like it is following an explanation on here fails to show anything in the listview on the frstForm.
Any hints would be appreciated.
public ImageList imageList_c = new ImageList();
private Family_Loader_ExtEventDialog _frstForm;
foreach (KeyValuePair<string, Bitmap> kvp in element_Dict(_doc_new, BuiltInCategory.OST_Walls))
{
//imageList_c.ImageSize = new Size(120, 120);
//imageList_c.Images.Add(kvp.Key, Properties.Resources.folder);
_frstForm.listView_Family.Items.Add(kvp.Key);
}
//_frstForm.listView_Family.LargeImageList = imageList_c;
imageList_c.Images.Add(kvp.Key, kvp.Value);
_frstForm.listView_Family.View = System.Windows.Forms.View.LargeIcon;
_frstForm.listView_Family.LargeImageList = imageList_c;
for (int i = 0; i < imageList_c.Images.Count; i++)
{
ListViewItem item = new ListViewItem();
item.ImageIndex = i;
item.Text = imageList_c.Images.Keys[i];
_frstForm.listView_Family.Items.Add(item);
}

Panel Does not update TreeView

I have a TreeView that is displayed inside a panel. The data in the TreeView is based on data returned from the database. The first time, the data is correct. The second time, the TreeView is not refreshed, and the previous data is still showing in the tree. I checked the list that contain the data. The list returned the correct data. I've Google the issue, and could not resolved it with some of the answers that were posted. Here is a sample code of how the TreeView is being created and added to the Panel.
ReportGroups gr = new ReportGroups();
var Name = gr.GetReportName(groupID);
TreeView tr = new TreeView();
tr.BeginUpdate();
tr.Size = new Size(570, 600);
tr.Name = "Home";
tr.Nodes.Add("Reports Name");
tr.CheckBoxes = true;
if (Name.Count() > 0)
{
foreach (var item in Name)
{
if (item != null)
{
tr.Nodes[0].Nodes.Add(item.reportName);
}
}
}
tr.Nodes[0].ExpandAll();
tr.EndUpdate();
this.pDisplayReportName.Width = tr.Width * 2;
this.pDisplayReportName.Height = 300;
this.pDisplayReportName.Controls.Add(tr);
this.pDisplayReportName.Refresh();
What am I doing wrong?
try to add this.pDisplayReportName.Clear(); so data will not double up. :)
The easy option would be to use this.pDisplayReportName.Controls.Clear(); just after tr.EndUpdate();. But, this would cause an issue if you have other controls within the same Panel.
The best option would be to use this.pDisplayReportName.Controls.RemoveByKey("MyTree"); instead of this.pDisplayReportName.Controls.Clear();
And, another option would be to add a TreeView in design time (with name tr) rather than dynamically to the panel. Then, use tr.Nodes.Clear(); before tr.BeginUpdate(); and remove following two lines from your code.
TreeView tr = new TreeView();
.
.
.
this.pDisplayReportName.Controls.Add(tr);
Cheers

SetBinding() on Custom Image Control

I'm stuck on the SetBinding method.
I would like to have 2 kinds of icons in the table, there will be a boolean property and the shown icon will depend on this property. Here is an example :
The problem is that I canĀ“t change the icon. I've tried to google it for about 3 hours, without success.
My idea was to put there Image control and than change the source dependenig on property, but i couldnt find out how to change image source.
So I tryed to create custom sample with image template and SetBinding and here I am stuck...
int index = 0;
foreach (var item in this.VidContentItems) {
if (item.Active == false) {
this.FindControlInCollection("TrueOrFalse",
this.VidContentItems.ElementAt(index)).SetBinding(?????????);
}
index++;
}
Or maybe I'm totally wrong...
OK i solved it like this: I created a table where was just one image item and i add a control image viewer to the grid of the target table, then i made loop and if the value was false i hided the control.. looks easy but this wasnt possible when i was trying to put there just image control, becouse visibility couldnt be changed for specific control , only for all controls...that stacked me a lot..never use image control!!!
partial void VidContentItemsGrid_Activated() {
int index = 0;
foreach (var item in this.VidContentItems) {
if (item.Active == false) {
this.FindControlInCollection("TrueIconInd", this.VidContentItems.ElementAt(index)).IsVisible = false;
}
index++;
}
}

Winforms Listbox or Listview image and text

I'm not very experienced on c#. I'm working with winforms and I'm looking for a way to create something like a list of elements with this template , something like the autocompletion list of visual studio.
Is it possible to do? Shall I use listbox or listview?
EDIT
Sorry my question wasn't clear I don't want to create an autocomplete but what i want to create is something like this a list of things with an icon next to the text of that thing.
As I understand from your question, you can create custom UserControl or create a Form and put ListBox in it. If you use From be sure that you change border style layout, just set it to none. After creation for use it you should create form and show it where you want like this:
FrmAutoComplete x = new FrmAutoComplete();
x.Show();
you can put this form in ToolTipItem and show it.
Good luck.
THis is a quick and dirty example of using images in your Listview control. Since I don;t have a lot of information about what you plan to do, I tried to keep is simple.
In short, you need to load some images into one of the ImageLists (Large or Small) built into the Listview control and assign them keys so that you can assign them to specific list items as you add them.
The trick to this is determining which image to use for a specific list item (assuming there are different images assigned to different list items depending on some differentiating factor. For this example, I used an arbitrary assignment of "cars" or "trucks," and simply decided that the first five items in the list would be cars, and the last five would be trucks. I then assigned each image appropriately, using the image key as I added each listview item. You can do this for more complex scenarios, and when using the image key, it does not matter what order the items are added.
For this use case, you will want to create or use images with dimensions of 16 x 16 pixels. I went ahead and added two images to my project resource file, then simply accessed them using the project Properties.Resources name space. There are other ways to do this as well, but this is the most convenient for me.
Hope that helps.
public partial class Form1 : Form
{
static string CAR_IMAGE_KEY = "Car";
static string TRUCK_IMAGE_KEY = "Truck";
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.SetupListview();
this.LoadListView();
}
private void SetupListview()
{
var imgList = new ImageList();
imgList.Images.Add("Car", Properties.Resources.jpgCarImage);
imgList.Images.Add("Truck", Properties.Resources.jpgTruckImage);
var lv = this.listView1;
lv.View = View.List;
lv.SmallImageList = imgList;
}
private void LoadListView()
{
for(int i = 1; i <= 10; i++)
{
string currentImageKey = CAR_IMAGE_KEY;
if(i > 5) currentImageKey = TRUCK_IMAGE_KEY;
var item = this.listView1.Items.Add("Item" + i.ToString(), currentImageKey);
}
}

how to add an image and item to a imageComboxBoxEdit

I have been trying and trying and looking for ways/tutorials on how to add an image and text item to a imageComboBoxEdit I even read the documentation, but that didn't really help. I used an imageList then I added Resource.Black; to the imageList however when I try to add it to the text via this
private void AddItems(ImageComboBoxEdit editor, ImageList imgList)
{
for(int i = 0; i < 10 ; i++)
editor.Properties.Items.Add(new ImageComboBoxItem("Item " + (i + 1).ToString(), i, i));
editor.Properties.SmallImages = imgList;
}
then doing AddItems(imageComboBoxEdit1, imageList1); it works fine for the text items but if I add a bunch of images to the ImageList it just deletes all the text items and doesn't display items in it at all.
Bottom line: I NEED HELP! lol
Any and all help will be appreciated! :D thanks
With the designer:
Drop an image list on the form and added an image to it.
In the properties window for the imageComboBoxEdit expand properties set SmallImages to the ImageList added to the form.
Click on the browse button for the Items Property
Click add, to create a new item, fill in description (text to display) and the imageIndex for the image in the image list, and a value. I usually use the same number for the value as the imageIndex.
Or in code, still assuming that the image list was added to the form and has an image in it.
ImageComboBoxItem someItem = new ImageComboBoxItem();
someItem.Description = "Text To Display";
someItem.ImageIndex = 0;
someItem.Value = 0;
imageComboBoxEdit1.Properties.Items.Add(someItem);
For this example, I just did this during form load.

Categories