-
December 15th, 2018, 01:45 PM
#1
image/icons in Windows form listBox
I am trying to understand what the issue here is:
The following code works
button1.Image = global:nfo.Properties.Resources.RTIimg;
But if trying to add image to listBox, it does not
listBox3.Items.Add(global:Info.Properties.Resources.RTimg);
The above command only displays the text "System.Drawing.Bitmap"
do I need to do additional conversation?
many thanks in advanced
-
December 15th, 2018, 01:49 PM
#2
Re: image/icons in Windows form listBox
clicked post too soon-made some misspells on post
global:AppInfo.Properties.Resources.RTimg; for both
-
December 15th, 2018, 06:34 PM
#3
Re: image/icons in Windows form listBox
Do a search for "how to add image to listbox in c#". There should be enough links to articles/examples to get you going.
-
January 2nd, 2019, 04:31 AM
#4
Re: image/icons in Windows form listBox
You may try this code to add icon or image to listBox
Code:
// GListBoxItem class
public class GlistBoxItem
{
private string _myText;
private int _myImageIndex;
// properties
public string Text
{
get {return _myText;}
set {_myText = value;}
}
public int ImageIndex
{
get {return _myImageIndex;}
set {_myImageIndex = value;}
}
//constructor
public GListBoxItem(string text, int index)
{
_myText = text;
_myImageIndex = index;
}
public GListBoxItem(string text): this(text,-1){}
public GListBoxItem(): this(""){}
public override string ToString()
{
return _myText;
}
}//End of GListBoxItem class
// GListBox class
public class GListBox : ListBox
{
private ImageList _myImageList;
public ImageList ImageList
{
get {return _myImageList;}
set {_myImageList = value;}
}
public GlistBox()
{
// Set owner draw mode
this.DrawMode = DrawMode.OwnerDrawFixed;
}
protected override void OnDrawItem(System.Windows.Forms.DrawItemEventArgs e)
{
e.DrawBackground();
e.DrawFocusRectangle();
GListBoxItem item;
Rectangle bounds = e.Bounds;
Size imageSize = _myImageList.ImageSize;
try
{
item = (GListBoxItem) Items[e.Index];
if (item.ImageIndex != -1)
{
imageList.Draw(e.Graphics, bounds.Left,bounds.Top,item.ImageIndex);
e.Graphics.DrawString(item.Text, e.Font, new SolidBrush(e.ForeColor),
bounds.Left+imageSize.Width, bounds.Top);
}
else
{
e.Graphics.DrawString(item.Text, e.Font,new SolidBrush(e.ForeColor),
bounds.Left, bounds.Top);
}
}
catch
{
if (e.Index != -1)
{
e.Graphics.DrawString(Items[e.Index].ToString(),e.Font,
new SolidBrush(e.ForeColor) ,bounds.Left, bounds.Top);
}
else
{
e.Graphics.DrawString(Text,e.Font,new SolidBrush(e.ForeColor),
bounds.Left, bounds.Top);
}
}
base.OnDrawItem(e);
}
}//End of GListBox class
Last edited by 2kaud; January 2nd, 2019 at 05:04 AM.
Reason: Added code tags
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|