views:

289

answers:

2

Good Day,

I'm using Generics with a ListView control whose initial class definition looks like this:

namespace BaseControlLibrary
{
    public partial class CustomListView<T> : System.Windows.Forms.ListView
    {
        // Custom fields, properties, methods go here

        public CustomListView(List<T> data)
        {
            _columnInfo = new Dictionary<int, string>();
            _columnIndex = 0;

            _lvwItemComparer = new ListViewItemComparer();
            this.ListViewItemSorter = _lvwItemComparer;

            InitializeColumnNames();
            BindDataToListView(data);

            this.Invalidate();
        }
    }
}

Here is my designer file:

partial class CustomListView
{
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    //protected override void Dispose(bool disposing)
    //{
    //    if (disposing && (components != null))
    //    {
    //        components.Dispose();
    //    }
    //    base.Dispose(disposing);
    //}

    #region Component Designer generated code

    /// <summary>
    /// Required method for Designer support - do not modify 
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        components = new System.ComponentModel.Container();
        // this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    }

    #endregion
}

What I want to do is to create a Windows Control Library and I have successfully done so, but the problem occurs when I can't add the DLL to the toolbox. I'm not exactly sure why I can't do this. I thought all Windows Forms Controls implement the IComponent interface which is a requirement to add items to the tool box. Is it because there's type parameter as part of the class definition?

+2  A: 

The designer hates:

  • generics
  • things with abstract base-classes

Even if it works at runtime, you're probably not going to get it to work in the IDE. Sorry. Perhaps consider a non-generic class with a Type property; that's about the best you'll do...

btw, CustomListView<T> and CustomListView are completely different classes. You have 2 classes, not one.

Marc Gravell
+1  A: 

You cannot use generic controls (i.e. control specialized through generics) in the designer. [I seem to remember reading that it was a design decision by the VS team, but i can't find the reference.]

For ObjectListView I used a Adapter pattern to provide typed access to a ListView control.

public class TypedObjectListView<T> where T : class
{
    /// <summary>
    /// Create a typed wrapper around the given list.
    /// </summary>
    public TypedObjectListView(ObjectListView olv) {
        this.olv = olv;
    }

    public void BindTo(IList<T> objects) {
       // Manipulate the attached ListView here
    }

    // plus whatever other methods you want
}

and you would use it like this:

TypedObjectListView<Person> tlist = 
   new TypedObjectListView<Person>(this.listView1);
tlist.BindTo(myListofPeople);

Or, instead of writing everything yourself, you could just use ObjectListView :)

Grammarian