Windows Develop Bookmark and Share   
 index > Windows Forms General > selected item in listview
 

selected item in listview

I want to display the selected items in a ListView with blue(default) back color and white fore color even if the ListView isn't focused.
vedika  Monday, February 25, 2008 10:46 AM

Hi,

Here is a code from the thread: http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=64369&SiteID=1

I have modify it a little (one little bug fixed) so it will fit your requirements:

Code Snippet

[Description("ListViewWithSelection")]

[ToolboxBitmap(typeof(ListView))]

public class ListViewWithSelection : System.Windows.Forms.ListView

{

private Color _SelectionBackColor;

private Color _SelectionForeColor;

private Color DefaultSelectionBackColor = Color.FromKnownColor(KnownColor.Highlight) ;

private Color DefaultSelectionForeColor = Color.White;

/// <summary>

/// The backcolor of the selected entry

/// </summary>

[Description("The backcolor of the selected entry.")]

[Category("Appearance")]

public Color SelectionBackColor

{

get { return _SelectionBackColor; }

set

{

if (_SelectionBackColor != value)

{

_SelectionBackColor = value;

Invalidate();

}

}

}

/// <summary>

/// The backcolor of the selected entry

/// </summary>

[Description("The forecolor of the selected entry.")]

[Category("Appearance")]

public Color SelectionForeColor

{

get { return _SelectionForeColor; }

set

{

if (_SelectionForeColor != value)

{

_SelectionForeColor = value;

Invalidate();

}

}

}

/// <summary>

/// Constructor sets a few properties

/// </summary>

public ListViewWithSelection()

: base()

{

ResetSelectionColors();

FullRowSelect = false;

HideSelection = true;

OwnerDraw = true;

//Anti flickering

this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);

}

/// <summary>

/// Resets the colors for the selection to the default colors

/// </summary>

protected void ResetSelectionColors()

{

_SelectionForeColor = DefaultSelectionForeColor;

_SelectionBackColor = DefaultSelectionBackColor;

}

/// <summary>

/// Custom drawing items

/// </summary>

/// <param name="e"></param>

protected override void OnDrawItem(DrawListViewItemEventArgs e)

{

e.DrawDefault = true;

if (this.SelectedItems.Contains(e.Item))

{

e.Item.BackColor = _SelectionBackColor;

e.Item.ForeColor = _SelectionForeColor;

}

else

{

e.Item.BackColor = this.BackColor;

e.Item.ForeColor = this.ForeColor;

}

e.DrawBackground();

}

/// <summary>

/// Custom drawing subitems

/// </summary>

/// <param name="e"></param>

protected override void OnDrawSubItem(DrawListViewSubItemEventArgs e)

{

base.OnDrawSubItem(e);

}

}

Hope this helps!

Cheers,

Jakub G

Gutek  Tuesday, February 26, 2008 11:30 AM
Hi,

You set HideSelection property to false - this will always show selected item. You can do this from the code:

private void Form1_Load_1(object sender, EventArgs e)
{
this.listView1.HideSelection = false;
}

or from Properties of ListView.

Cheers,
Jakub G
Gutek  Monday, February 25, 2008 11:35 AM
Hi,

Thanks for reply.
I have already select hideselection false so that it is showing gray as backcolor and black as fore color when listview is not focused. But I want to change these color from gray to blue and lack to white.

Thanks,
Vedika
vedika  Tuesday, February 26, 2008 6:44 AM

Hi,

Here is a code from the thread: http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=64369&SiteID=1

I have modify it a little (one little bug fixed) so it will fit your requirements:

Code Snippet

[Description("ListViewWithSelection")]

[ToolboxBitmap(typeof(ListView))]

public class ListViewWithSelection : System.Windows.Forms.ListView

{

private Color _SelectionBackColor;

private Color _SelectionForeColor;

private Color DefaultSelectionBackColor = Color.FromKnownColor(KnownColor.Highlight) ;

private Color DefaultSelectionForeColor = Color.White;

/// <summary>

/// The backcolor of the selected entry

/// </summary>

[Description("The backcolor of the selected entry.")]

[Category("Appearance")]

public Color SelectionBackColor

{

get { return _SelectionBackColor; }

set

{

if (_SelectionBackColor != value)

{

_SelectionBackColor = value;

Invalidate();

}

}

}

/// <summary>

/// The backcolor of the selected entry

/// </summary>

[Description("The forecolor of the selected entry.")]

[Category("Appearance")]

public Color SelectionForeColor

{

get { return _SelectionForeColor; }

set

{

if (_SelectionForeColor != value)

{

_SelectionForeColor = value;

Invalidate();

}

}

}

/// <summary>

/// Constructor sets a few properties

/// </summary>

public ListViewWithSelection()

: base()

{

ResetSelectionColors();

FullRowSelect = false;

HideSelection = true;

OwnerDraw = true;

//Anti flickering

this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);

}

/// <summary>

/// Resets the colors for the selection to the default colors

/// </summary>

protected void ResetSelectionColors()

{

_SelectionForeColor = DefaultSelectionForeColor;

_SelectionBackColor = DefaultSelectionBackColor;

}

/// <summary>

/// Custom drawing items

/// </summary>

/// <param name="e"></param>

protected override void OnDrawItem(DrawListViewItemEventArgs e)

{

e.DrawDefault = true;

if (this.SelectedItems.Contains(e.Item))

{

e.Item.BackColor = _SelectionBackColor;

e.Item.ForeColor = _SelectionForeColor;

}

else

{

e.Item.BackColor = this.BackColor;

e.Item.ForeColor = this.ForeColor;

}

e.DrawBackground();

}

/// <summary>

/// Custom drawing subitems

/// </summary>

/// <param name="e"></param>

protected override void OnDrawSubItem(DrawListViewSubItemEventArgs e)

{

base.OnDrawSubItem(e);

}

}

Hope this helps!

Cheers,

Jakub G

Gutek  Tuesday, February 26, 2008 11:30 AM

You can use google to search for other answers

Custom Search

More Threads

• Sizing buttons in buttoncolumn in DataGridView
• Setting a window's content to display an image stored in a saved image file
• datetimepicker null values, control readonly property
• Graphics Class: Can I Plot a Single Point?
• User resizing PictureBox at runtime
• Weird Treeview behavior when cleared
• Enforcing repaint in a form.
• Create Date user control
• Caret in text box focus
• How set CurrentUICulture in the backgroundworker thread?