dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: image/icons in Windows form listBox

  1. #1
    Join Date
    Dec 2018
    Posts
    2

    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

  2. #2
    Join Date
    Dec 2018
    Posts
    2

    Re: image/icons in Windows form listBox

    clicked post too soon-made some misspells on post
    global:AppInfo.Properties.Resources.RTimg; for both

  3. #3
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,173

    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.

  4. #4
    Join Date
    Dec 2018
    Posts
    15

    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)