ElektroStudios
ElektroStudios

Reputation: 20494

Generic EventArgs class is not showing on the WinForms designer's propertygrid

Under WinForms on Visual Studio 2015 Enterprise Edition, I defined a generic class that inherits from EventArgs, like this:

C# version:

/// <summary>
/// Defines the event-data of an event that notifies for item addition changes in a collection.
/// Suitable to notify changes of <see cref="ListView.ListViewItemCollection"/> for example.
/// </summary>
public class ItemAddedEventArgs<T> : EventArgs
{ }

VB.NET version:

''' <summary>
''' Defines the event-data of an event that notifies for item addition changes in a collection.
''' Suitable to notify changes of <see cref="ListView.ListViewItemCollection"/> for example.
''' </summary>
Public Class ItemAddedEventArgs(Of T) : Inherits EventArgs
End Class

Then I defined a custom ListView class, like this:

C# version:

public class ElektroListView : ListView {

    [Browsable(true)]
    [EditorBrowsable(EditorBrowsableState.Always)]
    [Localizable(false)]
    [Description("Occurs when a new item is added into the items collection of the control.")]
    public event EventHandler<ItemAddedEventArgs<ListViewItem>> ItemAdded;

}

VB.NET version:

Public Class ElektroListView : Inherits ListView

    <Browsable(True)>
    <EditorBrowsable(EditorBrowsableState.Always)>
    <Localizable(False)>
    <Description("Occurs when a new item is added into the items collection of the control.")>
    Public Event ItemAdded As EventHandler(Of ItemAddedEventArgs(Of ListViewItem))

End Class

However, that ItemAdded event is not shown in the events list of the visual studio's propertygrid:

enter image description here

...I think it is because I'm using a genetic type parameter so maybe the property initializer mechanism of the property grid can't resolve it.

My question: what and how I should modify my ItemAddedEventArgs(Of T) class in order to see and treat it in the propertygrid as a "normal" event?. (this means, that I can double click on the event name to create the auto-generated code of the event handler)

Maybe I just need to use some kind of metadata/attribute classes on the event definition for these kind of scenario, or maybe this could be resolved by implementing some kind of TypeConverter class, I really don't know, I'm just trying to give ideas...

Please note that I would like to preserve the generic class that I have, instead of creating a new non-generic class that will serve only to supply one kind of type (ListViewItem).


UPDATE

Thanks to this C# example now I can see the event name in the propertygrid by declaring the event in this way:

Public Delegate Sub ItemAddedDelegate(sender As Object, e As ItemAddedEventArgs(Of ListViewItem))
Public Event ItemAdded As ItemAddedDelegate

However, when I double-click on the event name in the propertygrid, Visual Studio does not generate the typename for the generic type parameter neither the Handles clausule for VB.NET. It generates this code:

Private Sub ElektroListView1_ItemAdded(sender As Object, e As ItemAddedEventArgs(Of T))
End Sub

... What more I must do to fix that issue?.

Upvotes: 2

Views: 264

Answers (1)

Reza Aghaei
Reza Aghaei

Reputation: 125312

You can create a non-generic event arg deriving from your generic event arg and use it.

C#

public class EventArg<T> : EventArgs { /*...*/ }
public class ItemAddedEventArgs: EventArg<ListViewItem> { /*...*/ }
public class MyListView : ListView
{
    public event EventHandler<ItemAddedEventArgs> ItemAdded;
    /*...*/ 
}

VB.NET

Public Class EventArgs(Of T)
    Inherits EventArgs
    '...
End Class
Public Class ItemAddedEventArgs
    Inherits EventArgs(Of ListViewItem)
    '...
End Class
Public Class MyListView
    Inherits ListView
    Public Event ItemAdded As EventHandler(Of ItemAddedEventArgs)
    '...
End Class

Upvotes: 1

Related Questions