Click here to Skip to main content
15,893,622 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi,

I have a listview in a windows forms application with 2 columns as shown below:

MIDL
listUsers1.View = View.Details;
listUsers1.FullRowSelect = true;
listUsers1.Columns.Add("Icon", 35, HorizontalAlignment.Left);
listUsers1.Columns.Add("Name", 200, HorizontalAlignment.Left);


This shows and icon and then a string. I need to be able to select a row and when it is highlighted only the name is on a blue background and not the icon.

I need to leave the fullrow select option as when I tried to remove it I lost some functionality.

Basically I wnat to set the highlight colour of the icon to white.

I tried changing the backcolor but as expected this takes effect after the selection is removed.

Any ideas please?
Posted
Updated 30-Jun-11 13:33pm
v2
Comments
Sergey Alexandrovich Kryukov 30-Jun-11 19:23pm    
No, it won't work like this. Not clear. First, tag it! Forms, WPF, what?!
--SA
milenalukic 30-Jun-11 19:33pm    
The listview works. The only problem is that when I select an item it highlights the icon and the text together and the highlight shows different shades of blue for some reason. I need to get it all the same colour or just highlight the text.

1 solution

There are a few examples on the Net. One of them is in documentation of the DrawText method of the ListViewItem: http://msdn.microsoft.com/en-us/library/989zcatz.aspx[^]

I found that the DrawText() method does not position the text properly, no matter what flags I passed it. So I used the DrawString instead (which still didn't position it perfectly, but at least there's more control over e.DrawString). You are probably showing an icon in the first column, so you will have to use the e.Graphics to draw the image.

On a side note. ListView is not a good control to display data. You'll be sorry you ever chose it if you ever need to customize some more. Consider a DataGridView instead.

public partial class Form1 : Form
    {
        private ColumnHeader _iconHeader;

        public Form1()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            listUsers1.View = View.Details;
            listUsers1.FullRowSelect = true;
            _iconHeader = listUsers1.Columns.Add("Icon", 35, HorizontalAlignment.Left);
            listUsers1.Columns.Add("Name", 200, HorizontalAlignment.Left);

            listUsers1.Items.Add(new ListViewItem(new[] { "def", "def" }));
            listUsers1.Items.Add(new ListViewItem(new[] { "abc2", "def" }));
            listUsers1.Items.Add(new ListViewItem(new[] { "abc3", "def" }));
            listUsers1.Items.Add(new ListViewItem(new[] { "abc4", "def" }));

            listUsers1.OwnerDraw = true;
            listUsers1.DrawItem += new DrawListViewItemEventHandler(listUsers1_DrawItem);
            listUsers1.DrawSubItem += new DrawListViewSubItemEventHandler(listUsers1_DrawSubItem);
            listUsers1.DrawColumnHeader += new DrawListViewColumnHeaderEventHandler(listUsers1_DrawColumnHeader);
        }

        void listUsers1_DrawItem(object sender, DrawListViewItemEventArgs e)
        {
            ListViewItemStates selectedState = ListViewItemStates.Selected | ListViewItemStates.Focused;
            if (selectedState == (e.State & selectedState))
            {
                e.DrawBackground();
                e.DrawText();
            }
            else
            {
                e.DrawDefault = true;
            }            
        }

        void listUsers1_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
        {
            e.DrawDefault = true;
        }

        void listUsers1_DrawSubItem(object sender, DrawListViewSubItemEventArgs e)
        {
            if (e.ColumnIndex == listUsers1.Columns.IndexOf(_iconHeader)
                && (e.ItemState & ListViewItemStates.Focused) == ListViewItemStates.Focused)
            {
                e.DrawBackground();
                using (StringFormat sf = new StringFormat())
                {
                    sf.Alignment = StringAlignment.Near;
                    sf.LineAlignment = StringAlignment.Center;

                    e.Graphics.DrawString(e.SubItem.Text, listUsers1.Font, Brushes.Black, e.Bounds, sf);
                }
            }
            else
            {
                e.DrawDefault = true;
            }
        }
    }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900