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.
Related
I have a ListView and a List<Image> (there are 3 images in the list). I'm trying to add all the images to the ListView and currently this is what I have:
foreach (Image image in ListOfImages)
{
PictureBox pictureBox = new PictureBox();
pictureBox.BackColor = Color.Gray;
pictureBox.Width = 200;
pictureBox.Height = 200;
pictureBox.Image = image;
pictureBox.SizeMode = PictureBoxSizeMode.Zoom;
listView1.Controls.Add(pictureBox);
}
And this is what it looks like in the end:
That image is the first one in my list. The other two aren't visible. Is there a reason why the others aren't showing up?
I think microsoft has a better solution to offer - ImageList.
Why can't you use the ImageList?
I can help you one that
First create an Imagelist control from Toolbox.
Set your istview's imagelist.
e.g. listView1.LargeImageList = imageList1;
foreach (Image image in ListOfImages)
{
imageList1.Images.Add(image);
}
Add list Items to your list control and mention the respective image index.
listView1.Items.Add(key, text, imageindex);
Hope this helps!
Ok, I've found that the correct answer for what I want to do is to not use a ListView but to instead use a FlowLayoutPanel. The code above will work exactly the same way, except instead of listView1 it should be flowLayoutPanel1 (or whatever the name of the FlowLayoutPanel is)
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);
}
For my project I would need to create clickable tiles sort of like a grid. To do so I have decided to try using an array of labels and clicking on any one of them would cause a mouse click event corresponding to the label clicked. I don't want to use the Visual Studio drag and drop labels to draw the 220 I need so i decided to create an array of Labels. Here is the code I am using to test out the use of the array of labels:
Label[] Tiles = new Label[10];
for (int i = 0; i != 10; i++)
{
for (int n = 0; n != 22; n++)
{
Tiles[i] = new Label();
Tiles[i].Size = new Size(62, 62);
Tiles[i].Location = new System.Drawing.Point(n * 62 + 118, 106 + i * 62);
Tiles[i].Text = (i+n).ToString();
Tiles[i].Name = (i + n).ToString();
Tiles[i].AutoSize = true;
Tiles[i].Click += new System.EventHandler(this.label1_Click);
}
}
I am using this code In the Form1_Load method but the problem is that it doesn't throw an error but also does not actually get the labels on the Form, it just initializes the labels but does not draw them, does someone know how to actually add them to the Form.
This is really easy to do!
In your innermost for loop, add this line:
this.Controls.Add(Tiles[i]);
It first gets all the controls on the form, then add the label in it!
However, I would advise you to add the labels to a Panel, just because since you're creating a grid, you should probably group the labels together using a Panel.
Create a panel in the designer, call it labelPanel or whatever, and call this method instead in the innermost for loop:
this.labelPanel.Controls.Add(Tiles[i]);
Please note that since the position of the labels are now relative to the panel, you need to adjust them again.
You have to add that labels to the form. like, put one groupbox in your form and named it as group1.
now add Labels to that group.
group1.Controls.Add(Tiles[i]);
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);
}
}
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.