ListView Hot Tracking - missing functionality

  • Thread starter Thread starter Dave Cline
  • Start date Start date
D

Dave Cline

Tribe,

Looking to see if anyone has coded up hot tracking for listbox or
listview controls.

Essentially it you do this:

ListViewItem hotListViewItem;

private void listView1_MouseMove(object sender, MouseEventArgs e)
{
ListView lv = (ListView)sender;
ListViewItem item = lv.GetItemAt(e.X, e.Y);
if (item != null)
{
item.BackColor = Color.Wheat;
if (hotListViewItem != null && !hotListViewItem.Equals(item))
hotListViewItem.BackColor = Color.White;
hotListViewItem = item;
}
}

You can make the rows track hot BUT there is an awful lot of flicker
when you do this. Anyone done this kind of thing and found an
alternative method?

Regards,
Dave Cline
~bangeye.com~
 
If you set a BackColor on a control that is the same as the current
BackColor, no repainting should occur... I'm not sure where your
flicker is coming from. If you could come up with a small repro case
that would easily compile and run, without me the group having to
code a working sample we could probably better answer your
questions in this case. Just takes too much time to try and reproduce
your scenario.
 
Justin, All,

Here is a form which exhibits the below flashing behavior.

Thanks for any insight.
Dave Cline
~bangeye.com~

/////////////////////////////////////
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace Testing
{
/// <summary>
/// Summary description for TestForm.
/// </summary>
public class TestForm : System.Windows.Forms.Form
{
// Custom fields
ListViewItem hotListViewItem;

private System.Windows.Forms.ListView listView1;
private System.Windows.Forms.ColumnHeader columnHeader1;
private System.Windows.Forms.ColumnHeader columnHeader2;
private System.Windows.Forms.ColumnHeader columnHeader3;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public TestForm()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing ){
if(components != null){
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
private void InitializeComponent()
{
this.listView1 = new System.Windows.Forms.ListView();
this.columnHeader1 = new System.Windows.Forms.ColumnHeader();
this.columnHeader2 = new System.Windows.Forms.ColumnHeader();
this.columnHeader3 = new System.Windows.Forms.ColumnHeader();
this.SuspendLayout();
//
// listView1
//
this.listView1.Anchor =
((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top
| System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.listView1.Columns.AddRange(new
System.Windows.Forms.ColumnHeader[] {
this.columnHeader1,
this.columnHeader2,
this.columnHeader3});
this.listView1.Location = new System.Drawing.Point(4, 8);
this.listView1.Name = "listView1";
this.listView1.Size = new System.Drawing.Size(280, 260);
this.listView1.TabIndex = 0;
this.listView1.View = System.Windows.Forms.View.Details;
this.listView1.MouseMove += new
System.Windows.Forms.MouseEventHandler(this.listView1_MouseMove);
this.columnHeader1.Text = "Col One";
this.columnHeader1.Width = 152;
this.columnHeader2.Text = "Col Two";
this.columnHeader3.Text = "Col Three";
//
// TestForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.listView1);
this.Name = "TestForm";
this.Text = "TestForm";
this.Load += new System.EventHandler(this.TestForm_Load);
this.ResumeLayout(false);

}
#endregion

private void TestForm_Load(object sender, EventArgs e)
{
ListViewItem item;
for (int i = 0; i < 50; i++)
{
item = new ListViewItem(new string[]
{
i.ToString() + ": Item testing rollover color switch",
"subitem 1",
"subitem 2"
});
this.listView1.Items.Add(item);
}
}

private void listView1_MouseMove(object sender, MouseEventArgs e)
{
ListView lv = (ListView)sender;
ListViewItem item = lv.GetItemAt(e.X, e.Y);
if (item != null)
{
item.BackColor = Color.Wheat;
if (hotListViewItem != null && !hotListViewItem.Equals(item))
hotListViewItem.BackColor = Color.White;
hotListViewItem = item;
}
}
}
}
/////////////////////////////////////
 
Back
Top