How to improve windows form tools with reusable classes in C#?

519 views Asked by At

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.

1

There are 1 answers

1
Stephen Brickner On BEST ANSWER

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.