0

I am pretty new in the programming world and cannot understand how to use this code from an answer here: How to avoid thousands of needless ListView.SelectedIndexChanged events by Robert Jeppesen.

public class DoublebufferedListView : System.Windows.Forms.ListView
{
 private Timer m_changeDelayTimer = null;
 public DoublebufferedListView()
    : base()
 {
    // Set common properties for our listviews
    if (!SystemInformation.TerminalServerSession)
    {
       DoubleBuffered = true;
       SetStyle(ControlStyles.ResizeRedraw, true);
    }
 }

 /// <summary>
 /// Make sure to properly dispose of the timer
 /// </summary>
 /// <param name="disposing"></param>
 protected override void Dispose(bool disposing)
 {
    if (disposing && m_changeDelayTimer != null)
    {
       m_changeDelayTimer.Tick -= ChangeDelayTimerTick;
       m_changeDelayTimer.Dispose();
    }
    base.Dispose(disposing);
 }

 /// <summary>
 /// Hack to avoid lots of unnecessary change events by marshaling with a timer:
 /// https://stackoverflow.com/questions/86793/how-to-avoid-thousands-of-needless-listview-selectedindexchanged-events
 /// </summary>
 /// <param name="e"></param>
 protected override void OnSelectedIndexChanged(EventArgs e)
 {
    if (m_changeDelayTimer == null)
    {
       m_changeDelayTimer = new Timer();
       m_changeDelayTimer.Tick += ChangeDelayTimerTick;
       m_changeDelayTimer.Interval = 40;
    }
    // When a new SelectedIndexChanged event arrives, disable, then enable the
    // timer, effectively resetting it, so that after the last one in a batch
    // arrives, there is at least 40 ms before we react, plenty of time 
    // to wait any other selection events in the same batch.
    m_changeDelayTimer.Enabled = false;
    m_changeDelayTimer.Enabled = true;
 }

 private void ChangeDelayTimerTick(object sender, EventArgs e)
 {
    m_changeDelayTimer.Enabled = false;
    base.OnSelectedIndexChanged(new EventArgs());
 }
}

So it is a reusable class whose behaviour I would like to add to my already existing ListViews. I am currently managing with the code provided by Ian Boyd on the same url, adding this code to all my ListViews. The problem is that there are so many ListViews already in the ERP program I am currently working on and new ones are coming all the time. Seems like a waste to rewrite the same code all the time.

Timer changeDelayTimer = null;

private void lvResults_SelectedIndexChanged(object sender, EventArgs e)
{
    if (this.changeDelayTimer == null)
    {
        this.changeDelayTimer = new Timer();
        this.changeDelayTimer.Tick += ChangeDelayTimerTick;
        this.changeDelayTimer.Interval = 200; //200ms is what Explorer uses
    }
    this.changeDelayTimer.Enabled = false;
    this.changeDelayTimer.Enabled = true;
}

private void ChangeDelayTimerTick(object sender, EventArgs e)
{
    this.changeDelayTimer.Enabled = false;
    this.changeDelayTimer.Dispose();
    this.changeDelayTimer = null;

    //Add original SelectedIndexChanged event handler code here
    //todo
}

I tried to search an answer for this topic from google, but I could not find the right articles by the keywords I was using. Hope someone here can guide me to the right direction on how these 'classes' that are considered as 'components' are to be used with windows form tools.

Community
  • 1
  • 1
  • Your class must be already in the ToolBox of your project(after building at least). Make sure your team will use your control when they need new `ListView`. Then change all existed ListView control to your control. Because it is derived from ListView it can be achieved by replacing ListView declaration and Constructor executing code with "Find-Replace" – Fabio Nov 03 '15 at 13:14
  • This was a helpful method Fabio, thx. – Mika Rissanen Nov 03 '15 at 13:59

1 Answers1

0

You either need to add the control programmatically:

var lb = new DoublebufferedListView();
yourForm.Controls.Add(lb);

[Older versions of visual studio]

Or by right-clicking the toolbox, selecting Customize Toolbox, and from the .NET Framework Components tab, clicking Browse and locating the Control Library DLL to add it to the toolbox.

[Newer versions (from msdn)]

On the Tools menu, click Choose Toolbox Items.

Click Browse.

The Open dialog box appears.

In the Folders pane, select My Computer to browse for items installed on your computer drives.

Click OK.

Stephen Brickner
  • 2,584
  • 1
  • 11
  • 19
  • Worked like a charm. Didn't even need to browse anything. Visual Studio just loaded the .NET components for a while when I went to the Tools -> Choose Toolbox Items. Pressed ok and my DoublebufferedListView appeared in the Toolbox. – Mika Rissanen Nov 03 '15 at 13:58