tags:

views:

3419

answers:

5

C# In a nut shell can I display images in a list box? I have a list of users and I want to display a green tick next to some of the names, is this possible?

PS Winforms program.

Thanks

+1  A: 

Steve, this article might point you in the right direction:
http://www.codeproject.com/KB/combobox/glistbox.aspx

gkrogers
+2  A: 

In WPF it's quite simple, but if you're using winforms, you can't do it with the System.Windows.Forms.ListBox control. You can do it with the ListView control, or third party controls.

Michael Meadows
A: 

System.Windows.Forms.ListView will do the trick very easily. You might have to work a little harder than a ListBox if you want the list in 'details' view though.

Coincoin
+4  A: 

The following code displays how to do custom drawing in a listbox.

using System.Windows.Forms;
using System.Drawing;

namespace Toolset.Controls
{
    public class CustomDrawListBox : ListBox
    {
        public CustomDrawListBox()
        {
            this.DrawMode = DrawMode.OwnerDrawVariable; // We're using custom drawing.
            this.ItemHeight = 40; // Set the item height to 40.
        }

        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            // Make sure we're not trying to draw something that isn't there.
            if (e.Index >= this.Items.Count || e.Index <= -1)
                return;

            // Get the item object.
            object item = this.Items[e.Index];
            if (item == null)
                return;

            // Draw the background color depending on 
            // if the item is selected or not.
            if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
            {
                // The item is selected.
                // We want a blue background color.
                e.Graphics.FillRectangle(new SolidBrush(Color.Blue), e.Bounds);
            }
            else
            {
                // The item is NOT selected.
                // We want a white background color.
                e.Graphics.FillRectangle(new SolidBrush(Color.White), e.Bounds);
            }

            // Draw the item.
            string text = item.ToString();
            SizeF stringSize = e.Graphics.MeasureString(text, this.Font);
            e.Graphics.DrawString(text, this.Font, new SolidBrush(Color.White),
                new PointF(5, e.Bounds.Y + (e.Bounds.Height - stringSize.Height) / 2));
        }
    }
}
Patrik
A: 

here"s where i am now. I added a WPF UserControl to my C# project, and it looks like this:

< UserControl x:Class="Project.TweetWPF" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300"> < Grid x:Name="LayoutRoot"> < ListBoxItem HorizontalAlignment="Center" Width="248"> < Grid Width="248"> < Label x:Name="_author" Content="{Binding Author}" Margin="81,1,8,0" Height="20" VerticalAlignment="Top" FontFamily="Microsoft Sans Serif" FontSize="10.667" FontWeight="Bold" Padding="0"/> < Label x:Name="_message" Width="159" Height="50" HorizontalAlignment="Left" Margin="81,16,0,16" d:LayoutOverrides="VerticalAlignment" Padding="3" MaxWidth="159" MaxHeight="75" ScrollViewer.VerticalScrollBarVisibility="Disabled" MinWidth="159" MinHeight="75" Content="{Binding Message}"/> < Image x:Name="_picture" Source="{Binding Image}" Width="58" HorizontalAlignment="Left" Margin="8,11.5,0,37.5"/> < Label x:Name="_date" Content="{Binding Date}" VerticalAlignment="Bottom" TabIndex="0" Margin="81,0,8,0" FontFamily="Microsoft Sans Serif" FontSize="9.333"/> < /Grid> < /ListBoxItem> < /Grid> < /UserControl>

In my C# code, i have

TweetWPF tweet = new TweetWPF(); tweet._author.Content = "Zoli"; tweet._date.Content = DateTime.Now.ToString(); tweet._message.Content = "hello, i'm a tweet";

I added a WPF Elementhost to my GUI, its name is elHost

finnally i added the tweet to the elHost like this:

elHost.Child = tweet;

It shows the tweet, but only one when i add more childs to the elHost like this,

elHost.Child = tweet2; elHost.Child = tweet3;

it only shows tweet3.

What should i modify in my code so i can add more items? And How? Thanks already for your help!

This isn't an answer to the question. Instead it's a new question. So post it as a new one (maybe with a reference to this question), delete this answer and you'll get some help.
Oliver